Docker container hardening
How to shrink what a compromised process can do inside a container.
A container is just a process
There is no separate machine. A container is a regular host process, running on the same kernel as everything else. What makes it look isolated are a few filters the kernel applies around it. If someone breaks into the app, the possible damage is exactly what those filters let through.
Capabilities: root in pieces
Linux does not treat root as a single block. Its powers are split into some forty capabilities: changing a file's owner (CHOWN), opening a raw socket (NET_RAW), mounting file systems (SYS_ADMIN) and so on. Every process has a bounding set, which is the ceiling: nothing outside it can be gained, not even by running a setuid binary.
Docker already cuts a lot by default and hands out 14. The recipe is to cut down to zero and give back only what a service provably uses. Hover over a square to see its name.
CapBnd shown in /proc/1/status.Two things help a lot here. Running as a non-root user makes the process finish execve with its effective capabilities zeroed. And the no-new-privileges flag ensures no setuid binary hands privilege back later.
Namespaces: what the process sees
A namespace gives the process its own version of a system resource: its own process list, its own network, its own hostname. Linux has eight kinds. Docker creates a new one of each for the container, except two: user and time. And the cgroup one is only new on hosts with cgroup v2; on cgroup v1 the default is to use the host's.
Seccomp: which syscalls exist
Every action of a process becomes a syscall, a request to the kernel. Seccomp puts a small filter in front of each one and decides: let it through, return an error, log it or kill the process. The filter is inherited by every child process and can never be removed.
Docker's default profile denies everything by default and allows about 300 syscalls. Left out are the ones that show up in known escapes, such as mount, unshare, kexec_load, bpf and keyctl. The most important rule is simple: never turn it off (seccomp=unconfined).
Cgroups: how much it can spend
The three filters above limit what the process can do. Cgroups limit how much it can consume: number of processes (against fork bombs), memory and CPU. Without them, a bug in one container takes down every other container on the same host.
In what order to apply them
You can do it all at once, but then, when something breaks, nobody knows what it was. The order below starts with what gives the most protection per hour of work and leaves for last what breaks the most. Each step is a separate change, closed by the audit gate.
1. The base block
The highest-impact recipes fit in a reusable YAML snippet at the top of the compose file. Each service pulls in the block and adds only its own limits. In the image, the process runs with a numeric uid that is not 0.
# Dockerfile, final stage
USER 10001:10001
# docker-compose.yml
x-hardened: &hardened
cap_drop: [ALL] # bounding set empty
security_opt:
- no-new-privileges:true # setuid never grants anything
ipc: private
services:
app:
<<: *hardened
pids_limit: 512 # threads count as tasks
mem_limit: 1g
cpus: "1.5"Official images that start as root and switch user on their own (Postgres, Redis, nginx) need a short list back, always with a comment saying why:
| Service | cap_add |
|---|---|
| Your own non-root app | none |
| Official Postgres | CHOWN DAC_OVERRIDE FOWNER SETUID SETGID |
| Official Redis | SETUID SETGID |
| Official nginx on port 80 | CHOWN SETUID SETGID (or nginx-unprivileged with none) |
2. Docker socket and privileged
Mounting /var/run/docker.sock into a container gives it control over all of Docker. Whoever talks to the socket can create a new, privileged container with the host disk mounted. That is root on the host, and the :ro on the mount changes nothing, because it only protects the file, not the API. When a service only needs to read the container list, the way out is a proxy that allows just that read.
DISABLE_IPV6=1, or it will not even start.The same reasoning applies to privileged: true, which turns on every capability the host has and turns off seccomp. The swap is for the exact list of cap_add and devices the service documents.
3. Namespaces and separate networks
First, no network_mode: host, pid: host or ipc: host on an app service. Then, the network. On a host with several projects behind the same proxy (Dokploy does this with the dokploy-network), everything on the proxy network sees everything. The rule: only what receives traffic from the proxy joins its network, and databases stay on an internal network with no route out.
4. Read-only rootfs
With read_only: true, nobody can write a new binary or change the image code while the container runs. The only writable places become the ones you declare: tmpfs (gone on restart) for cache and temporary files, and volumes for what needs to last.
EROFS shows up in the log, the fix is a tmpfs for that exact path. Never turn read-only off. app:
<<: *hardened
read_only: true
tmpfs:
- /tmp:size=128m,mode=1777
- /app/tmp:size=256m,uid=10001,gid=10001,mode=0755
volumes:
- uploads:/app/storageThree details that only show up when testing:
- tmpfs counts as container memory, so every tmpfs gets a
size=that fits insidemem_limit. - Docker mounts tmpfs with
noexecby default: you can write, but not execute. Only an app that extracts and runs code in/tmp(native modules, JIT) needsexecon that tmpfs, with a comment saying why. - A new volume inherits the owner of the folder that already exists in the image. If the folder does not exist, Docker creates it as root and user 10001 gets
Permission denied. That is why the folder is born in the Dockerfile, beforeUSER:
RUN mkdir -p /app/storage && chown 10001:10001 /app/storage
USER 10001:10001And logs go to stdout, not to a folder.
5. Custom seccomp
For small, stable services (a Go or Rust binary, a job, a proxy) you can go beyond the default profile and allow only the syscalls they really use. To find out which ones, you observe first, and only then block.
default.json, never from scratch.auditd, the events go to the kernel log, which is rate limited and throws most of them away. In one test, only 10 of about 580 events reached the log. Turn on auditd (and read with ausearch -m SECCOMP) or lift the limit during the window with sysctl -w kernel.printk_ratelimit=0. And keep in the allowlist the syscalls that show up with exe="/runc": runc installs the filter before starting the app, so its last steps also go through the profile.6. User namespaces
The last step deals with the namespace Docker does not create: the user one. With userns-remap on the daemon, uid 0 inside the container becomes a high uid with no power at all on the host.
overlay2 driver. That is why it comes last, tried first on a test host.Prove, don't assume
Reading the compose file tells you what should be running. What counts is the state of the live container. The kernel publishes it in /proc/1/status and Docker in docker inspect. A short script joins the two into one line per container, and that line is the gate that closes each recipe: without it, the work is not done.
# scripts/hardening-audit.sh, run on the Docker host
for c in $(docker ps -q); do
st=$(docker exec "$c" cat /proc/1/status)
# distroless (no cat): read it from the host instead
[ -n "$st" ] || st=$(cat /proc/$(docker inspect -f '{{.State.Pid}}' "$c")/status)
# Uid, CapBnd, NoNewPrivs, Seccomp from the kernel,
# ReadonlyRootfs, PidsLimit, Privileged, NetworkMode from docker inspect
doneReading the line
Pick a container and see how each field is judged, and which recipe fixes each failure.
/myproject-job-1 uid=10001 capbnd=0000000000000000 nnp=1 seccomp=2 ro=true pids=128 priv=false net=myproject_backend| Field | Value | Result | Next step |
|---|---|---|---|
uid | 10001 | PASS | — |
capbnd | 0000000000000000 | PASS | — |
nnp | 1 | PASS | — |
seccomp | 2 | PASS | — |
ro | true | PASS | — |
pids | 128 | PASS | — |
priv | false | PASS | — |
net | myproject_backend | PASS | — |
All green: ready. Non-root user, no capabilities, no-new-privileges, seccomp in filter mode, read-only rootfs and a process limit.
/myproject-app-1 uid=0 capbnd=00000000a80425fb nnp=0 seccomp=2 ro=false pids=<nil> priv=false net=myproject_default| Field | Value | Result | Next step |
|---|---|---|---|
uid | 0 | FAIL | baseline-service |
capbnd | 00000000a80425fb | FAIL | baseline-service |
nnp | 0 | FAIL | baseline-service |
seccomp | 2 | PASS | — |
ro | false | FAIL | readonly-rootfs |
pids | <nil> | FAIL | baseline-service |
priv | false | PASS | — |
net | myproject_default | PASS | — |
5 failures. Each one points to a recipe, applied in a separate change and closed by this same gate.
/myproject-metrics-1 uid=0 capbnd=000001ffffffffff nnp=0 seccomp=0 ro=false pids=<nil> priv=true net=host| Field | Value | Result | Next step |
|---|---|---|---|
uid | 0 | FAIL | baseline-service |
capbnd | 000001ffffffffff | FAIL | socket-privileged |
nnp | 0 | FAIL | baseline-service |
seccomp | 0 | FAIL | socket-privileged |
ro | false | FAIL | readonly-rootfs |
pids | <nil> | FAIL | baseline-service |
priv | true | FAIL | socket-privileged |
net | host | FAIL | namespace-isolation |
8 failures. Each one points to a recipe, applied in a separate change and closed by this same gate.
What counts as PASS
| Field | Where it comes from | PASS when |
|---|---|---|
uid | Uid: in /proc/1/status | not 0 (your own images) |
capbnd | CapBnd: | 0000000000000000 or the justified list |
nnp | NoNewPrivs: | 1 |
seccomp | Seccomp: | 2 (filter mode) |
ro | HostConfig.ReadonlyRootfs | true on stateless services |
pids | HostConfig.PidsLimit | has a value |
priv | HostConfig.Privileged | false |
net | HostConfig.NetworkMode | not host |
Negative tests
Besides reading the state, each recipe has a test that has to fail. It is the proof that the filter really is there:
docker compose exec -u 0 app chown nobody /tmpfails withOperation not permitted, because not even root has a capability left.docker compose exec app touch /app/xfails withRead-only file system.docker compose exec app unshare -r truefails, because seccomp blocks creating namespaces.- From inside postgres,
wget -T 3 https://example.comdoes not get out, because the backend network has no route. - From a socket proxy client,
docker run alpine truegets a 403.
