Sub topic 3
How containers isolate: namespaces, cgroups, capabilities, seccomp
This chapter assumes you know basic Docker usage (images, containers, volumes), can work on the Linux command line, and understand Linux users and file permissions. It explains the kernel features behind docker run so that the hardening flags in the next chapter make sense. Each flag maps to one of these features.
A container is a process with restrictions
A container is not a small virtual machine. It is an ordinary Linux process, or a group of processes, started with extra restrictions from the host kernel. The container shares the host kernel with every other container and with the host itself.
Four kernel features do most of the work:
| Feature | What it restricts | Question it answers |
|---|---|---|
| Namespaces | What the process can see | Which processes, mounts, networks, and users exist? |
| cgroups | How much the process can use | How much CPU, memory, and how many processes? |
| Capabilities | What privileged operations it may do | Can it mount filesystems, change clocks, load modules? |
| seccomp | Which system calls it may make | Can it call `ptrace`, `keyctl`, `mount`? |
A system call (syscall) is a request from a program to the kernel, such as opening a file or creating a process. Every action a sandboxed agent takes eventually becomes syscalls, so these four features together define what the agent can do.
Namespaces: limiting what the process can see
A namespace gives a process its own view of one kind of system resource. Processes in different namespaces see different versions of that resource. Linux has several kinds:
| Namespace | Isolates | Effect in a container |
|---|---|---|
| `pid` | Process IDs | The container's first process is PID 1. It cannot see host processes. |
| `mnt` | Mount points | The container sees its own root filesystem and only the volumes you mount. |
| `net` | Network interfaces, routes, ports | The container has its own interfaces. With `--network none` it has only loopback. |
| `user` | User and group IDs | UID 0 inside can map to an unprivileged UID outside. |
| `uts` | Hostname | The container has its own hostname. |
| `ipc` | Shared memory, message queues | The container cannot touch host shared memory. |
| `cgroup` | The cgroup tree view | The container sees only its own cgroup. |
You can see namespaces directly. Every process has links under /proc/<pid>/ns. Two processes share a namespace when the link targets match.
# On the host: namespaces of your shell
ls -l /proc/self/ns/pid /proc/self/ns/net
# Inside a container: different identifiers
docker run --rm python:3.12-slim ls -l /proc/self/ns/pid /proc/self/ns/netThe numbers in brackets, such as pid:[4026531836], differ between the host and the container. If they match, the container shares that namespace with the host.
You can also create namespaces without Docker. The unshare tool starts a command in new namespaces:
sudo unshare --pid --fork --mount-proc ps auxThis prints only a couple of processes, because the new PID namespace hides everything else.
Shared namespaces are an escape route
Docker lets you opt out of isolation. These flags remove a namespace boundary:
--pid=hostlets the container see and signal host processes.--network=hostputs the container on the host's network stack, including services bound to127.0.0.1.--ipc=hostshares host shared memory.--privilegedremoves most restrictions at once (see below).
For an agent sandbox, never use these flags. An agent that can see the host's process list or reach host loopback services can probe or attack them.
The user namespace and UID mapping
By default, Docker does not enable a user namespace. UID 0 in the container is UID 0 on the host, restricted only by capabilities and seccomp. A file the container creates as root on a bind mount is owned by root on the host. That is why the examples in this tutorial use --user 1000:1000. A non-root process is much less useful to an attacker even if some other layer fails.
Docker can remap IDs with userns-remap in the daemon configuration, so container root maps to an unprivileged host UID range. Rootless Docker and Podman go further and run the whole container runtime as an unprivileged user. These options reduce the damage of an escape, at the cost of some compatibility problems with bind mounts and file ownership.
cgroups: limiting what the process can use
Control groups (cgroups) account for and limit resource use by a group of processes. Namespaces control visibility. cgroups control consumption. Without cgroups, an agent that runs a fork bomb or allocates all memory can freeze the whole host.
The Docker flags map to cgroup controllers:
| Docker flag | cgroup controller | What happens at the limit |
|---|---|---|
| `--memory 2g` | memory | The kernel's out-of-memory killer ends a process in the group. |
| `--cpus 2` | cpu | The group is throttled to two CPUs' worth of time. |
| `--pids-limit 256` | pids | `fork` fails with an error once the count is reached. |
You can check the limits from inside a container on a host using cgroup v2:
docker run --rm --memory 512m --pids-limit 64 python:3.12-slim \
sh -c 'cat /sys/fs/cgroup/memory.max /sys/fs/cgroup/pids.max'The output is 536870912 and 64. Chapter 6 covers choosing values. Here the point is that these limits are enforced by the kernel, not by Docker, and a process inside cannot raise them.
A cgroup limit protects availability. It does not stop data theft or file deletion.
Capabilities: splitting up root
Traditionally, UID 0 could do anything. Linux capabilities split that power into about forty separate privileges. A process holds a set of capabilities, and a privileged operation succeeds only if the process holds the right one.
Some examples:
| Capability | Allows |
|---|---|
| `CAP_NET_RAW` | Raw sockets, such as crafting packets |
| `CAP_NET_BIND_SERVICE` | Binding to ports below 1024 |
| `CAP_CHOWN` | Changing file ownership |
| `CAP_SETUID` | Changing the process's user ID |
| `CAP_SYS_ADMIN` | A very broad set, including `mount` and many namespace operations |
| `CAP_SYS_PTRACE` | Inspecting and tracing other processes |
Docker starts containers with a reduced default set of roughly fourteen capabilities, including CHOWN, SETUID, NET_RAW, and NET_BIND_SERVICE. Most coding tasks need none of them. That is why the hardened command uses --cap-drop=ALL.
If a tool fails after you drop everything, add back only what it needs:
docker run --rm --cap-drop=ALL --cap-add=NET_BIND_SERVICE python:3.12-slim \
python -c "print('bind to port 80 would work')"no-new-privileges
Capabilities can come back through setuid binaries, which are programs that run with the file owner's privileges (for example sudo or su). The flag --security-opt no-new-privileges sets a kernel bit that prevents a process and its children from gaining privileges through execve. Setuid bits and file capabilities stop having any effect. Use it together with --cap-drop=ALL, because dropping capabilities alone leaves the setuid route open on images that ship such binaries.
seccomp: filtering system calls
Secure computing mode (seccomp) lets a process install a filter that the kernel checks on every syscall. A filter can allow a call, return an error, or kill the process. Docker applies a default profile that blocks around 40 to 50 of the more than 300 syscalls, including ones for loading kernel modules, rebooting, and changing the system clock.
Capabilities and seccomp overlap but are different. A capability gates a class of privileged operations. A seccomp filter gates the entry point itself, so a syscall blocked by seccomp never reaches kernel code that might contain a bug. This matters because most container escapes exploit kernel vulnerabilities reached through syscalls.
You can supply a stricter profile, written in JSON:
docker run --rm --security-opt seccomp=./strict-profile.json python:3.12-slim bashDo not use --security-opt seccomp=unconfined for agents. It turns the filter off.
A custom profile is more work than the other controls. A profile that is too strict breaks interpreters and package managers in confusing ways, usually with Operation not permitted errors and no hint about which syscall was denied. Start from Docker's default profile and remove syscalls one at a time, testing real agent tasks after each change.
What these features do not provide
All four features restrict a process, but the process still talks to the same kernel as the host. This produces the main limitation of containers:
- A bug in the kernel's syscall handling, filesystems, or networking code can let a process break out, whatever the namespace, cgroup, capability, and seccomp settings.
- Namespaces do not hide everything. Some kernel interfaces, such as parts of
/procand/sys, are masked or made read-only by Docker, but the list depends on the runtime and version. - Kernel resources that are not namespaced, such as the kernel keyring in older setups, can leak between containers.
- The settings only help if they are applied. A single
--privilegedor--pid=hostcancels the layers you configured.
For a coding agent that runs code from unreviewed sources, this shared-kernel risk is the reason later chapters introduce gVisor and microVMs. Containers raise the cost of an attack and contain most mistakes. They are not a hard boundary against a determined attacker.
Verifying your settings
Do not assume the flags took effect. Check from inside the container:
docker run --rm --user 1000:1000 --cap-drop=ALL \
--security-opt no-new-privileges --pids-limit 64 --network none \
python:3.12-slim sh -c '
id
grep -E "^(CapEff|NoNewPrivs|Seccomp):" /proc/self/status
cat /sys/fs/cgroup/pids.max
ls /sys/class/net
'Expected results:
idshowsuid=1000 gid=1000.CapEffis0000000000000000, meaning no effective capabilities.NoNewPrivsis1.Seccompis2, meaning a filter is active.pids.maxis64.- Only
loappears as a network interface.
Keep a check like this in your sandbox's test suite. It catches the case where a wrapper script or Compose file silently drops a flag.
Common errors
- Operation not permitted on a task that worked before. A dropped capability or seccomp rule is blocking it. Find the cause with
straceon a development machine, then add back the single capability needed. - Files created in the project are owned by an unexpected user. The container UID differs from your host UID. Run with
--user "$(id -u):$(id -g)"for bind-mounted projects. - Memory limit set but no error visible. The kernel killed a process. The shell reports exit code 137 (128 plus signal 9). Check with
docker inspect --format '{{.State.OOMKilled}}' <container>. - Assuming a read-only root filesystem is a namespace feature. It is a mount option applied to the container's root. It helps, but it does not replace the four features above.
Exercise
A teammate runs an agent with docker run --cap-drop=ALL --pids-limit 128 --memory 1g --pid=host python:3.12-slim bash. Which kernel feature is weakened by the --pid=host flag, what can the agent now do that it could not before, and why do the other flags not prevent it?
Show answer
--pid=host removes the PID namespace boundary, which controls what the process can see. The agent can now see all host processes, including their command lines (which may contain secrets or tokens), and can read their /proc entries. It can also signal host processes owned by the same user, and as root inside the container it could signal any process unless a capability or user mapping prevents it. --cap-drop=ALL limits privileged operations, --pids-limit and --memory limit resource use through cgroups. None of these controls visibility, so they do not stop the agent from observing the host. The fix is to remove --pid=host.
Exercise
Write a docker run command that starts python:3.12-slim as UID 1000 with no capabilities, no privilege escalation, a limit of 100 processes, and no network. Then name the line in /proc/self/status that confirms no capabilities are active.
Show answer
docker run --rm -it --user 1000:1000 --cap-drop=ALL --security-opt no-new-privileges --pids-limit 100 --network none python:3.12-slim bash. Inside, grep CapEff /proc/self/status should show CapEff: 0000000000000000.