Docker commands

@docker @cheatsheet

Start docker daemon (arch)

If you install Docker Desktop on Windows, everything is installed and managed already. But on Arch for example, you need to start the docker daemon as such: systemctl start docker.

Docker file

Build an image from a docker file: docker build -t [imagename] [path to docker file]

Example Dockerfile:

FROM node:12-alpine
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --production
COPY . .
CMD ["node", "src/index.js"]

.dockerignore

Specify files/folders that are not relevant to your image under .dockerignore to avoid unnecessary work while building images.

Layer caching (best practice)

  • Each instruction in a docker file will introduce an image layer
  • If a layer changes, all lower layers need to be rebuilt
  • Therefore make sure that layers that practically never change are defined first in your docker file!
    • A good example is dependencies

Images

Build a Docker image

docker build -t [imagename] [path to docker file]

The -t flag "tags" our image, which is just giving a human-readable name to reference it, as opposed to a hash only.

List your images

An image will not be written directly to the file system at the location of the Docker file, but can be found as follows:

docker image ls

Delete images

docker rmi <HASH>

Share an image on Docker Hub

Start by logging in to the container registry where you want to publish your image.

  • Login to Docker hub from CLI: docker login -u USERNAME
  • Login to Azure: docker login azure.

The push command requires you to tag an image you want to push. I'm not entirely sure why. This tag is a shorthand for referring to source image. In the following example, docker101tutorial is the name of the source image and edwinwenink127/docker101tutorial is the tag:

docker tag docker101tutorial edwinwenink127/docker101tutorial
docker push edwinwenink127/docker101tutorial

In this case we did not specify a tag to the image name, in which case Docker defaults to using the latest tag. If you do want to specify a specific tag, use the following format:

docker tag local-image:tagname new-repo:tagname
docker push new-repo:tagname

N.B. in this example edwinwenink127 is my account name, and it seems it is required to specify the correct namespace in this way! (Using edwinwenink128/docker101tutorial resulted in access denied.)

Scan image on vulnerabilities

Scan images on vulnerabilities using the Snyk external tool: docker scan [image].

See image layering (its history)

  • docker image history [image].
  • docker image history --no-trunc [image].

Containers

Run a container from an image

To create a new container from an image (here: hello-world):

docker run [image name]

Another example:

docker run -d -p 80:80 --name docker-tutorial docker101tutorial

  • The -d flag detaches the container so that it runs in the background, but does print the container ID so you can easily connect to it.
  • The p flag "publishes" a list of container ports to the host. In this case it maps port 80 of the host to port 80 in the container.
  • Flags can be combined: -dp
  • --name specifies the name of the container.
  • docker101tutorial is the name of the image created earlier.

To start a container again (but not create a new one from an image):

docker run [container ID]

And:

docker stop [container ID]

Find the container process ID with docker ps or docker container ls.

N.B. depending on the image, the container stays running or not. If you run a single command, the container will close. If you run a server or a continuing command in it, it will stay running. For example, the following container will close immediately after running the command:

docker run -d ubuntu bash -c "ls /"

But the following container, from the same image, will continue running:

docker run -d ubuntu bash -c "tail -f /dev/null"

List all containers spawned by the image (running, all including exited ones, all in quit mode)

  • docker ps (shorthand, "process status")
  • docker container ls
  • docker container ls --all
  • docker container ls -aq

Removing docker containers

To remove container with specific hash, first stop it and then rm it (recall, to get the container hash docker ps):

docker stop HASH
docker rm HASH

You can skip the stop step by forcing the removal: docker rm -f HASH.

Remove all inactive containers:

docker container prune

Executing commands in containers

In Docker Desktop there is a "Terminal" tab after clicking on a running container. From command line, you need to use the exec command:

docker exec [container id] cat /data.txt

Example: Running a docker container with a complete Scala environment (optional: remove it after use / spawn it in a shell)

N.B. running an image will create a new container. This is probably not what you want, so use start on an existing container instead.

docker run -it --rm williamyeh/scala

docker run -it williamyeh/scala /bin/bash

N.B. run creates a new container from an image. If you already have a container, use docker start NAME and docker attach NAME

To spawn a shell in a running container, use docker exec -it [container] /bin/bash instead.

What is the difference between run and exec? The exec function executes a command in a running container rather than creating a container and immediately running it from an image.

Copying a file from your regular user into a docker container

First find the hash value of the container:

docker ps -a

Then copy the file from your system into the container root

docker cp HelloWorld.scala vigourous_shockley:/ where vigourous_shockley is the generated name for the container.

docker start vigourous_shockley

