You write a Dockerfile, and later you reach your app on a port. Which parts of Docker do the work between these two steps, and in what order? And where is the shipping-container picture wrong?
The whole port in one picture
Follow one container from the Dockerfile to the street:
- The Dockerfile is the packing list.
docker buildmakes the image, a stack of read-only layers (part 2). - The image goes to a registry, the depot. It gets a label you choose, the tag. It also gets a fingerprint from its contents, the digest (part 3).
- On the server, the
dockercommand sends a request through the docker socket, the hatch, todockerd.dockerdis the crane operator, and it runs as root. containerd keeps the records. runc creates the container, sets up its namespaces and its cgroup limit, and exits (parts 1 and 4). - Inside is one ordinary process. Its data lives in a volume, the trailer. The street reaches the process only through a published port, the gate (part 5).
- Compose writes all of that as one plan (parts 6 and 8). A good Dockerfile keeps the image small and runs its program without root (part 7).
One container, every part
You can see most of that picture in a single running container. Start one with a memory limit, a volume and a port, then ask Docker about it:
docker run -d --name one --memory 256m -v notes:/data -p 127.0.0.1:8080:80 nginx:alpine
docker inspect -f 'tag {{.Config.Image}}
image {{.Image}}
pid {{.State.Pid}}
memory {{.HostConfig.Memory}}
volume {{range .Mounts}}{{.Name}} -> {{.Destination}}{{end}}
port {{range $p, $b := .NetworkSettings.Ports}}{{(index $b 0).HostIp}}:{{(index $b 0).HostPort}} -> {{$p}}{{end}}' one eb064846ae095068ea3d90e044c33fd1354e14b666845126794f192428171d43 tag nginx:alpine image sha256:892bb2152464f0c5d8972593a5f5afaf8054552580b699d95f8ff4af08c2d33d pid 2226 memory 268435456 volume notes -> /data port 127.0.0.1:8080 -> 80/tcp
Here is what each marked part does:
- 1
-druns the container in the background and prints its ID. - 2
-v notes:/datamounts the volumenotesinside the container at the folder/data. - 3
-p 127.0.0.1:8080:80publishes container port 80 on port 8080 of the host. Only the host itself can reach it, because of the address127.0.0.1. - 4
docker inspect -f '...'prints only the fields named in the quoted template. Each{{...}}is replaced by a value, andrangerepeats its part for every item in a list.
The figure below names each line. Two lines need an explanation. With Docker Desktop’s containerd image store, image is the digest from part 3. Other setups show an image ID, a different hash of the same image. pid is the process ID as the host sees it, outside the container’s namespaces. On Docker Desktop, the host is its Linux VM. memory is the 256 MiB limit, in bytes.
Where it breaks: one floor
The picture says each container is sealed from the container next to it. A virtual machine is sealed in this way, because it boots its own kernel. A container does not boot its own kernel. Every container on a machine runs on the same kernel, the host’s. Ask two very different images which kernel they run on:
alpine and debian are different distributions. Will they report different kernels?
docker run --rm alpine uname -r
docker run --rm debian:bookworm-slim uname -r 6.12.54-linuxkit 6.12.54-linuxkit
Here is what each marked part does:
- 1
uname -rprints the version of the kernel that runs the command. - 2
debian:bookworm-slimis an image name, then a colon, then a tag. The tag selects one version of the image.
Both print the same kernel, because the distribution in an image is only files. On Docker Desktop, the kernel belongs to its Linux VM, here linuxkit. On a Linux server, it is the server’s own kernel. A bug in that kernel is a bug under every container at once. Many container escapes are bugs in the kernel or in runc. The others use openings that we left ourselves, as the next section shows.
Where it breaks: separated, not protected
Namespaces and cgroups limit what a container can see and how much it can take. But they do not stop every way out. There are three common ways out of a container:
- A container that runs as root with a host folder mounted can write to the host as root (part 5).
--privilegedgives the container every capability and the host’s devices. It also disables seccomp and AppArmor, the kernel filters that block risky system calls and file access.- Anyone who can reach the docker socket can start a container with the whole host mounted inside (part 4).
What you give a container is also visible. A password passed as an environment variable is written into the container’s settings:
docker run -d --name leak -e DB_PASSWORD=hunter2 alpine sleep 60
docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' leak ae5d279e379ebdcfc1d0efa1371b883b36a01b1dcd84f2f0444337e11be142b0 DB_PASSWORD=hunter2 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Here is what each marked part does:
- 1
-e DB_PASSWORD=hunter2sets an environment variable inside the container. - 2
sleep 60keeps the container running for 60 seconds, so that we can inspect it. - 3
{{range .Config.Env}}{{println .}}{{end}}repeats for every environment variable and prints each one on its own line.
Anyone who can talk to the daemon can read the password in plain text. So part 6’s POSTGRES_PASSWORD: example is fine on a laptop but wrong on a server. Part 8 moved the password into a secret file.
Clean up with docker rm -f one leak and docker volume rm notes.
Key points
- A container is one ordinary process. Namespaces limit what it sees, and a cgroup limits what it uses. A root daemon starts it through containerd and runc.
- Its image is a stack of read-only layers, named by a tag you can move and a digest you cannot.
- Its data survives the container only in a volume or a host folder. The outside reaches it only through a published port, and the port’s address decides who can reach it.
- Every container shares the host’s kernel, so harden each container yourself.
The next series follows a request from the street to your app: DNS, TCP, TLS and the proxy in front of it.