Sub topic 2
How isolation works: namespaces, cgroups, and capabilities
This chapter assumes you can build and run containers, read a Dockerfile, and use basic Linux commands. You should also know what a Linux process, user ID (UID), and file permission are.
A container is an ordinary Linux process with restrictions applied by the kernel. Three features create those restrictions: namespaces limit what a process can see, cgroups limit what it can use, and capabilities limit what privileged actions it can take.
Namespaces: what a process can see
A namespace gives a process its own view of one system resource. Docker starts each container in a new set of namespaces.
| Namespace | What it isolates |
|---|---|
| pid | Process IDs. The container's first process is PID 1. |
| net | Network interfaces, routes, and ports. |
| mnt | Mount points and the filesystem tree. |
| uts | Hostname and domain name. |
| ipc | Shared memory and message queues. |
| user | UID and GID mappings (not enabled by default). |
You can see namespaces on a running container. Every process has a link for each one under /proc.
docker run -d --name demo alpine sleep 600
PID=$(docker inspect --format '{{.State.Pid}}' demo)
sudo ls -l /proc/$PID/ns
ls -l /proc/self/nsThe pid, net, and mnt links for the container differ from those of your shell. That difference is the isolation.
Cgroups: what a process can use
A control group (cgroup) caps and accounts for the resources a group of processes uses: memory, CPU time, process count, and disk I/O. Without limits, one container can use all host memory and cause the kernel to kill other processes. Docker creates a cgroup per container, and flags such as --memory and --pids-limit write values into it. Chapter 10 covers these flags in detail.
Capabilities: what a process can do
Traditionally, root (UID 0) could do everything and other users could do almost nothing. Linux capabilities split root's power into about 40 separate rights. Examples are CAP_NET_BIND_SERVICE (bind to ports below 1024), CAP_CHOWN (change file owners), and CAP_SYS_ADMIN (a very broad set of administrative actions).
Docker starts containers with a reduced default set, including CHOWN, SETUID, NET_BIND_SERVICE, and KILL. Root inside a container is therefore weaker than root on the host, but it is still more powerful than most applications need.
You can inspect the effective set from inside a container:
docker run --rm alpine sh -c 'grep CapEff /proc/self/status'
docker run --rm --cap-drop ALL alpine sh -c 'grep CapEff /proc/self/status'The second command prints 0000000000000000, which means no capabilities.
Common errors and gotchas
- Treating a container as a virtual machine. All containers share the host kernel. A kernel vulnerability can let a process escape every namespace at once.
- Assuming container root is harmless. Without the user namespace, UID 0 in the container is UID 0 on the host. Files in a bind mount are owned by real root.
- Forgetting that not everything is namespaced. The kernel keyspace, system time, and kernel modules are shared.
- Running without limits. Namespaces do not stop a process from exhausting memory or spawning thousands of processes.
Exercise
A container has its own pid, net, and mnt namespaces, but no memory limit. Which risk remains, and which kernel feature addresses it?
Show answer
A process in the container can consume all host memory and affect other workloads. Namespaces only control visibility, not usage. A cgroup memory limit, set with docker run --memory, addresses it.
Exercise
Run docker run --rm --cap-drop ALL alpine chown 1000 /etc/hostname. Will it succeed, and why?
Show answer
It fails with "Operation not permitted". Changing a file's owner needs CAP_CHOWN, and --cap-drop ALL removed it, even though the process runs as root.