Guides / Containers

Privileged containers and Linux capabilities in Docker

intermediateUpdated 26 Nov 20261016 words

A container is a group of processes that the kernel keeps 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; often a narrower option does the same job. Below, each option comes with what it gives the container, a command that finds it on a host, and its narrower replacement.

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. Docker starts containers with a short list of them. 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

You can see the mask yourself:

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

The Docker documentation adds that it "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 people use it, 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 the Docker documentation shows
Docker inside Docker for CI builds a build tool that works without a daemon, rootless Docker, or a dedicated runner host
"It did not work without it" read the error; it names the operation or the device that failed

Capabilities worth a second look

Adding a single capability is narrower than --privileged, but some are broad on their own. The capabilities(7) man page describes them:

  • 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)". Combined 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 this affects only the container; with --network=host it changes the firewall of the host.
  • ALL: every capability, close to --privileged without the devices.

A tight setup drops everything and adds 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 need none.

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. On that host, which did not load an AppArmor profile for containers, kill -0 1 from the container succeeded: a root process in the container could send signals to host processes. Monitoring agents and debugging tools ask for it; most other software does not need it.

--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. Services that rely on "only reachable from localhost" are reachable from such a container. On the other hand its ports are ordinary host sockets, which ufw and firewalld filter as usual; see Docker bypasses ufw. Use it when a program needs it, as some VPN and network monitoring tools do.

Neither option works with user namespace remapping (userns-remap), according to the Docker documentation.

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=[]

pid= empty 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

  1. Remove privileged: true and start the container. Read the error and add the device or capability for the operation that failed.
  2. Replace cap_add: [ALL] with the list the program needs.
  3. Remove pid: host and network_mode: host unless the software documents why it needs them.
  4. Recreate: docker compose up -d --force-recreate <service>, then run the inspect loop again.

Patchbrief lists privileged containers, dangerous capabilities and shared host namespaces in its daily checks, with the container name.

Sources

Check your servers every day

The agent of Patchbrief reports the packages and settings of a server once a day; the digest names what to fix first.

Start free