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:
containerdis 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.runcis the worker who opens the box. It asks the kernel for the namespaces and the cgroup, starts your program inside them, and exits.
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?
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
docker run -d --name webstarts the container in the background and gives it the nameweb. Its program,sleep 300, only waits for 300 seconds. - 2
--pid=hostlets the container see the processes of the host, not only its own.ps -o pid,ppid,argslists them with their ID, parent ID and command. - 3
grep -Ekeeps the lines that match one of the words separated by|.$(...)is replaced by the output of the command inside it, here the full ID ofweb. - 4
cut -c1-72keeps 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:
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
--unix-socketmakescurltalk to a socket file on this computer, not to the network. The option-shides the progress bar. - 2
jq -rreads the JSON reply..Components[]goes through each item of the list, and-rprints plain text without quotes. - 3
docker version -fprints the version with a template. The wordrangerepeats 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.
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:
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
head -1prints only the first line of the file. - 2
-v /:/host:roshows the root folder of the host inside the container as/host. The part:romakes 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.
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.
Key points
dockeris only a client. It pushes an HTTP request, the order slip, through a unix socket, the hatch, todockerd, which runs as root.dockerdgives 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
dockergroup is root withoutsudo. - 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.