Brain Dump

Docker

Tags
sys-design

Is a cross platform container management framework.

Dockerfile

Describes the step-by-step process of constructing a container image.

This involves mounting any local directories into the container, installing any base packages or dependencies, and specifying a base image to inherit any defaults from.

# Use archlinux as the base image.
FROM archlinux

# Install any python pip dependencies.
RUN python3.10 -m pip install termcolor

# Set the working directory for any of the following RUN, CMD, ENTRYPOINT, COPY and
# ADD instructions.
WORKDIR /workarea

# ...

A docker image is a stack of layers, built-up from each command in the Dockerfile. Every command adds a new layer to the stack and Docker stores only the deltas from one layer to the next. This way if you change one command only the layers from that command forward need to be rebuilt, the rest can reuse the cached build for the previous layers.

Command Reference

Building a Docker Image

docker build -t hello .
Code Snippet 1: Build an image with the tag hello from the Dockerfile in the cwd.

Running a Command in a Docker Container

docker run --rm -it hello /bin/bash
Code Snippet 2: Run the command /bin/bash in a new container created from the image tagged hello.

The --rm flag automatically removes the container file-system once the process you spawn exits.

Note: run always starts a new container. If a container with the same image is up and running then it doesn't matter, Docker will start a new container from that image again. If you'd like to reuse the same container multiple times use exec instead of run.

Running a Command with an Init Process

docker run --init hello /path/to/server.py

This makes Docker start the container with an Init process that forwards signals and reaps processes. This relates to PID 1 zombie reaping problem for containers.

Mount the cwd Into the Container

docker run -v "$(pwd -P)":/workarea hello
Code Snippet 3: Mount the cwd into the container at /workarea and run the default command for the image tagged hello.

Note: You can specify a command to run instead of the default command.

Links to this note