Lesson 8 of 9 · 22m
Runtime Hardening: Capabilities, Seccomp, Read-Only Roots
Default containers are convenient, not safe — drop capabilities, filter syscalls, make the root filesystem read-only, and run as a real user.
A default docker run gives the container fourteen Linux capabilities, access to hundreds of syscalls, a writable root filesystem, and root inside the user namespace. Almost no application needs any of that. Hardening is subtraction — each removal closes an escalation path an attacker would otherwise walk down after popping your app.
The baseline four flags
docker run -d --name web --read-only --cap-drop ALL --cap-add NET_BIND_SERVICE --security-opt no-new-privileges --user 1000:1000 --tmpfs /tmp:rw,noexec,nosuid,size=64m --pids-limit 200 --memory 512m myapp:1.4.2Each flag answers a specific attack:
--read-only: the classic post-exploitation move is writing a webshell or dropping a binary. Can't write what isn't writable. Apps that need scratch space get--tmpfsmounts — in memory, sized,noexec.--cap-drop ALL: capabilities are root sliced into pieces. Your web app needs none ofSYS_ADMIN(mount — kernel exploit surface),NET_ADMIN(iptables),SYS_PTRACE(read other processes' memory, including the host's under some configs). Start from zero and add back only what breaks.no-new-privileges: blocks setuid binaries and file capabilities from escalating — closes the "attacker writes a setuid binary then runs it" loop.--user: not root inside means not root anywhere. Combined with the previous lesson's user namespaces, even a breakout chain has nothing to work with.
Seccomp: fewer syscalls, fewer CVEs that reach you
Every kernel vulnerability is triggered through a syscall. Docker's default seccomp profile already blocks ~44 dangerous ones (mount, ptrace, kexec_load) — that's why the default isn't terrible. A custom profile tightens further for your specific app:
{
"defaultAction": "SCMP_ACT_ERRNO",
"architectures": ["SCMP_ARCH_X86_64"],
"syscalls": [
{ "names": ["read","write","openat","close","socket","connect","accept4","bind","listen","epoll_wait","mmap","brk","exit_group","futex"], "action": "SCMP_ACT_ALLOW" }
]
}docker run --security-opt seccomp=/path/profile.json myappBuilding the exact allowlist is tedious — record what the app actually uses with strace in staging, or accept the default profile plus the other flags. The default already prevented exploitation of several published kernel CVEs in containerized contexts.
What this looks like in Kubernetes
securityContext:
runAsNonRoot: true
runAsUser: 1000
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]
seccompProfile:
type: RuntimeDefaultThat's the restricted Pod Security Standard, and it's enforced cluster-wide in any serious environment. Write your containers to survive it now and you'll never have the "works in Docker, fails in prod" conversation.
Field notes
--read-onlybreaks apps that write PID files or temp files next to their binary. Fix the app, not the flag — writable containers aren't a requirement, they're a habit.--cap-drop ALLand the app still works? Most do. If something breaks, the strace or audit log tells you exactly which capability it wanted — add back the minimum.- Don't expose the docker socket into a container "for monitoring." That's read-write root on the host as a feature. Use the remote API with TLS and scoped auth if you must.
--privilegedis never the answer. It's the absence of an answer — every flag above set to maximum danger at once.
Was this lesson helpful?