Sub topic 12
Troubleshooting failures and tightening step by step
This chapter assumes you can run containers with docker run, read basic Linux permissions and process concepts, and read simple YAML and JSON. It also assumes you have met the controls from earlier chapters: dropped capabilities, no-new-privileges, seccomp, and a read-only root filesystem.
Tighten in small steps
Hardening breaks applications in ways that look unrelated to security. A web server fails to start, or a log file cannot be opened. If you apply every control at once, you cannot tell which one caused the failure.
Add one control at a time and run the application after each change. Keep the last working command. When something breaks, you know which control did it.
Match the symptom to the control
Each control produces a recognizable kind of error. The table maps what you see to the likely cause and the fix.
| Symptom | Likely cause | Fix |
|---|---|---|
| `Read-only file system` on write | Read-only root filesystem | Mount a tmpfs or volume at that path |
| `Permission denied` on a file you own as root | Non-root user | Change ownership or use a volume with the right owner |
| `Operation not permitted` when binding port 80 | Missing `NET_BIND_SERVICE` | Add the capability back, or listen on a port above 1024 |
| `Operation not permitted` on `chown` | Missing `CHOWN` | Fix ownership at build time, or add `CHOWN` |
| `Operation not permitted` on a system call | Seccomp denies it | Find the call and allow it in a custom profile |
| `sudo` or a setuid tool fails | `no-new-privileges` | Remove the need for privilege gain |
Seccomp and capability failures can both appear as Operation not permitted. Seccomp returns the error EPERM, the same one a missing capability produces. To tell them apart, run the same command with --security-opt seccomp=unconfined. If it now works, seccomp was the cause. Use this only as a diagnostic and never keep it.
Worked example: a service that cannot bind port 80
Suppose you start a small web server that listens on port 80, with all capabilities dropped.
docker run --rm \
--cap-drop ALL \
--user 10001:10001 \
python:3.12-slim \
python -m http.server 80The server exits with PermissionError: [Errno 13] Permission denied. Ports below 1024 are privileged. Binding one needs the NET_BIND_SERVICE capability. Two fixes are possible.
The first is to add back only that capability.
docker run --rm \
--cap-drop ALL \
--cap-add NET_BIND_SERVICE \
--user 10001:10001 \
python:3.12-slim \
python -m http.server 80This may still fail for a non-root user. The kernel only gives a non-root process the capability if it is in the process's effective set, and a plain docker run --user does not always arrange that. Recent Docker versions set the sysctl net.ipv4.ip_unprivileged_port_start=0 inside the container namespace by default, which makes the problem go away.
The second fix is simpler and needs no capability: listen on a high port and map it on the host.
docker run --rm \
--cap-drop ALL \
--user 10001:10001 \
-p 80:8080 \
python:3.12-slim \
python -m http.server 8080Prefer the second approach. It leaves the container with an empty capability set.
Find the missing system call
When seccomp is the cause, you need to know which call was blocked. Run the command under strace in a test container, or read the audit log on the host. On a host with auditd, blocked calls appear as type=SECCOMP records.
sudo ausearch -m SECCOMP --start recentEach record has a syscall= number. Translate it to a name with ausyscall.
ausyscall 83Add the name to an SCMP_ACT_ALLOW rule in your custom profile, then rerun. Allow as few calls as you can, and re-test after each addition.
Kubernetes differences
In Kubernetes you do not get an error on the command line. The pod enters CrashLoopBackOff or CreateContainerConfigError. Check the logs and events.
kubectl logs hardened-app
kubectl describe pod hardened-appA common event is container has runAsNonRoot and image will run as root. Set runAsUser to a numeric ID, as the pod example earlier in this tutorial does.
Exercise
A container runs with --read-only --cap-drop ALL. It crashes with Operation not permitted when it tries to write /var/run/app.pid. A colleague suggests adding --cap-add ALL. What is the more likely cause, and what should you do instead?
Show answer
The error on a write to a path is most likely caused by the read-only root filesystem, not a missing capability (a read-only filesystem usually reports Read-only file system, so confirm by rerunning with --security-opt seccomp=unconfined and reading the exact message). If the message says Read-only file system, mount a tmpfs at /var/run with --tmpfs /var/run, or change the application to write its PID file under /tmp. Do not add capabilities.