What actually happens when you type docker run

docker run only sends a message. We look at who receives it, who asks the kernel for the blindfold and the limit, and why anyone who can send that message is root.

Part 4 of 9 · Containers4 min read
rundocker: only a clientthe hatchdocker.sockdockerd: rootcontainerdrunc starts itand leavesshimstays as parentreach the hatch = be rootthe docker group is root tooKubernetes skips Docker:it talks to containerd

You type docker run -d alpine sleep 300 and a container starts. Which process started it? It was not docker. That command finished in under a second, and the container is still running.

An order slip for the crane

In part 1 Bub opened a box and found one blindfolded worker with a limit. In part 3 the box arrived from the depot. But Bub never lifts a box himself. He writes an order slip, docker run -d alpine sleep 300, pushes it through the hatch in the crane cab and leaves.

The docker command is Bub writing the slip. It is only a client. It turns your command into a request and sends the request on. The crane operator is dockerd, a daemon that runs as root. Creating namespaces, mounting filesystems and writing firewall rules all need root.

The operator does not open boxes either. He sends the order along a chain:

  • containerd is the yard office. It keeps the records of which images exist and which containers run.
  • containerd-shim-runc-v2, the shim, is the foreman who stays with one box.
  • runc is the worker who opens the box. It asks the kernel for the namespaces and the cgroup, starts your program inside them, and exits.
dockerthe order slip (client)dockerdthe crane operatorcontainerdthe yard officecontainerd-shim-runc-v2the foremansleep 300the hatch (docker.sock)stays as parentruncthe workerexits after startstartsstartsroot
docker sends a request; dockerd, containerd and runc do the work as root. runc leaves once the process runs; the shim stays.

Look at the chain from the host

Start a container, then look at it from the host’s side. --pid=host removes the helper container’s blindfold, so its ps sees every process. We keep only the lines that matter:

After the container starts, is runc still running?

Shell
docker run -d --name web alpine sleep 300 
docker run --rm --pid=host alpine ps -o pid,ppid,args \ 
| grep -E "PID|dockerd|bin/containerd |runc |$(docker inspect -f '{{.Id}}' web)|sleep 300" \ 
| cut -c1-72 
629325b4bc3c9550a00f50740f2dc4977a1ecf1201d7d2df44429fbc3dd21f95
PID   PPID  COMMAND
287   153 /usr/bin/containerd --config /etc/containerd/containerd.toml
305   153 /usr/local/bin/dockerd --config-file /run/config/docker/daem
2009     1 /usr/bin/containerd-shim-runc-v2 -namespace moby -id 629325b
2034  2009 sleep 300

Here is what each marked part does:

  1. 1docker run -d --name web starts the container in the background and gives it the name web. Its program, sleep 300, only waits for 300 seconds.
  2. 2--pid=host lets the container see the processes of the host, not only its own. ps -o pid,ppid,args lists them with their ID, parent ID and command.
  3. 3grep -E keeps the lines that match one of the words separated by |. $(...) is replaced by the output of the command inside it, here the full ID of web.
  4. 4cut -c1-72 keeps the first 72 characters of each line, so that long lines fit on the page.

There is no runc process. runc set up the box, started sleep, and exited. The parent of sleep (PPID 2009) is the shim. The shim’s own parent is PID 1, not containerd. The shim exists so that containerd can restart for an upgrade or after a crash, while the container keeps running under its foreman. dockerd is stricter. By default it stops its containers when it shuts down. The containers survive a dockerd restart only with "live-restore": true in daemon.json. On Docker Desktop these processes run inside its Linux VM. For that reason the paths look like the paths on a Linux server.

Remove the container afterwards with docker rm -f web.

The hatch

The slip goes through a hatch in the cab: a unix socket. Anyone standing at the hatch can push a slip through it. On a Linux server the socket is /var/run/docker.sock. Docker Desktop on a Mac puts it at ~/.docker/run/docker.sock.

The CLI sends plain HTTP requests over that socket. Any program that can open the file can ask the same questions. Ask with curl, then with docker:

