Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Docker Security: Non-Root Users, Linux Capabilities, and Seccomp Profiles

Containers start as root with 14 capabilities. How to run non-root, drop ALL caps and add one, keep seccomp, and why port 80 stops working.

~6 min readDocker
#Docker#Security#Containers
3D illustration of a container sandbox reinforced by a non-root privilege wall, dropped capability chips and a seccomp syscall shield

Quick Summary

A container starts as root with a set of default Linux capabilities and the default seccomp profile applied. That is a reasonable starting point and a poor ending point: root inside the namespace can write to any file in the image, CAP_NET_RAW lets it craft packets, CAP_SYS_CHROOT and a writable filesystem give an escape room to work with, and the seccomp default — while it blocks a lot — still allows most syscalls an exploit needs.

Three controls, each cheap, cover most of the exposure. Run as a non-root USER so a compromise lands with unprivileged rights. Drop every capability and add back only what the process actually calls. Keep the seccomp profile at its default and add no-new-privileges so setuid binaries and file capabilities cannot raise the process's rights.

The failure modes are all in the details: a non-root user that cannot bind port 80 any more, a chown that makes a bind-mounted volume root-owned on the host, a clone3 denial that looks like a broken runtime, and docker exec -u 0 quietly undoing the whole design.

Basic syntax and query signatures

dockerfile
FROM node:22-bookworm-slim AS build WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build FROM node:22-bookworm-slim ENV NODE_ENV=production WORKDIR /app COPY --from=build --chown=10001:10001 /app/dist ./dist COPY --from=build --chown=10001:10001 /app/node_modules ./node_modules USER 10001:10001 EXPOSE 8080 ENTRYPOINT ["node", "dist/server.js"]
bash
docker run -d --name api \ --user 10001:10001 \ --cap-drop=ALL \ --cap-add=NET_BIND_SERVICE \ --security-opt no-new-privileges:true \ --security-opt seccomp=/etc/docker/seccomp-api.json \ --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m \ example/api:1.4.2
yaml
# compose.yaml services: api: image: example/api:1.4.2 user: "10001:10001" read_only: true tmpfs: - /tmp:size=64m cap_drop: [ALL] cap_add: [NET_BIND_SERVICE] security_opt: - no-new-privileges:true - seccomp=./seccomp-api.json volumes: - uploads:/var/lib/app/uploads
bash
# verify what the process actually has docker exec api id docker exec api grep -E 'Cap(Eff|Bnd)|Seccomp|NoNewPrivs' /proc/1/status docker exec api capsh --decode=$(docker exec api grep CapEff /proc/1/status | awk '{print $2}') docker inspect -f '{{.HostConfig.User}} {{.HostConfig.CapDrop}} {{.HostConfig.SecurityOpt}}' api

Key comparison table

ControlDefaultHardenedWhat it stops
Userroot (uid 0)USER 10001Writing to image files, root-owned host files
Capabilities14 default caps--cap-drop=ALL + explicit addsRaw sockets, chown, mount-adjacent tricks
Seccompdefault profiledefault, or a tighter custom onemount, kexec, bpf, keyring syscalls
no-new-privilegesOffOnsetuid binaries, file capabilities
Root filesystemWritable--read-only + tmpfsPersisting a payload, editing binaries
User namespaceOffuserns-remapContainer root equalling host root
Docker socketNot mountedNever mountedFull host takeover from inside
Default capabilityWhat it allowsDrop it when
CHOWNchown any fileThe app never changes ownership
DAC_OVERRIDEBypass file permission checksThe app runs as the file owner
FOWNERModify files owned by othersStartup does not fix permissions
SETUID / SETGIDSwitch userNo su/sudo in the container
NET_RAWRaw and packet socketsNo ping, no custom ICMP
NET_BIND_SERVICEBind ports below 1024The app listens on 8080 or higher
SYS_CHROOTchrootNothing legitimate in most apps
MKNODCreate device nodesAlmost always
KILLSignal other users' processesOnly matters with several uids
SETFCAP, SETPCAP, AUDIT_WRITECapability and audit manipulationAlmost always

Practical edge cases and deep dives

1. Non-root and port 80

A process that is not uid 0 has no effective capabilities, so NET_BIND_SERVICE in the container's bounding set does not help unless the binary carries the file capability — and no-new-privileges blocks exactly that:

Error: listen tcp :80: bind: permission denied

