Sub topic 2
How isolation works: namespaces, cgroups, and capabilities
This chapter assumes you can build and run containers, use the Linux command line, and read basic Linux process and permission concepts. Examples need a Linux host, or a Linux VM under Docker Desktop. Commands that read /proc or /sys run inside containers, so they work the same way anywhere.
A container is an ordinary Linux process with three kinds of restriction applied by the kernel. Namespaces limit what the process can see. Control groups (cgroups) limit how much it can use. Capabilities limit what privileged operations it can perform. Every later hardening step in this tutorial adjusts one of these three.
Namespaces: limiting what a process can see
A namespace wraps a global system resource so that processes inside it see their own private copy. Docker creates a new set of namespaces for each container.
| Namespace | What it isolates | Effect in a container |
|---|---|---|
| `pid` | Process IDs | The container's first process is PID 1. Host processes are invisible. |
| `net` | Network interfaces, routes, ports | The container has its own `eth0` and its own port space. |
| `mnt` | Mount points | The container sees its own root filesystem, not the host's. |
| `uts` | Hostname and domain name | The container can have its own hostname. |
| `ipc` | Shared memory and message queues | Containers do not share System V IPC objects by default. |
| `user` | User and group ID mappings | Not enabled by default in Docker. See the callout below. |
| `cgroup` | The view of the cgroup tree | The container sees its own cgroup as the root. |
Every process lists its namespaces under /proc/<pid>/ns. Two processes are in the same namespace when the link targets match. Compare the host with a container:
ls -l /proc/self/ns/pid /proc/self/ns/net
docker run --rm alpine ls -l /proc/self/ns/pid /proc/self/ns/netThe inode numbers in brackets, such as pid:[4026531836], differ between the two outputs. That difference is the isolation.
You can see the PID namespace working directly:
docker run --rm alpine psThe output lists only ps and its parent shell, starting at PID 1. Now remove the isolation on purpose:
docker run --rm --pid=host alpine psThis lists every process on the host. A container started with --pid=host can inspect, and with enough privilege signal, host processes. The same pattern applies to --net=host, --ipc=host, and --uts=host. Each flag shares one namespace with the host and removes one boundary.
The mount namespace and the filesystem
The mount namespace gives the container its own root filesystem, built from the image layers plus a writable layer. This is why a container cannot read /etc/shadow from the host by default. Bind mounts and volumes deliberately punch holes through this boundary. Mounting / or /var/run/docker.sock into a container hands over control of the host, which is covered in the daemon chapter.
Control groups: limiting what a process can use
Namespaces do nothing to stop one container from consuming all the memory or CPU on the host. Cgroups do that. A cgroup is a kernel object that groups processes and applies resource controllers to the group. The common controllers are:
memory: caps RAM use. A process that exceeds the cap is killed by the out-of-memory (OOM) killer.cpu: caps or weights CPU time.pids: caps the number of processes and threads, which defends against fork bombs.io: throttles block device bandwidth.
Docker translates run flags into cgroup settings. On a host using cgroup v2, which is the default on current distributions, you can read the values from inside the container:
docker run --rm --memory 128m --cpus 0.5 --pids-limit 50 alpine sh -c '
echo "memory.max: $(cat /sys/fs/cgroup/memory.max)"
echo "cpu.max: $(cat /sys/fs/cgroup/cpu.max)"
echo "pids.max: $(cat /sys/fs/cgroup/pids.max)"
'Expected output:
memory.max: 134217728
cpu.max: 50000 100000
pids.max: 50The cpu.max pair means 50,000 microseconds of CPU time allowed per 100,000 microsecond period, which is half of one CPU. On a cgroup v1 host the paths differ (for example /sys/fs/cgroup/memory/memory.limit_in_bytes), so check which version you have with stat -fc %T /sys/fs/cgroup. The result cgroup2fs means v2.
Without limits, a container can use all host memory. When the host runs out, the kernel may kill an unrelated process, including the Docker daemon. Setting --memory and --pids-limit on every long-running container is cheap protection. A later chapter returns to these flags together with read-only filesystems.
Capabilities: splitting up root
Traditional Unix has two kinds of process: root, which can do anything, and everyone else. Linux capabilities break root's power into about forty separate units. For example, CAP_NET_BIND_SERVICE allows binding ports below 1024, CAP_SYS_TIME allows setting the clock, and CAP_SYS_ADMIN allows a very broad set of operations, including mounting filesystems.
Each process carries several capability sets. The two you will see most are the permitted set, which is the ceiling, and the effective set, which the kernel checks on each privileged operation. Docker starts container processes with a reduced default set of 14 capabilities:
CHOWN, DAC_OVERRIDE, FSETID, FOWNER, MKNOD, NET_RAW, SETGID, SETUID, SETFCAP, SETPCAP, NET_BIND_SERVICE, SYS_CHROOT, KILL, AUDIT_WRITE.
That is far fewer than the roughly forty a real root process has. It is still more than most applications need. NET_RAW allows crafting raw packets, which enables ARP spoofing against other containers on the same bridge network. DAC_OVERRIDE lets the process ignore file permission checks.
You can read the set from /proc:
docker run --rm alpine grep Cap /proc/self/status
docker run --rm --cap-drop ALL alpine grep CapEff /proc/self/statusWith defaults, CapEff is 00000000a80425fb. After --cap-drop ALL it is 0000000000000000. On a host with the libcap tools installed, decode the first value:
capsh --decode=00000000a80425fbThe output lists the 14 names above. If you drop everything and the application then fails, add back only what the error message points to, for example --cap-add NET_BIND_SERVICE.
Capabilities apply to a process regardless of its UID. A process running as a non-root user with no capabilities cannot perform privileged operations. A process running as root with a full set can. The two controls reinforce each other, which is why the next chapters pair --user with --cap-drop.
What the isolation does not cover
All three mechanisms are enforced by one kernel, shared between the host and every container. That has direct consequences:
- A kernel vulnerability can let a process escape all three restrictions at once. Containers do not provide the separation that a virtual machine with its own kernel does.
- Some kernel resources are not namespaced, including the system clock, kernel modules, and parts of
/proc/sys. Kernel keyrings were partly shared in older setups. - Namespaces hide things but do not enforce policy. A process with
CAP_SYS_ADMINin the initial namespaces can often undo the isolation. - The
--privilegedflag grants all capabilities, disables the default seccomp and AppArmor profiles, and exposes host devices. It removes most of the protection described here.
Isolation depends on the settings you leave in place. Each host-sharing flag (--pid=host, --net=host, --privileged, -v /:/host) is a removed boundary, and each limit you omit is a missing control.
Exercise
You run docker run --rm --pid=host alpine kill -9 1234, where 1234 is a PID on the host. The container runs as root with the default capabilities. Which namespace did you share, and which default capability makes the kill likely to succeed on a process owned by another user?
Show answer
You shared the PID namespace with the host, so PID 1234 is visible. The default capability CAP_KILL lets the process bypass the permission check for sending signals to processes owned by other users. Running the container as a non-root user with --cap-drop ALL, and not using --pid=host, would stop this.
Exercise
A container runs with --memory 128m on a cgroup v2 host. What value do you expect in /sys/fs/cgroup/memory.max inside the container, and what happens to a process that tries to allocate more than the limit?
Show answer
The value is 134217728 (128 × 1024 × 1024 bytes). When the container's memory use exceeds the limit and the kernel cannot reclaim enough, the OOM killer terminates a process in that cgroup. Other containers and the host are not affected.