How Linux Containers Work: Namespaces, cgroups, and Isolation

A container is not a miniature virtual machine and it is not a special object inside the Linux kernel. It is a process, or a tree of processes, given a private view of the system and a budget for CPU, memory, and I/O. Container runtimes assemble existing kernel knobs.

This article explains the two primitives that do most of the work: namespaces, which change what a process can see, and cgroups, which change how much of a resource it can consume. Overlay filesystems, seccomp, and Linux Security Modules complete the picture.

Processes and threads are the execution units being isolated; see Processes vs Threads Explained. Address spaces still use page tables; see How Virtual Memory Works.

There Is No container_t

The kernel tracks tasks, namespaces, mounts, sockets, and control groups. It does not track a type named container. When a runtime starts a container it creates or joins namespaces with clone(), unshare(), or setns(); moves the process into a cgroup; sets up a root filesystem and calls pivot_root; optionally installs a seccomp filter and an LSM profile; then executes the entrypoint.

After that, the scheduler treats the workload like any other process. Isolation is a property of the namespaces and limits attached to those tasks.

Namespaces Change Visibility

A namespace wraps a global kernel resource so members see their own instance of it. Linux exposes eight types: Mount (the mount tree), UTS (hostname), IPC (System V IPC and POSIX queues), PID (process IDs), Network (interfaces, routes, sockets), User (UIDs, GIDs, capabilities), Cgroup (apparent cgroup root), and Time (offsets for CLOCK_MONOTONIC and CLOCK_BOOTTIME).

A process holds a pointer to an nsproxy that references most of these. When a child is created without new-namespace flags, it shares the parent's nsproxy. When any namespace diverges, the kernel allocates a new proxy. That is why a runtime can join a container's network namespace without joining its mount namespace.

PID Namespaces and the Fake Init

Inside a PID namespace, numbering starts at 1. The same task still has a different PID in every ancestor namespace. Tools on the host see the host PID; ps inside the container sees 1, 2, 3.

PID 1 is supposed to reap orphaned children. If the container's PID 1 ignores SIGCHLD and never waits, zombies accumulate. That is why runtimes install a small init when the application is not written to be init. Killing the host PID of that init usually tears down the tree because children die when their namespace init exits.

Mount Namespaces and a Private Root

A mount namespace is a copy of the caller's mount tree at creation time, subject to propagation settings. The runtime mounts the image, bind-mounts volumes and resolv.conf, mounts a fresh proc so /proc/1 matches the new PID namespace, then calls pivot_root so the image becomes /.

chroot alone is not enough. It changes the root for path lookup but leaves the old mount tree reachable through open file descriptors. pivot_root swaps the root mount and lets the runtime unmount the old tree.

Overlay filesystems give a writable disk without copying the image. A lower layer holds the image, an upper layer holds writes. Containers that start from the same image share the lower layer until one writes a file.

Network Namespaces and veth Pairs

A new network namespace starts with only loopback. The common pattern is a virtual Ethernet pair: one end in the container as eth0, the other on a host bridge. Because sockets belong to a network namespace, two containers can both bind port 80. Publishing a host port is an explicit forward, not a shared TCP stack. TCP still works as described in How TCP Works; the namespace only chooses which interfaces that stack sees.

User Namespaces and Root That Is Not Root

User namespaces map container UIDs to host UIDs. Rootless setups often map container UID 0 to host UID 1000. Inside the namespace the process is root. On the host those operations run as 1000. Capabilities are namespaced too: CAP_SYS_ADMIN inside a user namespace is not CAP_SYS_ADMIN on the host. That mapping is what makes unprivileged engines possible and why bugs in user namespaces have historically been escalation paths.

cgroups Budget the Process

Namespaces hide things. They do not stop a process from filling RAM. Control groups do that. A cgroup is a directory in a virtual filesystem. Writing a PID into cgroup.procs puts the task in the group. Controller files set limits.

cgroup v2 uses a unified hierarchy. cpu.max sets quota and period. cpu.weight sets relative share. memory.max is a hard cap; exceeding it triggers reclaim and then a cgroup-scoped OOM kill. memory.high throttles before the cap. pids.max stops fork bombs. v2 forbids internal processes in non-leaf cgroups, so orchestrators put tasks in leaves.

A cgroup namespace does not create new limits. It only changes the path shown in /proc/self/cgroup and which subtree a delegated process may manage.

What Isolation Does Not Give You

Containers share a kernel. A kernel exploit is a host exploit. Namespaces and cgroups reduce the blast radius of a buggy program. They are not a hypervisor.

The kernel also shares the syscall table unless seccomp hides calls, the page cache for shared inodes, and hardware side channels. Seccomp attaches a filter that allows or denies syscalls by number. AppArmor and SELinux add path and label checks. Isolation is the product of that whole stack, not of the word container.

How a Runtime Puts the Pieces Together

The engine allocates an ID, a rootfs, and a cgroup path. It creates a child with CLONE_NEWNS, CLONE_NEWPID, CLONE_NEWNET, CLONE_NEWIPC, CLONE_NEWUTS, CLONE_NEWCGROUP, and often CLONE_NEWUSER. The child writes UID maps, sets the hostname, joins the cgroup, configures veth, pivots root, drops capabilities, installs seccomp, then execve's the entrypoint.

From that moment the application is a normal process. /proc/<host-pid>/ns/ shows the namespace inodes it holds. Two processes with the same set of namespace inodes are in the same container from the kernel's point of view.

Containers Compared with Virtual Machines

A virtual machine runs a guest kernel. The cost is extra memory, slower boot, and a heavier I/O path. A container reuses the host kernel. Start time is process start time. Density is limited by host RAM and cgroup policy. Untrusted multi-tenant systems often put VMs under a container API. Platforms deploying their own binaries usually want containers.

Misconceptions

A container is a lightweight VM. It is a process with extra namespace and cgroup attributes.

PID 1 in a container is like systemd. Only if you installed an init that behaves that way.

memory.max equals VM RAM. File-backed pages and host page cache are accounted under specific controller rules. Treat the number as a policy, not as a DIMM.

Unpublished ports are unreachable. They are unreachable via host forwards. Processes that share or can enter the net namespace still see the sockets.

Rootless means no privileges exist. It means host-root is not required to start the container. Mapped root still needs constraints.

Takeaways

A Linux container is a packaged illusion: namespaces rewrite what the process sees, cgroups cap what it can spend, a root filesystem plus pivot_root give it a private /, and seccomp plus LSMs shrink the syscall surface. The kernel never allocates a container object. It allocates tasks that share a particular set of namespace inodes and a cgroup path.

When a container misbehaves, ask which layer failed. A name collision is a namespace issue. A noisy neighbor is a cgroup issue. A breakout is usually a kernel, seccomp, or capability issue. The runtime is glue. The isolation is in the kernel APIs it called.

Next Post Previous Post