The right answer is to listen on 8080 inside the container and publish -p 80:8080; the host's port 80 is bound by the daemon, which is already privileged. The alternatives are --sysctl net.ipv4.ip_unprivileged_port_start=0 (kernel 4.11+, and it lowers the barrier for every process in that namespace) or setcap cap_net_bind_service=+ep on the binary, which no-new-privileges disables. Do not solve it by keeping the app as root.

2. chown at build time versus volume ownership

dockerfile
# correct: ownership baked into the layer, no runtime chown COPY --chown=10001:10001 . /app # wrong: needs root, and fails when USER is already set RUN chown -R app:app /app

A named volume inherits the ownership and permissions of the image's directory the first time it is mounted — so chown in the image is what makes a non-root container able to write to its volume. A bind mount is the opposite: it takes the host's ownership, and a uid 1000 process writing there produces files owned by uid 1000 on the host. If the app must write to a bind mount, align the container uid with the host user, or accept chmod 777, which is worse than the problem it solves.

3. docker exec -u 0 and the socket

bash
docker exec -u 0 -it api sh # root in the container, whatever USER says

Running as non-root is a boundary against a compromised process, not against an operator — the Docker API is root-equivalent on the host. The same logic applies to a mounted socket: a container with /var/run/docker.sock can start a privileged container and own the host, no matter which user it runs as. Treat socket mounts and --privileged as full trust, because they are.

4. Seccomp denials that look like application bugs

OCI runtime create failed: ... operation not permitted

or, more often, a program that dies with EPERM on a syscall it expects to work. The classic case is clone3: glibc 2.34+ uses it for thread creation, and seccomp profiles written before the syscall existed returned EPERM instead of ENOSYS, so the fallback to clone never happened. Updated Docker releases allow clone3 to return ENOSYS. If you maintain a custom profile, copy the current default from the moby repository and add your syscalls to it rather than starting from an old copy.

Build a minimal profile by tracing, not by guessing:

bash
docker run --rm --security-opt seccomp=unconfined --cap-add=SYS_PTRACE \ --entrypoint sh strace-image -c 'strace -f -c -o /tmp/strace.txt node server.js'

Then compare the syscall list against the default profile. Never ship seccomp=unconfined and never --privileged as a fix for a denial: both remove every control at once.

5. Read-only root filesystem and the directories that still need writes

--read-only turns every write into EROFS:

Error: EROFS: read-only file system, open '/app/tmp/upload'

The exceptions are known: /tmp via tmpfs, the app's data directory via a volume, and package managers that insist on writing during startup. Run the image once with --read-only and see what breaks; each failure is either a missing tmpfs or an application that should be writing to a volume. The payoff is large: a compromised process cannot modify binaries or drop a persistence script in the image.

6. User namespaces, and what they do not fix

"userns-remap": "default" in daemon.json maps container uid 0 to an unprivileged host uid (typically 100000), so an escape that lands as root in the container lands as nobody on the host. The cost is that every existing volume and bind mount now has the wrong owner, and images that chown at build time keep working while host directories do not. It also does not apply to --privileged containers and interacts badly with some storage drivers. Enable it deliberately, on a host you can rebuild, not during an incident.

Common mistakes and runtime errors

Error or symptomCauseFix
listen tcp :80: bind: permission deniedNon-root cannot bind privileged portsListen on 8080, publish -p 80:8080
EACCES: permission denied, open '/app/x'USER set, files owned by rootCOPY --chown, or a volume with correct ownership
Files on the host owned by rootContainer wrote as uid 0 to a bind mountRun as the host uid, or use a named volume
EROFS: read-only file system--read-only without tmpfs or a volumeAdd --tmpfs /tmp, mount the data dir
operation not permitted on a syscallSeccomp denialUpdate the profile, or add the syscall
setcap binary still failsno-new-privileges blocks file capabilitiesRemove the dependency, or drop the option knowingly
--privileged in a compose fileConvenience fix left inReplace with explicit cap_add and devices
Container root can write host filesNo userns-remapEnable userns-remap, or run non-root
docker exec -u 0 works despite USERThe API is root-equivalentRestrict socket access; do not rely on USER alone
App cannot write to its own uploads directoryVolume created before chown in the imageRecreate the volume, or fix ownership once

The design that holds up is unglamorous: a non-root USER, cap_drop: [ALL] with at most one or two additions, the default seccomp profile, no-new-privileges, a read-only root with named volumes and tmpfs where writes are needed, and no Docker socket in any container. Verify it with id and /proc/1/status rather than trusting the compose file, because the compose file only describes what you meant.

Try matching browser tools

All tools

Small utilities that fit Docker work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Docker guides

More in Docker

All Docker guides

The guides filed next to this one in Docker, in library order.