Docker and container concepts
63 concepts explained in plain words, each linked to the exercises that train it.
Containers and images
- Container A container is an ordinary process on the server, isolated by the kernel: it sees its own files, its own network and its own process list, and gets a share of CPU and memory. It starts in a fraction of a second because there is no operating system to boot.
- Image An image is a read-only package of everything a program needs to run: files, libraries, environment variables and the command to start. You build it once, then run it anywhere, as many times as you like.
- Image vs container The image is the recipe, the container is the dish: one image can start any number of containers, each with its own state. Deleting a container leaves the image intact, and changing a container never changes the image.
- Writable layer Each container gets a thin writable layer on top of its image. Every file it creates or changes lands there, and that layer is deleted with the container.
- Container lifecycle A container goes through created, running, exited and removed. docker stop, start and restart move it between these states; only docker rm deletes it, with its writable layer.
- Main process (PID 1) A container lives exactly as long as its main process, the one started by CMD or ENTRYPOINT. When that process exits, the container stops, whatever else runs inside.
- Exit codes When a container stops, docker ps -a shows the exit code of its main process. The number tells why it stopped: 0 it finished, 1 the app failed, 137 it was killed, 143 it was asked to stop.
Building images
- Dockerfile A Dockerfile is the recipe of an image: a base image, then instructions that copy files, run commands and say how the container starts. docker build turns it into an image, with one layer for each instruction that changes files.
- CMD and ENTRYPOINT CMD and ENTRYPOINT tell a container what to run. ENTRYPOINT is the fixed program, CMD the default arguments, which docker run can replace.
- Image layers An image is a stack of read-only layers, one for each instruction that changes files. Images built on the same base share its layers, on disk and in downloads.
- Build cache docker build reuses a layer when its instruction and its inputs have not changed since the last build. The first instruction that changes invalidates the cache of every instruction after it.
- Build context The build context is the folder given to docker build, often the final dot. Its files are sent to the builder, and they are the only files COPY can read.
- .dockerignore A .dockerignore file at the root of the build context lists files that are never sent to the builder. It keeps images small and keeps secrets out of them.
- Multi-stage builds A multi-stage Dockerfile has several FROM lines: the first stages compile with the full toolchain, the last one copies only the result. Compilers and sources never reach the final image.
- Base images The FROM image decides most of the final size, the tools available and the security fixes you inherit. Full, slim, alpine and distroless variants trade convenience for size.
- Static binaries A statically linked binary carries every library it needs, so it runs on scratch or distroless with nothing else in the image. Go builds one with CGO_ENABLED=0.
Data and disk
- Volumes A volume is storage managed by Docker that lives outside any container. Mount it where an app writes its data, and the data survives docker rm, upgrades and recreated containers.
- Bind mounts A bind mount puts a folder of the server inside the container. A change on one side is seen at once on the other, which is ideal in development and risky in production.
- Docker disk usage Images, stopped containers, volumes and build cache pile up on the server's disk until something runs out of space. docker system df shows what takes room and how much can be reclaimed.
- Dangling images A dangling image is an old image whose tag moved to a newer build. It shows up as <none>:<none>, nothing refers to it, and it can be removed safely.
- Log rotation Docker keeps everything a container prints, by default in a json-file that only disappears with the container. Rotation caps it: max-size and max-file, per container or for the whole daemon.
- Upgrading a database container A minor version reads the same data files: change the tag, keep the volume. A major version cannot: dump the data with the old version and restore it into the new one.
Networking
- Port publishing A container's ports are closed to the outside world until you publish them. -p 8080:80 forwards port 8080 of the server to port 80 of the container.
- Default bridge network Containers started without --network join the default bridge network. They can reach each other by IP address, but not by name.
- User-defined networks A network created with docker network create gives its containers a DNS: each one is reachable by its name. It also keeps them apart from containers on other networks.
- EXPOSE vs publishing EXPOSE in a Dockerfile only documents the port the app listens on; it opens nothing. Publishing with -p or ports: is what makes a port reachable from outside the server.
- Proxies and container addresses Docker's DNS (127.0.0.11) returns the current address of a service's containers. A proxy that resolved the name once keeps the old address after the container is recreated.
Running containers
- Container logs Whatever the main process writes to stdout and stderr becomes the container's log. docker logs is the first thing to read when a container misbehaves.
- docker exec docker exec runs an extra command inside a running container, in the same files and the same network. It is how you look around, test a connection or open a shell.
- Environment variables Environment variables configure a container at start without rebuilding its image: the same image runs in staging and in production with different values. -e, --env-file and environment: in Compose set them.
- Restart policies A restart policy tells Docker what to do when a container stops: nothing (no), restart it after a crash (on-failure), or keep it running through crashes and reboots (unless-stopped, always).
- Healthchecks A healthcheck is a command Docker runs regularly inside the container to know whether the app really works. The container's status becomes starting, healthy or unhealthy.
- Graceful shutdown docker stop sends SIGTERM to PID 1, waits for the grace period (10 seconds by default), then sends SIGKILL. A clean stop needs the app to receive the signal, and enough time to finish its work.
Memory and CPU
- Resource limits By default a container may use all the memory and all the CPU of the server. --memory and --cpus cap it, so one runaway service cannot take the whole server down.
- OOM killer When a container goes over its memory limit, or the server runs out of memory, the kernel's OOM killer kills a process to free memory. The container exits with code 137.
- Swap Swap is disk space the kernel uses as extra memory. It delays out-of-memory kills, but memory on disk is hundreds of times slower, so every service on the server slows down.
Docker Compose
- Docker Compose Compose describes a whole application, its services, networks and volumes, in one compose.yaml file. docker compose up creates and starts everything, in the right order, with one command.
- Compose networks Compose creates a network for each project and attaches every service to it. Services reach each other by service name, like db or cache, on their own ports.
- Compose override files Compose merges several files: compose.yaml for what is shared, compose.override.yaml for development, and a production file given with -f. Each environment only changes what differs.
- Startup order depends_on starts the database before the API, but started is not ready: PostgreSQL needs a few seconds before it accepts connections. condition: service_healthy waits until it really does.
- Dev/prod parity Development and production should run the same image with different settings. Code mounted from the laptop, debug ports and hot reload belong to development only.
Security
- Keeping secrets out of images Anything written into an image, by ENV, ARG, COPY or a RUN, can be read by whoever can pull it, even if a later layer deletes it. Secrets are given to the container when it starts.
- Running as non-root Containers run as root unless told otherwise. With USER in the Dockerfile, a compromised app only has the rights of an ordinary user.
- File permissions in containers Inside a container, Linux permissions work as on any server: a process can only write where its user is allowed to. Users are numbers (UIDs), shared between the container and the server.
- Build secrets Some builds need a credential: a private registry token, an SSH key. BuildKit secrets give it to one RUN only, without writing it into any layer, history or cache key.
- Read-only root filesystem read_only: true mounts the container's image read-only: nothing can be added or changed outside volumes and tmpfs mounts, neither by the app nor by an attacker who takes it over.
- Linux capabilities Root's powers are split into capabilities: CHOWN, SETUID, NET_RAW and so on. Docker gives 14 of them to every container by default; most services need none.
Shipping images
- Image registries A registry stores images so that other machines can pull them. An image's name is its address: registry host, project, repository and tag, as in registry.shop.internal/shop/api:1.4.0.
- Build once, deploy everywhere An image is built once, by the CI, and that very image goes to every environment and every server. Servers pull it; they never build it.
- Image digests A digest (sha256:...) is the hash of an image's manifest: it names exactly one image, forever. A tag is a label that anyone allowed to push can move to another image.
Orchestration with Swarm
- Swarm mode docker swarm init turns Docker engines into a cluster: managers keep the desired state and schedule work, workers run it. You describe services; the cluster keeps them running across the nodes.
- Services and tasks A service is a desired state (an image, a number of replicas, constraints); the managers turn it into tasks, one container each, placed on the nodes that can run them, and replace any task that ends.
- Overlay networks and the routing mesh Overlay networks span every node of the swarm. Each service gets a virtual IP that balances over its healthy tasks, and a published port answers on every node and forwards to a task wherever it runs.
- Rolling updates docker service update replaces a service's tasks batch by batch, following deploy.update_config: how many at a time, in which order, how long to watch each one. Done right, customers never notice a release.
- Automatic rollback update_config.failure_action decides what Swarm does when a new task fails during its monitor window: pause (default), continue, or rollback to the previous spec with rollback_config.
- Swarm secrets docker secret create stores a value encrypted in the managers' Raft log; a service that lists it gets it as a file in /run/secrets, in memory, and nowhere else. docker service inspect never shows its value.
- Placement constraints deploy.placement.constraints limits the nodes a service may run on: by role, host name, or labels you put on nodes. It is how a stateful service stays where its data is.
- Managers and quorum Managers agree through Raft: every change needs a majority of all the managers. 3 managers tolerate 1 failure, 5 tolerate 2; 2 managers tolerate none.
Designing a platform
- Supply-chain security Every image you run is built on images and packages written by others. Supply-chain security is making sure that what you build and deploy is exactly what you reviewed, even if a registry, a mirror or an account is compromised.
- Multi-platform images An image is built for one CPU architecture. An image index groups several, one per platform (linux/amd64, linux/arm64), under a single tag: each machine pulls the one it can run.
- Capacity planning Capacity planning is making sure the cluster can hold what the services need, including after losing a node (N+1), and telling the scheduler what each task needs so that it places them accordingly.
- Disaster recovery drills A recovery plan says how to rebuild everything somewhere else from what survives: the repository, the registry, the vault and the backups. RTO is how long it takes, RPO how much data can be lost. Only a drill proves both.
No concept matches these filters.