Where the shipping container picture breaks

The whole port in one picture, from the packing list to the gate. The picture stops being true in two places. Every container shares the host's kernel, and every container needs hardening of its own.

Part 9 of 9 · Containers4 min read
blindfoldsees itselfrationcgrouplayersimagetag + digestnamestrailervolumegateportthe whole port, in one pictureone kernel under every boxwalls are curtains:separated, not protecteda hole in the kernel reaches every boxharden each box

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 build makes 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 docker command sends a request through the docker socket, the hatch, to dockerd. dockerd is 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).
Dockerfileparts 2, 7imagepart 2registrypart 3buildpushcompose up plans this (parts 6, 8)dockerpart 4dockerd (root)part 4containerdpart 4runcpart 4pullyour processnamespaces + cgrouppart 1volumepart 5portpart 5
From the Dockerfile to a process with its volume and its port. Each step is one part of the series.

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:

Shell
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. 1-d runs the container in the background and prints its ID.
  2. 2-v notes:/data mounts the volume notes inside the container at the folder /data.
  3. 3-p 127.0.0.1:8080:80 publishes container port 80 on port 8080 of the host. Only the host itself can reach it, because of the address 127.0.0.1.
  4. 4docker inspect -f '...' prints only the fields named in the quoted template. Each {{...}} is replaced by a value, and range repeats 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.

tag nginx:alpineimage sha256:892bb215…pid 2226memory 268435456volume notes -> /dataport 127.0.0.1:8080 -> 80/tcpthe label (part 3)the fingerprint (part 3)the process on the host (part 1)the ration (part 1)the trailer (part 5)the gate (part 5)
One docker inspect, six lines from three parts of the series: the tag, the digest, the process, the memory limit, the volume, the port.

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?

Shell
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. 1uname -r prints the version of the kernel that runs the command.
  2. 2debian:bookworm-slim is 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.

virtual machinescontainerskernel kernel kernel one kernel each on its own kernelevery box on one kernel
Virtual machines: each one on its own kernel. Containers: all of them on the host's one kernel.

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).
  • --privileged gives 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:

Shell
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. 1-e DB_PASSWORD=hunter2 sets an environment variable inside the container.
  2. 2sleep 60 keeps the container running for 60 seconds, so that we can inspect it.
  3. 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.

-v /:/hosthost --privilegedhostdocker.sockhost
Three ways out of a container: root with a host mount, --privileged, and access to the docker socket.

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.

New posts by email

One email when a post ships, about twice a month. Nothing else, and one click to leave.

Latest: Finding where a request breaks · RSS