docker attach vigourous_shockley

Volumes

When running a container data updates are made in its local "scratch" space, but these changes are not persistent. So if you write data in container A from image X and then start a new container B from the same image X, container B will not contain the changes from container A. If you want to write data in a persistent way, you should write it to a volume.

There are two types of volumes:

  • named: we specify a name for the volume, but otherwise do not worry about where the data is stored (no control over the mount point)
  • bind mounts: control the specific mount point for the data
    • for data persistence
    • provide additional data in containers: e.g. mount our source code into the container
    • use tools to watch for file changes and restarting the application, such as nodemon for Node.js
    • but note that the new volume is not populated with container contents

Named

  • Create a named volume: docker volume create [name]
  • Start a container with a volume mount: docker run -dp 3000:3000 -v [name]:[local file path] [image]
  • Inspect volume mount: docker volume inspect [name]
    • The mountpoint is the actual location on disk where the data is stored (typically under a root folder like /var/lib/docker/volumes/)

Bind Mounts

Example:

docker run -dp 3000:3000 \
    -w /app -v "$(pwd):/app" \
    node:12-alpine \
    sh -c "yarn install && yarn run dev"
  • -w /app sets the containers working directory
  • -v "$(pwd):/app": bind mount i.e. link the host's present working directory (checked out in the local app directory) to /app in the container.
    • absolute paths are needed

Logs

  • docker logs -f [container id]
  • "Follow" the logs as the come in: docker logs -f [container id]

Multi-container Apps

Why multi-container apps?

  • Containers should do one thing and do it well
  • Scale front-end and API different from backend
  • Logically separating app and database makes it easy to use a managed service for the database in production

Networking

In order for containers to communicate, we need to connect containers to the same network.

First create a network: docker network create [name]. Then connect it to the network. The following example connects a container with a MySQL backend to a network called "todo-app":

docker run -d \
    --network todo-app --network-alias mysql \
    -v todo-mysql-data:/var/lib/mysql \
    -e MYSQL_ROOT_PASSWORD=secret \
    -e MYSQL_DATABASE=todos \
    mysql:5.7
  • if you have not made the volume todo-mysql-data docker will autocreate it.
  • the -e flag passes environment variables into the container
    • Don't pass secrets in production, too easy to expose
    • Docker doesn't do anything with this; your app is responsible for doing something with the env variables

Connect a netshoot container (see https://github.com/nicolaka/netshoot) to the network we just created:

docker run -it --network todo-app nicolaka/netshoot

Inside that container, you can use the network alias in commands, as such: dig mysql. This is super handy! You can just easily talk to the mysql server using this alias, instead of having to find and enter an IP adres.

You can also connect a container with an app to this MySQL backend, e.g.:

docker run -dp 3000:3000 \
  -w /app -v "$(pwd):/app" \
  --network todo-app \
  -e MYSQL_HOST=mysql \
  -e MYSQL_USER=root \
  -e MYSQL_PASSWORD=secret \
  -e MYSQL_DB=todos \
  node:12-alpine \
  sh -c "apk --no-cache --virtual build-dependencies add python2 make g++ && yarn install && yarn run dev"

Docker compose

Create a docker-compose.yml file. Each container startup command will correspond to a "service" entry in the YAML file.

  • Compose supports relative paths, regular docker in terminal does not
  • You need to be explicit with Compose. E.g. volumes are not created automatically.
  • Docker compose automatically creates a network for the containers included in the stack!

Reference commands:

docker run -dp 3000:3000 \
  -w /app -v "$(pwd):/app" \
  --network todo-app \
  -e MYSQL_HOST=mysql \
  -e MYSQL_USER=root \
  -e MYSQL_PASSWORD=secret \
  -e MYSQL_DB=todos \
  node:12-alpine \
  sh -c "yarn install && yarn run dev"

and

docker run -d \
  --network todo-app --network-alias mysql \
  -v todo-mysql-data:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=secret \
  -e MYSQL_DATABASE=todos \
  mysql:5.7

Translated to Docker Compose:

version: "3.8"

services:
  app:
    image: node:12-alpine
    command: sh -c "yarn install && yarn run dev"
    ports:
      - 3000:3000
    working_dir: /app
    volumes:
      - ./:/app
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: root
      MYSQL_PASSWORD: secret
      MYSQL_DB: todos

  mysql:
    image: mysql:5.7
    volumes:
      - todo-mysql-data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: todos

volumes:
  todo-mysql-data:

Commands:

  • Just build, but do not start containers docker compose build
  • Start up containers: docker compose up
    • Will also build if not done yet
  • Remove containers: docker compose down