Sub topic 9
Dropping capabilities and avoiding --privileged
This chapter assumes you can run containers and Dockerfiles, and that you know Linux users, file permissions, and processes. It builds on the previous chapters on namespaces and running as non-root.
What capabilities are
Linux splits the powers of root into about 40 units called capabilities. Examples are CAP_NET_BIND_SERVICE (bind ports below 1024), CAP_CHOWN (change file owners), and CAP_SYS_ADMIN (a very broad set of administrative actions). Docker starts every container with a default set of 14 capabilities. Most applications need very few of them.
Running as non-root helps, but it does not replace dropping capabilities. A process that is root inside the container still holds the default set, and some setuid binaries can regain powers. Dropping capabilities limits what a compromised process can do, even if it gains root.
Drop everything, add back what you need
Start from zero and add only what the application proves it needs.
docker run -d --name web \
--user 10001:10001 \
--cap-drop ALL \
--security-opt no-new-privileges \
myapp:1.0--cap-drop ALL removes every capability. no-new-privileges stops a process from gaining more privileges through setuid binaries or file capabilities.
If the app must listen on port 80, add back one capability:
docker run -d --name web \
--cap-drop ALL \
--cap-add NET_BIND_SERVICE \
myapp:1.0A simpler fix is to listen on a high port such as 8080 and publish it with -p 80:8080. That needs no capability at all.
Why --privileged is different
--privileged gives the container all capabilities, access to all host devices, and relaxes seccomp and AppArmor. The container is then close to a root shell on the host. From inside, an attacker can mount the host disk and read or change anything.
Narrow alternatives exist for common needs:
| Need | Instead of --privileged |
|---|---|
| Use one host device | `--device /dev/ttyUSB0` |
| Change network settings | `--cap-add NET_ADMIN` |
| Use a trace tool such as strace | `--cap-add SYS_PTRACE` |
Also avoid --cap-add SYS_ADMIN. It is so broad that it is nearly as risky as privileged mode. Mounting /var/run/docker.sock into a container is equally dangerous, and a later chapter covers it.
Checking what a container has
You can inspect the settings of a running container:
docker inspect web --format 'Privileged={{.HostConfig.Privileged}} CapAdd={{.HostConfig.CapAdd}} CapDrop={{.HostConfig.CapDrop}}'To see the capabilities of the process itself, read its status inside the container:
docker exec web grep Cap /proc/1/statusAn all-zero CapEff value means the main process has no effective capabilities.
Common errors
- Dropping all capabilities and seeing "permission denied" at startup. The app probably binds a low port or changes file ownership. Add back only the matching capability.
- Using
--privilegedto "make it work" and never removing it. - Assuming
--useralone is enough. Without--cap-drop, the default capabilities remain available to any process that gains root. - Forgetting that Docker Compose has equivalent keys,
cap_drop,cap_add, andsecurity_opt, which should carry the same settings.
Exercise
A container needs to listen on port 443 as a non-root user. It currently runs with --privileged. Write the safest run options that still work, and name one alternative that avoids adding a capability.
Show answer
Use --user 10001:10001 --cap-drop ALL --cap-add NET_BIND_SERVICE --security-opt no-new-privileges and remove --privileged. The alternative is to have the app listen on a high port such as 8443 and publish it with -p 443:8443, so no capability is needed.