Privileged containers and Linux capabilities in Docker
The kernel keeps a container apart from the rest of the host with namespaces, a reduced set of capabilities,
a seccomp filter and, where available, an AppArmor or SELinux profile. Options such as --privileged,
--cap-add, --pid=host and --network=host remove parts of that separation. Some software needs one of
them; for most uses a narrower option does the same job.
Capabilities in short
Linux splits the rights of root into capabilities. CAP_NET_ADMIN allows network configuration,
CAP_SYS_MODULE loading kernel modules, CAP_SYS_PTRACE tracing other processes. On Docker 29.9 the default
effective set of a root process in a container decodes to these 14:
AUDIT_WRITE CHOWN DAC_OVERRIDE FOWNER FSETID KILL MKNOD NET_BIND_SERVICE NET_RAW SETFCAP SETGID SETPCAP SETUID SYS_CHROOT
To see the mask:
docker run --rm alpine:3.24 grep CapEff /proc/self/status
CapEff: 00000000a80425fb
capsh --decode=00000000a80425fb turns the mask into names. capsh is in the package libcap2-bin on Debian
and Ubuntu, libcap on RHEL, Rocky and Alma, and libcap-utils on Alpine.
What --privileged gives
--privileged (or privileged: true in compose) turns off most of the isolation at once. In a test on
Docker 29.9 with kernel 6.12:
| default container | --privileged |
|
|---|---|---|
Capabilities (CapEff) |
14 | all 41 of the kernel |
Seccomp (/proc/self/status) |
2 (filter on) |
0 (off) |
Entries in /dev |
15 | 161, including the disks of the host |
According to the Docker documentation, it also "reconfigures AppArmor or SELinux to allow the container nearly
all the same access to the host". With the disks in /dev and CAP_SYS_ADMIN, a root process in the container
can mount the file system of the host and change it. Treat a privileged container as root on the host.
Common reasons for --privileged, and what to use instead:
| Need | Narrower option |
|---|---|
A device (USB, GPU for video transcoding, /dev/net/tun for a VPN) |
--device /dev/net/tun (compose: devices:) |
| Network configuration inside the container (VPN, routing) | --cap-add NET_ADMIN and the device it needs |
| FUSE mounts | --cap-add SYS_ADMIN --device /dev/fuse, as in the Docker documentation; on hosts with AppArmor also --security-opt apparmor=unconfined, because the default profile docker-default denies mounts |
| Docker inside Docker for CI builds | a build tool that works without a daemon, rootless Docker, or a separate runner host |
| "It did not work without it" | read the error; it names the operation or the device that failed |
Broad capabilities
A single added capability is narrower than --privileged, but some of them are broad on their own. From the
capabilities(7) man page:
| Capability | What it allows |
|---|---|
SYS_ADMIN |
"Perform a range of system administration operations", among them mounting file systems. The man page calls it overloaded and "the new root". |
SYS_MODULE |
"Load and unload kernel modules". A kernel module runs with full rights in the kernel of the host. |
SYS_PTRACE |
"Trace arbitrary processes using ptrace(2)". With --pid=host, that includes processes of the host. |
NET_ADMIN |
Interface configuration, firewall rules and routing tables. In the network namespace of the container it affects only the container; with --network=host it changes the firewall of the host. |
ALL |
Every capability, close to --privileged without the devices. |
Drop everything and add back what the program needs:
docker run -d --cap-drop ALL --cap-add NET_BIND_SERVICE \
--security-opt no-new-privileges example/app:1.4.2
no-new-privileges stops setuid programs such as su or sudo inside the container from gaining rights. In
compose:
services:
app:
image: example/app:1.4.2
cap_drop: [ALL]
cap_add: [NET_BIND_SERVICE]
security_opt:
- no-new-privileges:true
Start with --cap-drop ALL, run the program, and add one capability per error. Many programs run without any.
Host namespaces: --pid=host and --network=host
--pid=host puts the container in the process namespace of the host. In a test, a container started with it
saw all host processes (/proc/1/comm was systemd) and their command lines, which sometimes contain
passwords or tokens. That host did not load an AppArmor profile for containers, and kill -0 1 from the
container succeeded: a root process in the container could send signals to host processes. Monitoring tools
and debugging tools ask for this option.
--network=host gives the container the network stack of the host. In the same test it saw 23 network
interfaces instead of 2, and 7 TCP sockets of the host that listen on 127.0.0.1. A service that counts on
being reachable only from localhost is reachable from such a container. Its ports are ordinary host sockets,
and ufw and firewalld filter them as usual (see Docker bypasses ufw). Use it
when a program needs it, as some VPN and network monitoring tools do.
According to the Docker documentation, neither option works with user namespace remapping (userns-remap).
Find these options on a host
docker ps -q | xargs -r docker inspect --format \
'{{.Name}} privileged={{.HostConfig.Privileged}} pid={{.HostConfig.PidMode}} net={{.HostConfig.NetworkMode}} capadd={{.HostConfig.CapAdd}}'
/vpn privileged=false pid= net=bridge capadd=[CAP_NET_ADMIN]
/node-monitor privileged=false pid=host net=host capadd=[]
An empty pid= means the container has its own process namespace. capadd lists the added capabilities with
a CAP_ prefix. In compose files, search for privileged:, cap_add:, pid: and network_mode:.
Change a container
- Remove
privileged: trueand start the container. Read the error and add the device or capability for the operation that failed. - Replace
cap_add: [ALL]with the list the program needs. - Remove
pid: hostandnetwork_mode: hostunless the documentation of the software says why it needs them. - Recreate with
docker compose up -d --force-recreate <service>, then run the inspect loop again.
The daily checks of Vulnpocalypse list privileged containers, dangerous capabilities and shared host namespaces by container name.
Sources
- Docker docs: Running containers, runtime privilege and Linux capabilities:
default capability list,
--privileged,--cap-add, FUSE example. - capabilities(7): meaning of each capability.
- Docker docs: Isolate containers with a user namespace:
does not work with
--pid=hostand--network=host. - Docker docs: Docker security, Linux kernel capabilities
Check your servers every day
The Vulnpocalypse collector reports the packages and settings of a server once a day, and the digest lists what to fix, most urgent first.
Vulnpocalypse