You type docker run, and a fresh Linux system answers you before you release the Enter key. A virtual machine takes far longer to boot. So what is a container, if it is not a small computer?
Containers became popular because of a familiar problem. Your service works on your laptop and breaks on the server, with the same code. Everything around the code is different: the Python version, or a library you forgot you installed. The image ships that environment with the code, and part 2 covers the image. Here we open a container and look at what runs inside.
A whole ship for one crate
Think of a shipping port. Before the standard box, every cargo had its own shape: sacks, barrels, crates. Every ship and every crane handled them differently. Bub, our dock worker, spent half his day planning how to lift the next strange object.
The first fix for software was the virtual machine. You simulate a computer, install a full operating system inside it, put your app on top and ship all of it. In the port, a VM is a whole ship, with its own engine and crew, built to carry one crate.
A VM works, but it is heavy. It uses gigabytes on disk. A full Linux VM takes tens of seconds to boot on a typical laptop, and every app needs one.
Look at what the VM spends its effort on. It simulates hardware, and it runs a second kernel.
Your program never needed a second kernel. It needed its own view of files, processes and network, separate from the host’s view. Linux can give exactly that view to an ordinary process.
The blindfold
Bub opens a standard box. He expects a small computer inside, but he finds one worker, wearing a blindfold.
The blindfold is a namespace. The process inside is an ordinary process on the host’s kernel. The namespace shows it only its own part of the port.
Start a container and ask it for its process list.
What PID does the process inside see for itself?
docker run --rm alpine ps PID USER TIME COMMAND 1 root 0:00 ps
Here is what each marked part does:
- 1
--rmremoves the container when its command ends, so nothing is left behind. - 2
alpine psnames the imagealpineand the programpsto run inside it.pslists the processes it can see.
We asked for every process on the machine and got one: ps itself. It believes it is PID 1. PID 1 is the first process the machine starts, and it is normally init.
Now keep one running and look at it from both sides.
docker run -d --name blind alpine sleep 300
docker exec blind ps
docker top blind -o pid,args 7e5c3c527f93ec23d8ead3360f5a5e0fb194301cee2a607419cbf2ed7f03feef PID USER TIME COMMAND 1 root 0:00 sleep 300 6 root 0:00 ps PID COMMAND 3197 sleep 300
Here is what each marked part does:
- 1
-d --name blindruns the container in the background and gives it the nameblind.sleep 300keeps it alive for 300 seconds. - 2
docker exec blind psruns one more program,ps, inside the container that is already running. - 3
docker topshows the same container from the host side.-o pid,argspicks the columns: the process ID and the command line.
Inside, sleep is PID 1. The host sees the same process as PID 3197, one of hundreds. Nothing was copied or moved. We looked at one process through two windows. Your container ID and PIDs will differ, here and in the rest of the series. Remove the container afterwards with docker rm -f blind.
There is a namespace for each thing a process can see: the process list, the filesystem, the network, the hostname and the users. Docker gives a container a new namespace for most of them, but not for users. Compare the container with the host’s PID 1. --pid=host removes the blindfold so we can see that process, and --privileged lets us read its namespaces:
docker run --rm alpine ls -l /proc/self/ns/pid /proc/self/ns/user
docker run --rm --privileged --pid=host alpine ls -l /proc/1/ns/pid /proc/1/ns/user lrwxrwxrwx 1 root root 0 Oct 17 14:08 /proc/self/ns/pid -> pid:[4026534045] lrwxrwxrwx 1 root root 0 Oct 17 14:08 /proc/self/ns/user -> user:[4026531837] lrwxrwxrwx 1 root root 0 Oct 17 14:08 /proc/1/ns/pid -> pid:[4026531836] lrwxrwxrwx 1 root root 0 Oct 17 14:08 /proc/1/ns/user -> user:[4026531837]
Here is what each marked part does:
- 1
/proc/self/ns/pidis a file the kernel makes for the process that reads it. It names the pid namespace that process belongs to. - 2
--privilegedgives the container extra rights, so it may read the namespace files of another process. - 3
--pid=hostmakes the container use the pid namespace of the host, so/proc/1is the first process of the host.
The pid namespaces differ. The user namespace is the same one the host uses. So root inside a container has the same user ID 0 as the host’s root. The exception is a daemon that runs with userns-remap or in rootless mode. Docker still removes most of root’s capabilities and applies a seccomp profile. On Linux hosts that have AppArmor, Docker applies AppArmor too. So by default, root in a container does not have full root power. The shared user ID matters when a process escapes the container. Then the process is the host’s root.
The ration
The blindfold limits what the worker can see. It does not limit how much he takes. A blindfolded process can still take every byte of memory on the host.
So the worker also gets a ration card, a limit on what he may take. The ration card is a cgroup. Give a container 64 MB and ask it for more. tail /dev/zero reads an endless stream with no line breaks. So tail keeps buffering until no memory is left.
docker run --rm --memory 64m --memory-swap 64m alpine tail /dev/zero
echo "exit code: $?" exit code: 137
Here is what each marked part does:
- 1
--memory 64mlimits the container to 64 megabytes of RAM.--memory-swap 64msets RAM plus swap to the same size, so it cannot use swap space as extra memory. - 2
$?is the exit code of the last command. Here that is the exit code ofdocker run, which returns the exit code of the container.
There is no error message, only an exit code. 137 is 128 plus 9, so the process was killed with signal 9, SIGKILL. The kernel did not ask it to stop first, and it had no chance to clean up. In production, this is the container that restarts every few minutes with nothing in its log.
The process keeps asking the kernel for memory. The cgroup counts every page.
The limit is an ordinary file the kernel keeps for the group. The container can read its own limit at this path on cgroup v2. Current Docker Desktop and most current Linux distributions use cgroup v2:
docker run --rm --memory 64m alpine cat /sys/fs/cgroup/memory.max 67108864
Here is what each marked part does:
- 1
/sys/fs/cgroupis where the kernel shows the cgroup of the process that reads it. Inside the container, this is the container's own cgroup. - 2
memory.maxis the file that holds the memory limit of that cgroup, in bytes.
That number is 64 × 1024 × 1024 bytes. runc wrote the number at Docker’s request, and the kernel enforces it.
One kernel, shared
Together, the namespace and the cgroup are everything in the box. A container is a process on the host’s kernel, with a restricted view and a limit. There is no simulated hardware and no second operating system to boot.
Ask a container which kernel it runs on.
docker run --rm alpine uname -r 6.12.54-linuxkit
Here is what each marked part does:
- 1
unameprints facts about the system it runs on. - 2
-rselects the kernel release, the version of the running kernel.
We asked from inside the container and got the host’s kernel, because there is no other kernel. On a Mac or Windows, Docker Desktop runs a small Linux VM. So you see that VM’s kernel, here linuxkit. On a Linux server, you see the server’s own version.
A container starts fast because nothing boots. The daemon, containerd and runc set up the mounts, the network, the namespaces and the cgroup. Then the kernel runs your program. On Docker Desktop on a laptop, docker run --rm alpine true takes a few hundred milliseconds end to end. Nearly all of that time goes to the setup work, and the exact number depends on your machine.
Sharing the kernel has a cost. Linux containers need a Linux kernel, so Docker Desktop runs a hidden VM on a Mac. Also, a bug in the kernel affects every container, because every container uses the same kernel.
Key points
- A container is an ordinary process on the host, not a small computer.
- A namespace is the blindfold: it decides what the process can see.
- A cgroup is the ration: it decides how much CPU and memory the process can take.
- There is one shared kernel, so nothing boots. A container starts in a few hundred milliseconds, not the tens of seconds a full VM needs.
Next, in part 2: what the box is packed with, which is the image and its layers.