Docker & Containers
Learn what a container really is, then build images from a Dockerfile, keep them small, and connect containers to data and to each other. By the end you can read a Compose file, debug a container that will not stay up, and ship a release you can roll back.
- Beginner
- 4 units, 12 lessons, 155 exercises
- Dockerfile, shell
- Builds on Linux & the Shell
Unit 01 · 3 lessons
Containers
See a container as an ordinary process with its own view of the machine
- 01Container or VM
A process with its own view, sharing the host kernel
- 02Running containers
run, ps, logs, exec, stop, rm, and where written files go
- BOSSImages are layers
Names, tags and digests, shared layers and the writable layer on top
Read the unit guide
A container is a process. The kernel gives it a private view of the machine: its own files, its own process list, its own network and hostname. Nothing is emulated and no second operating system boots, which is why a container starts about as fast as any other program.
Two kernel features do the work. Namespaces decide what the process can see (process IDs, network interfaces, mounts, hostname). Control groups (cgroups) decide how much it may use (memory, CPU). A virtual machine isolates by pretending to be hardware and booting its own kernel; containers share the host's kernel, so they are lighter and a little less isolated.
An image is the read-only package a container starts from: system files, a runtime, your app and a default command. docker run creates a container from an image. -d runs it in the background, --name names it, -e sets an environment variable and -p HOST:CONTAINER publishes a port, because the container's ports are otherwise its own.
Look after containers with docker ps (add -a for stopped ones), docker logs, docker exec, docker stop and docker rm. Whatever a container writes goes into its own thin writable layer. Stopping keeps that layer; removing the container deletes it.
Images are stacks of layers named by the hash of their content. A full name is registry/namespace/repository:tag, and the parts you leave out have defaults (Docker Hub, library, latest). A tag is a label the publisher can move; a digest names exact bytes forever.
The classic pitfall: treating a container like a small server you log into and fix. Anything changed inside is gone with the container. Change the image, or keep the data outside (unit 3).
Unit 02 · 3 lessons
Building images
Write a Dockerfile, keep its builds fast and its image small
- 01The Dockerfile
FROM, WORKDIR, COPY, RUN, ENV, EXPOSE, CMD and ENTRYPOINT
- 02The build cache
Why one changed line can rebuild everything after it
- BOSSSmall and safe
One image cut down: slim base, two stages, no root, a pinned base
Read the unit guide
A Dockerfile is a recipe read top to bottom. FROM picks the image to start from. WORKDIR sets the directory for the steps that follow. COPY brings files in from the build context, the directory you name at the end of docker build. RUN executes a command while the image is being built. CMD runs nothing during the build: it records the command a container starts with.
Commands come in two forms. Shell form (CMD python app.py) is run by /bin/sh -c, so variables expand, but a shell sits between Docker and your app. Exec form (CMD ["python", "app.py"]) is a JSON array run directly. ENTRYPOINT is the fixed part of the command and CMD the default arguments: whatever follows the image name in docker run replaces CMD and is added after ENTRYPOINT.
Each instruction is cached. On a rebuild Docker walks the file in order and reuses a step when the instruction and its input files are unchanged. The first step that differs is rebuilt, and so is everything after it. So put what changes rarely first: copy the dependency list, install, and only then copy your code.
A .dockerignore file keeps files such as .git out of the build context, so they are never sent, never copied and never spoil the cache.
Small images pull faster and hold less to patch. A multi-stage build compiles in one stage and copies only the result into a slim final stage. Add a USER line so the app does not run as root, and pin the base image so the build does not change under you.
The classic pitfall: COPY . . above the install step. Every edit to any file then reinstalls every dependency.
Unit 03 · 3 lessons
Data and networks
Keep data when containers go, and let containers find each other
- 01Volumes and bind mounts
Named volumes for data that must last, bind mounts for your source
- 02Container networking
Published ports, what localhost means inside, and names on a network
- BOSSCompose
A web app, a database and a cache described in one file
Read the unit guide
Containers are disposable: you replace one to upgrade it, and its writable layer goes with it. Data that must last has to live outside. A named volume is storage that Docker manages; you mount it at a path with -v pgdata:/var/lib/postgresql/data, and it stays until you remove it yourself.
A bind mount puts a host directory you choose into the container: -v "$(pwd)":/app. Both sides see the same files, so an edit on the host shows up inside at once. That is what you want for source code while developing. Either kind of mount hides whatever the image had at that path.
Every container has its own network namespace, so localhost inside a container is that container alone. -p HOST:CONTAINER lets the outside world in. For containers to talk to each other, put them on a user-defined network (docker network create): Docker's DNS then resolves each container's name, and they connect on the container's own port, published or not.
Compose writes all of this down in one file. Each entry under services is a container with its image or build, ports, environment and volumes. docker compose up creates a network for the project, and every service is reachable by its service name.
Started is not ready. depends_on only orders the starts. With a healthcheck on the database and condition: service_healthy on the app, Compose waits until the database actually answers.
The classic pitfall: pointing an app at localhost to reach a database in another container. Use the service name.
Unit 04 · 3 lessons
Production
Run the same image everywhere, find out why one died, and release with a way back
- 01Configuration and secrets
One image for every environment, settings and secrets from outside
- 02Debugging containers
Why it exited: exit codes, logs, inspect, exec and memory limits
- BOSSShip it
Tag a release, build it in CI, push it, check its health, roll it back
Read the unit guide
The image that passed your tests should be the image that runs in production, byte for byte. So nothing that differs between environments may be built into it. Addresses, modes and credentials come from the environment when the container starts: -e, --env-file, or the platform that runs it.
Secrets never go into an image. Everyone who can pull an image can read every layer, and docker history prints the ENV and ARG values that built it. A file copied in and deleted later is still in the layer that added it. Pass secrets at run time; for a build that needs one, use a secret mount.
A container lives as long as its main process. When it exits, look at docker ps -a for the exit code and docker logs for its last words. 0 means it finished, 127 that the command was not found, and 128 + n that signal n ended it: 137 is SIGKILL, which is what the kernel sends when a container goes over its memory limit (--memory), and what docker stop sends when the app ignores SIGTERM.
docker inspect shows how a container was configured and how it ended. docker exec only works in a running container; to look inside an image whose container will not stay up, start a new one with a shell as its command.
Shipping is build, tag, push, deploy. CI builds from a clean checkout and tags the image with something that never moves: a version or a commit hash. A health check tells the deploy whether the new container actually answers. Rolling back is deploying the previous tag.
The classic pitfall: deploying latest, or pushing a fix under a tag that is already out. Then no one can say which image a server is running.
Learn Docker & Containers on your phone.
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.