How Linux Namespaces and cgroups Work: Isolation, Resource Limits, and Why a Container Is Not a Virtual Machine
A process that calls unshare(CLONE_NEWPID) and then fork does not get a private machine. It gets a private view of process IDs. The child sees itself as PID 1. The host still sees both tasks under their real PIDs. Memory, the network stack, and the CPU scheduler remain shared unless other namespaces and controllers are applied. That split is what makes a container possible without the cost of a virtual machine.
This article explains the two kernel mechanisms Docker, containerd, and Kubernetes rely on: namespaces for isolation of names and cgroups for accounting and limits. Related reading: How Virtual Memory Works, How fork and Copy-on-Write Work, and How Linux CFS Works.
The contract
Namespaces virtualize a kernel resource so processes inside one namespace see a private instance. The kernel tags objects with a namespace pointer and filters observations. cgroups place tasks into hierarchical groups and attach controllers that account usage and enforce limits.
Namespaces
Linux provides PID, Network, Mount, UTS, IPC, User, Cgroup, and Time namespaces. Creation uses clone with CLONE_NEW* flags or unshare. Joining uses setns on /proc/pid/ns files. A container typically combines several so the process tree, mounts, network, and user IDs appear private while the host kernel remains shared.
cgroups
cgroups v2 uses a unified hierarchy. Controllers include cpu (weight and bandwidth), memory (limits and reclaim), io, pids, and cpuset. The kernel charges resources to the cgroup of the running or allocating task. Parent limits apply to the aggregate of children.
Why this is not a virtual machine
A hypervisor gives the guest its own kernel and page tables. Containers share the host kernel. User namespaces map container root to an unprivileged host user and restrict capabilities, but they do not create a second kernel. The performance benefit is the lack of VM exits and the ability to share page cache pages.
Misconceptions
A container does not have its own kernel. cgroups limit resources; namespaces isolate names. User namespaces shrink the attack surface but do not eliminate kernel vulnerabilities reachable from unprivileged code.
Takeaways
Namespaces give private views of global resources by filtering lookups. cgroups enforce hierarchical limits. Container runtimes compose both plus a root filesystem and capability drops to produce an isolated process tree that still runs on the host kernel. The boundary remains the kernel's correctness.