25.1. Understanding Container Isolation

Three kernel mechanisms define a container. Namespaces give the container its own view of processes, network devices, mounts, and user IDs; cgroups cap how much CPU, memory, and I/O it may consume; and capabilities slice the privileges of root into named pieces so a container's process 1 can be "root" without holding every power that root has on the host. Every choice in this chapter - rootless versus rootful, application versus system container, one runtime versus another - is a choice about how those three mechanisms are configured.

25.1.1. Namespaces, Cgroups, Capabilities, and Shared Kernels

A namespace makes a system resource appear private to the processes inside it. The kernel maintains one namespace per container per type, and the container's processes simply cannot name objects outside their own view. The namespaces that matter for daily container work:

Namespace What the container gets
pid its own process tree, numbered from 1
net its own interfaces, addresses, routes, and port space
mnt its own mount table and root filesystem
uts its own hostname
ipc its own System V and POSIX message queues and shared memory
user its own UID and GID map (the basis of rootless containers)
cgroup its own view of the cgroup hierarchy

The practical consequence of pid and mnt namespaces is what podman run does to a process; the consequence of the user namespace is what a rootless container cannot do to the host. Podman, the runtime used throughout this chapter, composes all of these at once. On ctr1, as john:

$ podman --version
podman version 5.4.2

The 5.4.2 release is the version Debian 13 ships. Its configuration is worth one look before running anything, because the report states whether this store is rootless, which storage driver it uses, and which components answer for networking and cgroups. On ctr1, as john:

$ podman info --format '{{.Host.Security.Rootless}} {{.Store.GraphDriverName}} {{.Host.NetworkBackend}} {{.Host.CgroupManager}}'
true overlay netavark systemd

The same command under sudo addresses the rootful store, which is an entirely separate image and container store owned by root. On ctr1, entered by john:

$ sudo podman info --format '{{.Host.Security.Rootless}} {{.Store.GraphDriverName}} {{.Host.NetworkBackend}}'
false overlay netavark

Two stores, two answers: Rootless=true for john's own store, false for root's. Both use the overlay storage driver - rootless through the kernel's support for user-namespace-mounted overlay filesystems - and both delegate network setup to netavark and cgroup management to systemd, which is the pairing Debian configures by default.

The user namespace is the piece that makes rootless containers safe, so its mapping deserves a direct look. podman unshare runs a command inside the same user namespace that rootless Podman prepares for containers. On ctr1, as john:

$ podman unshare cat /proc/self/uid_map
         0       1001          1
         1     100000      65536
     65537     165536      65536

Each line reads container ID, host ID, count. Container UID 0 maps to host UID 1001 - john - and to no other ID. Container UIDs 1 through 65536 map to host UIDs 100000 through 165535, and 65537 onward to the second subordinate range starting at 165536. Inside the container, a process may believe it is root; on the host it is john, or an unmapped pool ID with no account, home directory, or files attached. A compromised rootless container compromises john's account at worst, not the host.

The pid, net, and mnt namespaces are visible as symlinks under /proc/self/ns. The next example starts a container and reads its own namespace links; it is also the first image pull, so the transcript includes the fetch that Podman performs. On ctr1, as john:

$ podman run --rm docker.io/library/alpine:3.20 sh -c 'for n in pid net mnt; do readlink /proc/self/ns/$n; done'
Trying to pull docker.io/library/alpine:3.20...
Getting image source signatures
Copying blob sha256:25f1d6b1951ac8eb3740558fe94cb83d377bdadf95fd9f98b50d2e1b96130471
Copying config sha256:bf8527eb54c3680e728d5b4b383a8ba730d72dae7236fbc8dff97ed6b224a731
Writing manifest to image destination
pid:[4026532317]
net:[4026532247]
mnt:[4026532314]

The three pid:, net:, and mnt: values are inode numbers of distinct namespace objects. Compare them with the host's own - run the same readlink loop in john's shell and the numbers differ - and the split becomes concrete: the container's PID 1, its eth0, and its root directory are objects the host kernel tracks separately from the shell's.

Capabilities are the third mechanism. Linux divides traditional root powers into dozens of named capabilities, and every process carries a mask saying which it holds. A default container drops most of them even when it runs as root inside its user namespace. On ctr1, as john:

$ podman run --rm docker.io/library/alpine:3.20 grep CapEff /proc/self/status
CapEff: 00000000800405fb

The hexadecimal mask is the effective set: roughly a dozen capabilities such as CHOWN, DAC_OVERRIDE, SETUID, SETGID, NET_BIND_SERVICE, SYS_CHROOT, and AUDIT_WRITE - enough to run a conventional service, not enough to load kernel modules, reconfigure the host's network, or mount filesystems. The mask is per-process, and a container can be tightened further. On ctr1, as john:

