What a container really is
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.
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.
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.)
- 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.
- 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.
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.
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.
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.