Skip to content

What a container really is

Working knowledgeUnder the hood

A container feels like a small machine of its own, yet it starts instantly and shows up in the host’s ps. Here is what the kernel really does: one ordinary process, given namespaces and a cgroup.

01 Faster than any machine could boot

A container starts in well under a second. Inside, ps lists only a couple of processes, the first one is PID 1, and the hostname is a random string. It feels like a tiny separate computer. Yet run ps on the host and the same program is right there in the list, next to everything else.

Shell
$ docker run -d --name web --memory 512m --cpus 1 nginx
$ docker top web -o pid,ppid,args
PID PPID COMMAND
4321 4310 nginx: master process nginx -g daemon off;
4350 4321 nginx: worker process

docker top shows host PIDs. Both answers are true at once, and explaining why is explaining what a container is.

02 The wrong picture: a small virtual machine

A virtual machine emulates hardware. It boots its own kernel, which manages its own memory and devices, on top of a hypervisor. A container has no kernel of its own. It is a normal Linux process that the host kernel shows a restricted, relabelled view of the system.

Virtual machineOwn kernelBoots an operating system, reserves its memory up front, and can run a different OS from the host.
ContainerHost’s kernelStarts like any process and uses memory as it goes, but can only run programs for the host’s kernel.

03 What really happens on docker run

Two kernel features do nearly all the work. Namespaces give a process its own copy of one kind of global name: process IDs, mounts, network interfaces, hostname. Cgroups (control groups) cap and count the CPU, memory and I/O a group of processes uses. Docker hands your request to containerd, which asks runc to set these up.

In reality runc asks for the namespaces together in one system call. Here they are taken one at a time so you can see what each one changes. (Docker also gives the process new IPC and cgroup namespaces, left out to keep the picture small.)

What docker run does to one process
Step 1 of 8
PID it sees 4321
Process 4321 · what the kernel gives it
pid ns→hostmount ns→hostnet ns→hostuts ns→hostcgroup→host, no limitroot dir→/ of the hostprogram→runc init
  • just changed
  • as before

docker run asks the runtime (runc) to start your container. It begins as an ordinary child process, PID 4321, sharing everything with the host.

4321 PID on the host
The same nginx process, listed from both sides
ps on the host
1 systemd812 dockerd4310 containerd-shim4321 nginx: master4350 nginx: worker
ps inside the container
1 nginx: master30 nginx: worker
  • the master process
  • its worker

The host sees every process, including the container’s. The container sees only its own, numbered from 1.

04 See it yourself on Linux

You do not need Docker to make a namespace. unshare (from util-linux) starts a program in new ones. With a new PID namespace and a fresh /proc, the shell becomes PID 1 and sees nothing else.

Shell
$ sudo unshare --pid --fork --mount-proc bash
# ps -o pid,comm
PID COMMAND
1 bash
2 ps

For a running container, find its host PID and look in /proc. Each namespace is a link with an ID; two processes with the same ID share that namespace. NSpid lists the process’s number in each PID namespace it belongs to, host first.

Shell
$ docker inspect -f '{{.State.Pid}}' web
4321
$ sudo readlink /proc/1/ns/pid /proc/4321/ns/pid
pid:[4026531836]
pid:[4026532965]
$ grep NSpid /proc/4321/status
NSpid: 4321 1

The memory limit is just a file in the cgroup filesystem. On a cgroup v2 host, the container sees its own cgroup at /sys/fs/cgroup, and 512m reads back in bytes.

Shell
$ docker exec web cat /sys/fs/cgroup/memory.max
536870912

On a Mac or Windows, Docker Desktop does use a virtual machine: one small Linux VM that all your containers run inside. The containers are still processes sharing that one Linux kernel, which is why uname -r inside any of them shows the VM’s kernel.

05 Where the illusion leaks

uname -rOne kernel for everyoneEvery container uses the host’s kernel. You cannot run a different kernel version in one, and a kernel bug can let a process break out. Namespaces are isolation, not a security boundary as strong as a VM.
Exited (137)Hitting the memory limitGo past the cgroup’s memory limit and the kernel’s OOM killer sends SIGKILL. The container exits with 137 (128 + 9), often with no log line from your program.
nproc, /proc/meminfoTools that read the host’s numbersSome files under /proc are not namespaced, so free shows the host’s RAM, not your limit. Runtimes that size themselves from these, such as older JVMs before container support arrived in JDK 10 and 8u191, pick sizes far too big.
docker stop # waits 10sPID 1 ignores signalsThe kernel does not apply default signal actions to PID 1 of a namespace. If your program has no SIGTERM handler, docker stop waits out its timeout (10 seconds by default) and then kills it.
USER rootRoot inside is root outsideWithout a user namespace, which Docker does not turn on by default, UID 0 in the container is UID 0 on the host. Run as a normal user unless you need root.

06 Check yourself

Question

The host runs Linux kernel 6.8. You start a container from an image built on a distro that ships kernel 5.15. What does uname -r print inside the container?

Question

Which feature stops a container from using all of the host’s memory?

Question

Inside a container, nginx is PID 1. On the host, is there a process with PID 1 that is also nginx?

07 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?