Getting Started with Docker

A few months back I taught a workshop for the FullStack Community in Cluj regarding this very subject, Getting started with Docker. Given the popularity of the subject and the positive review of the workshop, I decided to start writing this series in which I plan to cover the basics of Docker, Docker Compose, Open Stack and Jenkins, culminating in Continuous Delivery with Docker, Jenkins on Open Stack.


So, why Docker?

Docker innovated over the virtual machine architecture popular at the time by the simple fact that a Docker container runs natively on Linux and shares the kernel of the host machine with other containers. It runs as a discrete process, taking no more memory than any other executable, making it lightweight. By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.


Installation

I'm going to skip over the installation process and just link to their documentation since is very well written.

Mac Os X

Windows

Linux


Concepts

Docker introduced three basic concepts to describe how the end user should interact with the stack.

Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

Anatomy of a Dockerfile

# base image we will work upon
FROM node:10

# maintainer label
LABEL maintainer="Darius Cupsa <cupsadarius@gmail.com>" 

# create the directory and change dir into it
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# expose the port
EXPOSE 80

# install os dependencies
RUN apt-get update -qq && apt-get install -y \
  ghostscript \
  libgs-dev \
  imagemagick \
  pdftk \
  pdfjam

# define available environment variables
ENV LOGGER_LEVEL ${LOGGER_LEVEL}
ENV LOGGER_SOURCE ${LOGGER_SOURCE}
ENV APP_ENV ${APP_ENV}
ENV TOKEN_SECRET ${TOKEN_SECRET}
ENV PORT ${PORT}
ENV MONGODB_DATABASE ${MONGODB_DATABASE}
ENV MONGODB_PASSWORD ${MONGODB_PASSWORD}
ENV MONGODB_USER ${MONGODB_USER}
ENV MONGODB_HOST ${MONGODB_HOST}

# copy the dependency tree
COPY package.json /usr/src/app/package.json
COPY package-lock.json /usr/src/app/package-lock.json

# install said dependencies
RUN npm install

# copy the rest of the sourcecode
COPY . /usr/src/app

# check for linting issues
RUN npm run lint

# define the entrypoint
CMD ["npm", "run", "-s", "start:migrate"]

Docker Image

An image is an executable package that includes everything needed to run an application–the code, a runtime, libraries, environment variables, and configuration files.

Docker Container

A container is a runtime instance of an image–what the image becomes in memory when executed (that is, an image with state, or a user process).

Usual commands

  • docker build -t <tag> . build a docker image with a set <tag>.
  • docker images list all docker images
  • docker rmi <image-id> remove an image
  • docker run -d --name <name> -p <port> -e <env> -v <host>:<path> <image> start a container with the name <name> running on port <port> with env variables <env> with <host> folder mapped to <path> in container based on <image>
  • docker ps list all running containers
  • docker ps -a list all containers running or not
  • docker stop <container> stop a running container
  • docker restart <container> restart a running container

In conclusion, if you got this far you should at least have a basic grasp of what Docker is and what it does.


References