hostname lab# unshare -upf --mount-proc bash# hostname box# ps -eo pid,comm PID COMMAND 1 bash 3 psPID 1systemdhosthostname labhostname boxPID 1bashhost 4242PID 3pshost 4251appwants RAMPID 4OOMLinux kernelnamespacescgroupipcnetmntpidtimeuseruts/sys/fs/cgroup/boxmemory.maxmaxcgroup.procsCPUmemory.max 100 MiBbox cgroupmemoryunsharehostname boxmkdir box100M4242pidutsmnt
Back: user space (terminal, systemd, the box). Middle: the kernel with namespaces and cgroups. Front: CPU and memoryAqua: namespaces, the box's private view (white until explained)Tokens: typed commands, namespace copies, a start signal and envelopes to and from the kernelUser space at the back: the terminal, systemd (the host's PID 1) and the box, a glass plate. The Linux kernel in the middle holds the eight namespace badges and the cgroup folder; the CPU and memory are in front. unshare copies the pid, uts and mnt namespaces onto the box and bash starts inside as PID 1; hostname and ps show its private view. From a host terminal a cgroup gets a 100 MiB memory limit and bash's host PID, and the OOM killer stops a program in the box that goes past it. The host PID 4242 and the hostname lab are examples. Find bash's real host PID with lsns -t pid or ps -ef on the host. Inside the box, PIDs count up from 1: hostname ran as PID 2, so ps got 3 (checked on Debian 12). If your shell's start-up files run programs, ps gets a higher number. Column spacing differs between ps versions.

One kernel

  1. A container is not a second computer. Its programs are ordinary processes on the same Linux kernel as every other process. The kernel only gives them two extras: a smaller view of the system, and a budget.

Namespaces: what it sees

cgroups: what it uses

What happens

;; step 1/12 · one-kernel
container  = ordinary processes
           + namespaces  (what they can see)
           + cgroups     (what they can use)
kernel     one, shared by the host and every container
host       systemd is PID 1, the first process

Run these as root (sudo -i) in a throwaway virtual machine, not on a server you care about. To clean up: type exit in the box shell, then rmdir /sys/fs/cgroup/box in the host terminal (it must be empty). The host PID 4242 and the hostname lab are examples. Find bash's real host PID with lsns -t pid or ps -ef on the host. Inside the box, PIDs count up from 1: hostname ran as PID 2, so ps got 3 (checked on Debian 12). If your shell's start-up files run programs, ps gets a higher number. Column spacing differs between ps versions.