$ podman run --rm --cap-drop=ALL docker.io/library/alpine:3.20 grep CapEff /proc/self/status
CapEff: 0000000000000000

--cap-drop=ALL empties the mask; the container's process then holds no capability at all and survives only on ordinary file permissions. Section 25.8.4 returns to capability hygiene, including the --privileged flag that deliberately grants everything and when that is (rarely) justified.

Container isolation layers and UID mapping

Figure 25.2. One kernel, partitioned six ways. The namespaces give the container private views, the cgroup caps its resource use, and the capability mask decides which root powers its process 1 actually holds. The UID map and the three capability masks are the values observed on the practice host, not illustrations.

Cgroups, finally, are the same mechanism described in Chapter 9: the kernel tracks and limits a subtree of processes as a unit. Podman places every container in its own cgroup under systemd's hierarchy, which is why --memory=128m in Section 25.4 works without any manual cgroup configuration, and why a container that exhausts its memory limit is killed by the kernel rather than by Podman. Namespaces partition what a container can see; cgroups partition what it can consume; capabilities partition what root can do. All three are enforced by the one shared kernel, which is why kernel security updates matter on container hosts just as they do on hypervisors.

25.1.2. Application Containers and System Containers

The word "container" covers two quite different shapes. An application container packages one process - a web server, a job runner, a build step - with the minimum filesystem it needs, as a layered OCI image that can be run identically on any host. A system container packages an entire Debian userspace: its own init, its own systemctl, its own package database, booting like a lightweight virtual machine but on the shared kernel. Debian supports both natively: Podman for the first, systemd-nspawn for the second.

Property Application container System container
First process the application itself an init system (systemd in Debian)
Root filesystem layered OCI image, replaced wholesale on updates a directory tree, upgraded with apt from inside
Services inside one, defined by the image as many as the machine needs
Persistent data named volumes and bind mounts the whole tree, plus bind mounts
Created by podman build from a Containerfile debootstrap (Section 25.7.1)
Managed by podman, Quadlet units (Section 25.4) machinectl, systemd-run -M

The distinction is one of degree, not of mechanism: a system container uses the same namespaces and cgroups as an application container, configured less aggressively so that a full init can run. Section 25.7 builds one and shows where the boundary sits. Neither shape replaces a virtual machine when the workload needs a separate kernel - Chapter 24 covers that case - and the choice is usually easy: package an application as an image, run an operating system only when you genuinely need a second one for integration or isolation work.

25.1.3. Rootless and Rootful Runtime Choices

Rootless Podman is the same program with a different starting point: the user who launched it. Because john cannot create devices, remount filesystems, or manage the host's cgroup tree, rootless Podman compensates with the user namespace from Section 25.1.1 - mapping its "root" onto john - and with user-space helpers for the two operations that normally require privilege: networking (pasta) and storage (fuse-overlayfs or user-namespace-mounted overlay). The result is a container runtime that an ordinary account can use on a shared host without any sudo grant.

Aspect Rootless Rootful (sudo podman)
Container root is the invoking user, via the user namespace host root
Host files reachable only what the user could already read everything
Ports below 1024 not bindable (no NET_BIND_SERVICE on the host side) bindable
Cgroup limits systemd delegates a subtree to the user full hierarchy control
Published ports forwarded by pasta in user space netavark rules in the host firewall
Storage location ~/.local/share/containers /var/lib/containers
Blast radius of a break the invoking account the host

Both modes ran in Section 25.1.1's podman info probes. The subordinate ID ranges that feed the user namespace are recorded per account in /etc/subuid and /etc/subgid; the pool they are drawn from is defined by SUB_UID_MIN, SUB_UID_COUNT, and their GID counterparts in /etc/login.defs. An account without entries cannot run rootless containers that write files; add ranges with sudo usermod --add-subuids 100000-165535 --add-subgids 100000-165535 john and log in again. On ctr1, as john:

$ grep '^john:' /etc/subuid /etc/subgid
/etc/subuid:john:165536:65536
/etc/subuid:john:100000:65536
/etc/subgid:john:165536:65536
/etc/subgid:john:100000:65536

Each entry names a start and a count: together they reserve host UIDs and GIDs 100000–165535 and 165536–231071 for john's containers, matching the two mapped rows of the earlier uid_map.

Rootful containers remain necessary for work that must act on the host: binding a port below 1024 directly, touching devices, or operating on storage the invoking user cannot reach. They also concentrate risk: a rootful container that misuses a mount or a capability acts with host root's authority. The working rule this chapter follows is the one worth taking home - rootless by default, rootful only for a named reason, and never --privileged where a single capability or a bind mount would do.