Shell
curl -s --unix-socket ~/.docker/run/docker.sock http://localhost/version \ 
| jq -r '.Components[] | "\(.Name) \(.Version)"' 
docker version -f '{{range .Server.Components}}{{.Name}} {{.Version}}{{"\n"}}{{end}}' 
Engine 29.1.2
containerd v2.2.0
runc 1.3.4
docker-init 0.19.0
Engine 29.1.2
containerd v2.2.0
runc 1.3.4
docker-init 0.19.0

Here is what each marked part does:

  1. 1--unix-socket makes curl talk to a socket file on this computer, not to the network. The option -s hides the progress bar.
  2. 2jq -r reads the JSON reply. .Components[] goes through each item of the list, and -r prints plain text without quotes.
  3. 3docker version -f prints the version with a template. The word range repeats the template for each server component.

Both commands print the same answer, because both send the same slip through the same hatch. The reply also names the chain we just saw: the engine (dockerd), containerd and runc. On a Linux server, replace the socket path with /var/run/docker.sock.

docker versionEngine 29.1.2containerd v2.2.0runc 1.3.4curl --unix-socketEngine 29.1.2containerd v2.2.0runc 1.3.4same answerthe hatchdocker.sockGET /versionGET /versionsame slipdockerd
docker and curl push the same slip through the same hatch and get the same answer. The daemon only checks who can reach the hatch, not what the slip asks.

Whoever can reach the hatch is root

The crane operator runs as root and does what the slip says. He does not ask who wrote it. By default the only lock is on the hatch, which is the socket file itself. On Linux, the docker group is allowed to open the socket.

So we ask root to mount the host’s whole filesystem into a container. We mount it read-only (:ro) and only read one file:

Shell
docker run --rm alpine head -1 /etc/os-release 
docker run --rm -v /:/host:ro alpine cat /host/etc/os-release 
NAME="Alpine Linux"
PRETTY_NAME="Docker Desktop"

Here is what each marked part does:

  1. 1head -1 prints only the first line of the file.
  2. 2-v /:/host:ro shows the root folder of the host inside the container as /host. The part :ro makes it read-only.

The first line comes from inside the box, and it says Alpine. The second line comes from the host’s own /etc. On a Mac the host is Docker Desktop’s VM. On a Linux server it would be the server’s own distribution. We used no sudo and no password. Without :ro, the same command could write anywhere on the host, as root.

sudoersroot%adminnot in sudoersdocker groupuserdocker.sockthe hatchorder slipmount / into a containerdockerdroothost files /hostthe user reads + writes

A user in the docker group. The user is not in sudoers and has no root password.

Docker’s own install guide says this directly. The docker group grants root-level privileges. But the docker group never appears in the place where an auditor looks for root, the sudoers file.

Docker is optional

Look at the chain again. The part that runs containers is containerd and runc. The crane operator, dockerd, adds the parts in front of them: the API, image builds, networks, volumes.

Kubernetes skips the crane operator. The kubelet talks to a runtime directly through the CRI. On most nodes that runtime is containerd. CRI-O is another runtime, and cri-dockerd adds Docker back as an adapter. Since Kubernetes 1.24 the kubelet has no built-in Docker support at all. A Kubernetes node can run all its containers with no Docker installed. The images are the same ones that Docker builds. Any runtime that reads the image format can run them.

Docker hostKubernetes nodedockerdockerdcontainerdruncyour processkubeletcontainerdruncyour processCRIno Docker installed
A Docker host and a Kubernetes node. Both end in containerd and runc; only one has dockerd in front.

Key points

  • docker is only a client. It pushes an HTTP request, the order slip, through a unix socket, the hatch, to dockerd, which runs as root.
  • dockerd gives the work to containerd. runc asks the kernel for the namespaces and cgroup, starts your process and exits. The shim stays as its parent.
  • Anyone who can reach the hatch can ask root for anything, so the docker group is root without sudo.
  • Docker is a toolkit around containerd. Kubernetes usually talks to containerd directly, with no Docker on the node.

Next, in part 5: what stays after the box is gone, volumes and published ports.

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