How Containers Actually Work: Namespaces, Cgroups, and Images

Matt George

Fractional CTO · Founder of Injuria.ai

•7 min read•

Containers changed how software gets shipped, and Docker made them easy enough that millions of developers use them without ever asking what is underneath. The common mental model is that a container is a lightweight virtual machine, a small computer running inside your computer. That model is wrong in a way that matters. A container is not a separate machine at all. It is a normal process running directly on the host's Linux kernel, fenced off by a few features that make it believe it has the machine to itself.

Not a virtual machine

A virtual machine runs a full guest operating system on top of virtualized hardware. There is a hypervisor, a complete second kernel, virtual disks, and real overhead. That isolation is strong, which is why you can run Windows inside Linux this way, but it is heavy and slow to start.

A container shares the host's kernel. There is no guest operating system and no virtualized hardware. When you run a process in a container, the host kernel is still the one scheduling it, handling its system calls, and managing its memory. The container is just that process, plus a set of restrictions on what it can see and use. This is why containers start in milliseconds and why you can run dozens on a laptop. It is also why a Linux container needs a Linux kernel, which is what the Linux virtual machine inside Docker Desktop quietly provides on a Mac or Windows box.

Namespaces: what the process can see

The first half of the disguise is Linux namespaces.[2] A namespace wraps a global system resource so the processes inside it see their own private version of it. A container uses several at once. The PID namespace gives it its own process tree, where the first process becomes PID 1 and cannot see the host's other processes.[7] The mount namespace gives it its own view of the filesystem. The network namespace gives it its own interfaces, routing table, and ports, which is why two containers can both bind port 8080 without a fight. The UTS namespace lets it have its own hostname (and NIS domain name). The user namespace can map root inside the container to an unprivileged user on the host, so root in the container is not root on the machine.

You can see this with a plain kernel command, no Docker required, but there is a subtlety worth getting right.

# Give the shell its own PID and mount namespaces, and its own /proc
unshare --pid --mount --fork --mount-proc bash
ps aux   # now the process tree really is nearly empty

The --mount-proc part matters. ps reads from /proc, so without mounting a fresh /proc inside the new namespace you would still see the host's processes and wonder why the isolation "did not work." That one flag is a good reminder that a namespace only isolates what you actually put inside it.

Cgroups: what the process can use

Namespaces control what a process sees. They do nothing to control how much it consumes. That job belongs to control groups, or cgroups, the second half of the disguise.[3] A cgroup measures and caps resource usage for a group of processes. Tell a cgroup that this container gets at most half a gigabyte of memory and the equivalent of one and a half CPUs, and the kernel enforces it, calling in the out-of-memory killer if a process exceeds its memory cap. When you pass --memory or --cpus to docker run, you are configuring a cgroup. Isolation of view comes from namespaces, isolation of resources comes from cgroups, and both have been ordinary kernel features for years.

The part that is actually security

People say "containers are not a security boundary," and the reason sits in two features the basic story leaves out. The first is Linux capabilities, which slice the old all-or-nothing root privilege into dozens of separate powers like "can change network config" or "can load kernel modules," so a container can run as root yet hold almost none of root's real abilities.[4] The second is syscall filtering: Docker ships a default seccomp profile that blocks dozens of dangerous system calls outright. Seccomp is a separate kernel feature that uses BPF to filter system calls; it is not backed by AppArmor or SELinux, which are separate Mandatory Access Control (MAC) systems.[8] Namespaces and cgroups create the illusion of a private machine, but it is capabilities and seccomp that keep a process inside that illusion from reaching out and compromising the host. When people say container isolation is weaker than a VM's, this is the gap they mean: everything is still one shared kernel, and the kernel's entire system-call surface is the attack surface.

Images: layers stacked with a union filesystem

The last piece is the filesystem the container runs on. A container image is not a single blob. It is a stack of read-only layers, each a set of filesystem changes, presented as one directory tree by a union filesystem, in practice OverlayFS.[5] OverlayFS is worth seeing concretely: the read-only image layers are the lowerdir, a writable directory the runtime adds on top is the upperdir, and the unified view the process actually sees is the merged mount.

When you write a Dockerfile, only instructions that change the filesystem create a layer, and it is worth being precise, because a common belief is wrong.

FROM node:20          # base layers
WORKDIR /app          # creates a filesystem layer if the specified directory does not already exist
COPY package.json .   # a layer
RUN npm install       # a layer
COPY . .              # a layer

ENV and similar instructions only set configuration metadata and add no filesystem layer, while RUN, COPY, and ADD do. WORKDIR creates a filesystem layer if the specified directory does not already exist. Layers are content-addressed and cached, so if your package.json has not changed, the expensive npm install layer is reused, and a layer shared by ten images is stored and downloaded once. When the container runs, the runtime (not the kernel on its own) adds that thin writable upperdir on top, so changes the process makes do not alter the image.

The runtime underneath Docker

One more layer the mental model usually skips: Docker is not the thing that actually creates the namespaces and cgroups. It hands a standard specification to a low-level runtime, runc, built to the Open Container Initiative standard, which does the real work of setting up the namespaces, applying the cgroups and seccomp profile, calling pivot_root to swap the process onto the image filesystem, and starting your process.[6] Docker and containerd are the friendly management layer on top. That standardization is why an image built with Docker runs under completely different tooling like Podman or Kubernetes without change.

Why "works on my machine" gets solved, and what you now know

An image packages the process and its entire userland filesystem together, the libraries, the runtime, the files it expects, as one versioned, content-addressed artifact that runs identically on a laptop, in CI, and in production, all on the same shared kernel interface. The surroundings travel with the application instead of being assembled differently in each place.

Strip away the branding and a container is a process the kernel has been told to show a private view through namespaces, to limit through cgroups, to restrict through capabilities and seccomp, and to run on a stack of read-only image layers. There is no second operating system and very little magic. That is not a letdown, it is the useful part, because it means container security is really kernel security, container performance is really host performance, and a container that misbehaves is really just a process you now know exactly how to inspect.

Sources (8)
  1. Docker overview (Docker Docs)
  2. namespaces(7) (Linux man-pages)
  3. cgroups(7) (Linux man-pages)
  4. capabilities(7) (Linux man-pages)
  5. OverlayFS (Linux Kernel documentation)
  6. Open Container Initiative Runtime Specification
  7. Brian Ward, How Linux Works, 3rd Edition (No Starch Press, 2021)
  8. Binildas A. Christudas, Java Microservices and Containers in the Cloud (Springer Nature, 2024)

Recommended reading