Lesson 1 of 9 · 22m
Containers Are Not VMs: Namespaces and cgroups
A container is a lie the kernel tells a process — namespaces decide what it can see, cgroups decide what it can use. Learn both and the magic disappears.
The first thing to unlearn: there is no such thing as a container in the Linux kernel. No container object, no container syscall. A container is a regular process that has been boxed in by two independent kernel features — namespaces (what it can see) and cgroups (what it can use). Docker is mostly bookkeeping around those two primitives. Once you believe that, every weird container behavior becomes predictable.
Namespaces: the illusion of a private machine
Each namespace type virtualizes one slice of the system. docker run typically creates six or seven:
mnt – private mount table (that's how the container has its own /)
pid – private PID tree; the container's init is PID 1 *inside*
net – private interfaces, routing table, iptables
uts – private hostname (that's why hostname inside ≠ hostname outside)
ipc – private SysV IPC / shared memory segments
user – UID mapping (the rootless story — later lesson)
cgroup – virtualized view of the cgroup hierarchy itselfYou can watch this happen without Docker at all:
# run a shell in fresh mount+pid+uts namespaces
sudo unshare --mount --pid --uts --fork --mount-proc bash
hostname prisoner # host hostname is untouched
ps aux # only two processes: your shell is PID 1That shell is a container in every sense that matters — it just has a terrible image, no cgroups, and no network setup.
cgroups: the resource meter
Namespaces lie about visibility; cgroups enforce physics. Every container joins a cgroup that caps CPU, memory, pids, and block IO:
docker run -d --memory=256m --cpus=0.5 --pids-limit=100 nginx
# the kernel now enforces it — look at the actual knobs:
cat /sys/fs/cgroup/system.slice/docker-<id>.scope/memory.max
# 268435456The failure mode that bites everyone: the OOM killer is a kernel actor, not a Docker one. When a container exceeds memory.max, the kernel kills a process inside it — usually the biggest, not the guilty one. Docker then reports exit code 137 (SIGKILL). If you've ever stared at a "randomly dying" Java service, the classic cause is a JVM that sizes its heap off the host's RAM because pre-1.9 JVMs can't see cgroup limits.
PID 1 consequences
Because your entrypoint becomes PID 1 of a private PID namespace, it inherits PID 1's special duties: reaping zombies and receiving only the signals it explicitly handles. A shell script entrypoint that doesn't exec the real binary means docker stop's SIGTERM goes nowhere — Docker waits 10 seconds and SIGKILLs it. That's why every graceful-shutdown mystery starts with exec.
Field notes
exit code 137= cgroup OOM kill until proven otherwise. Checkdocker inspect <id> --format '{{.State.OOMKilled}}'.- Two containers can share namespaces — that's exactly what a Kubernetes pod is (
--pid=container:x, shared net namespace). - Containers don't "escape to the host" by magic; they escape because a namespace wasn't created (e.g.
--pid=host) or something dangerous was mounted in. - Processes inside containers run as real host PIDs —
ps auxon the host shows everything. Nothing is hidden from root on the host.
Was this lesson helpful?