Chapter 01
Images, containers and the engine
What an image is, what a container is, and the everyday commands for starting, listing and removing them.
Image
Also called container image, OCI image
- What
- A read-only bundle of filesystem layers plus metadata (default command, environment, exposed ports, user). Images follow the OCI format, so any compliant runtime can run them.
- Why
- An image is the unit you build once and ship everywhere: a laptop, CI and production all run the same bytes.
Watch out
Treating an image as a VM snapshot and changing a running container by hand; rebuild the image instead so the change is recorded.
Container
- What
- A running (or stopped) instance of an image: an isolated process with a thin writable layer on top of the image. Linux namespaces isolate it and cgroups cap its resources.
- Why
- Many containers can run from one image, each with its own state, network identity and limits, and each starts in about the time its process takes to boot.
Watch out
Storing data in the writable layer: it disappears when the container is removed.
Docker Engine, containerd and runc
Also called dockerd, OCI runtime
- What
- The
dockerCLI talks to thedockerddaemon, which hands containers to containerd, which starts them with an OCI runtime such as runc. Docker Desktop runs all of this in a small Linux VM on macOS and Windows. - Why
- Knowing the layers explains why Kubernetes can run your images without Docker installed, and why file mounts are slower on Docker Desktop.
Watch out
Adding a user to the docker group casually: access to the daemon socket is effectively root on the host.
docker run
- What
- Creates and starts a container from an image. Common flags:
-ddetached,--name,-ppublish a port,-eset a variable,-vmount storage,--rmdelete on exit,-itinteractive terminal. - Why
- It is the single command behind most local experiments, one-off tools and quick checks of an image.
docker run -d --name web -p 8080:80 nginx:1.27
docker run --rm -it python:3.13-slim python
docker run --rm -v "$PWD":/work -w /work \
node:22-slim npm testWatch out
Forgetting --rm on throwaway runs, which leaves hundreds of stopped containers behind.
Publishing ports
Also called -p, port mapping
- What
-p host:containerforwards a host port to a port inside the container.-p 127.0.0.1:5432:5432binds only to localhost.EXPOSEin a Dockerfile documents a port but publishes nothing.- Why
- Without publishing, a container is reachable only from other containers on its network.
Watch out
Publishing a database with -p 5432:5432 on a server: it binds every interface, and Docker's iptables rules can bypass host firewalls such as ufw.
Container lifecycle commands
Also called docker ps, docker stop
- What
- A container moves through created, running, paused, exited and removed.
docker stopsends SIGTERM then SIGKILL after a grace period;docker killsends SIGKILL at once. - Why
- Knowing which state a container is in, and how it stopped, is the first step in every "why is it not running" question.
docker ps -a
docker stop web
docker start web
docker rm -f web
docker ps -a --filter status=exited