Sub topic 3
Dropping all capabilities and adding back the minimum
This chapter assumes you can run containers with docker run and know how Linux users, file permissions, and processes work. The previous chapter explained what capabilities are. This one shows how to remove them and how to restore only what a workload needs.
Why drop everything first
By default, Docker gives a container 14 capabilities, even when the process runs as root. They include CHOWN, DAC_OVERRIDE, SETUID, SETGID, NET_RAW, and NET_BIND_SERVICE. Most applications use few or none of them. Each one you keep is more kernel surface a compromised process can use.
The safest approach is an allowlist. Drop all capabilities, then add back only the ones the application proves it needs. Starting from the default set and removing capabilities one by one is a denylist. A denylist is easy to get wrong because you must know every capability that matters.
Dropping all capabilities
Use --cap-drop ALL. This command prints the container's capability masks:
docker run --rm --cap-drop ALL alpine grep Cap /proc/self/statusCapEff is the set the kernel checks when the process attempts a privileged operation. With everything dropped, CapEff and CapPrm print as 0000000000000000. CapBnd is the bounding set, the ceiling on what the process could ever gain, and it is also zero.
Adding back the minimum
Use --cap-add after --cap-drop ALL. Names are written without the CAP_ prefix. The order of the flags on the command line does not matter, because Docker applies the drops before the adds.
A web server running as root that listens on port 80 needs NET_BIND_SERVICE. Binding to a port below 1024 is a privileged operation. Without the capability, the bind fails:
docker run --rm --cap-drop ALL \
python:3.12-slim python -m http.server 80The server exits with PermissionError: [Errno 13] Permission denied. Add the one capability it needs:
docker run --rm --cap-drop ALL --cap-add NET_BIND_SERVICE \
python:3.12-slim python -m http.server 80Now the server starts. Run grep Cap /proc/self/status the same way to see that CapEff has a single bit set.
Finding which capability is missing
Failures from missing capabilities usually show up as Operation not permitted or Permission denied. Common sources of these errors:
| Symptom | Likely capability |
|---|---|
| Cannot bind to a port below 1024 | `NET_BIND_SERVICE` |
| `chown` fails on a file | `CHOWN` |
| Root cannot read a file owned by another user with mode 600 | `DAC_OVERRIDE` |
| Process cannot switch user with `setuid` | `SETUID`, `SETGID` |
| `ping` fails with a raw socket error | `NET_RAW` |
Check the application's documentation, then change one capability at a time and rerun. Many official images start as root and then switch to an unprivileged user. The official nginx image does this, so it needs CHOWN, SETUID, and SETGID as well as NET_BIND_SERVICE. Running it as a non-root user on a high port avoids most of these.
Common mistakes
- Writing
--cap-addwithout--cap-drop ALL. This only adds to the default set and removes nothing. - Using the
CAP_prefix in Docker flags. Docker acceptsNET_BIND_SERVICE, and the prefix form is for Kubernetes manifests only in some tools, so be consistent with what the tool documents. - Assuming root in a container is harmless once capabilities are dropped. Root still owns files and can reach anything the filesystem permits. The next chapters add a non-root user and a read-only filesystem for that reason.
Exercise
A container started with --cap-drop ALL runs a script as root that calls chown 1000:1000 /data/file and fails with "Operation not permitted". Which flag fixes this with the smallest change, and why not a broader flag?
Show answer
Add --cap-add CHOWN. The chown call to give a file to another user requires the CHOWN capability, and it is the only one the script needs. A broader choice such as --privileged or --cap-add SYS_ADMIN would restore far more kernel access than the script requires.