Non-Root Containers & Image Scanning
Harden containers with numeric non-root UIDs, read-only root filesystems, dropped Linux capabilities, minimal base images, and CI vulnerability scanning.
TL;DR
Default containers run as root with a writable root filesystem and extra Linux capabilities. Run as a numeric non-root UID, mount the root filesystem read-only (with --tmpfs for /tmp), drop ALL capabilities, pick Alpine or Distroless, and gate CI with Trivy or Grype.
Why it exists
A container shares the host Linux kernel. If a web framework RCE gives an attacker a shell inside a container running as UID 0, they own every writable path in the container and hold default kernel capabilities—turning a minor app bug into a much easier container escape or lateral-movement pivot.
Mental model
Container hardening works in four independent layers:
| Layer | Control | What it blocks |
|---|---|---|
| Identity | USER 1001:1001 (numeric UID/GID) | Root ownership inside the container; satisfies Kubernetes runAsNonRoot: true |
| Filesystem | --read-only + --tmpfs /tmp | Runtime binary tampering and webshell drops on / |
| Kernel surface | --cap-drop=ALL (--security-opt=no-new-privileges) | Privileged syscalls and setuid escalation |
| Image contents | Multi-stage + Alpine/Distroless + Trivy/Grype | Unneeded shells, compilers, and known OS/library CVEs |
Distroless vs Alpine
- Alpine (
node:22-alpine3.21): ~5 MB base withmusllibc,busybox,sh, andapk. Easy to debug (docker exec -it ... sh), thoughmuslcan occasionally differ fromglibcfor native C/C++ addons. - Distroless (
gcr.io/distroless/nodejs22-debian12):glibc-based with no shell and no package manager. Smaller CVE count and harder for an attacker to run post-exploitation commands, at the cost of needing ephemeral debug containers (kubectl debug) for troubleshooting.
Do it
1. Numeric non-root USER in the Dockerfile
Look at labs/docker-multistage-next/Dockerfile in this repo—it creates nextjs at UID 1001 and switches before CMD:
# Snippet adapted from labs/docker-multistage-next/Dockerfile
RUN addgroup -g 1001 -S nodejs \
&& adduser -S nextjs -u 1001 -G nodejs
COPY --from=deps --chown=nextjs:nodejs /app/node_modules ./node_modules
COPY --chown=nextjs:nodejs server.mjs ./
USER 1001:10012. Run with read-only rootfs and dropped capabilities
# not run (example runtime hardening flags)
docker run --rm -p 3000:3000 \
--read-only \
--tmpfs /tmp:rw,noexec,nosuid,size=64m \
--cap-drop=ALL \
--security-opt=no-new-privileges:true \
mt-multistage-next:lab3. Scan the built image in CI with Trivy or Grype
Trivy scans both OS packages (apk/dpkg/rpm) and language lockfiles (package-lock.json, node_modules) for known CVEs and accidentally baked secrets:
# not run (trivy/grype not installed on this workstation)
docker build -t mt-multistage-next:lab labs/docker-multistage-next
trivy image --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 mt-multistage-next:labIn GitHub Actions, fail the build on fixable HIGH or CRITICAL vulnerabilities, or upload SARIF to the repository Security tab so every CVE is tracked without blocking unrelated PRs during an upstream outage.
When you DON'T need this
- Static sites with no container runtime: This site builds to static assets served on an edge network—there is no Linux container in production to harden.
- Ephemeral local build/test containers: During local unit tests or throwaway
docker buildstages,--read-onlyand--cap-drop=ALLadd friction without meaningful risk reduction. - Cost / complexity trade-off: Numeric
USER+--cap-drop=ALLare nearly free once set up. Distroless and strict--read-onlycost debugging convenience (nosh, explicittmpfsmounts for Next.js.next/cacheor/tmp), and strict Trivy--exit-code 1gates can block urgent deploys when a zero-day CVE has no upstream patch yet (mitigate with--ignore-unfixed).
Gotchas
USER nextjswithout a numeric UID in Kubernetes:runAsNonRoot: truefails withCreateContainerConfigErrorbecause kubelet cannot verify a username maps to a non-zero UID without inspecting/etc/passwd. Always useUSER 1001:1001or setrunAsUser: 1001in the PodsecurityContext.- Apps that write to
/tmpor cache dirs under--read-only: Next.js, Nginx, and Node tools often write temp files; mount explicit writabletmpfsoremptyDirvolumes for those paths. - Scanning without
--ignore-unfixed: Failing CI on unpatched OS CVEs halts releases when there is literally no upgrade available.
Production checklist
- Final stage sets a numeric
USER <uid>:<gid>(not0). - Runtime drops
ALLLinux capabilities and enablesno-new-privileges. - Root filesystem is read-only, with explicit
tmpfs/emptyDirmounts for/tmpor cache paths. - Base image is minimal (
alpine,slim, ordistroless) and pinned by tag or digest. - CI scans every built image with Trivy or Grype on push and on a nightly schedule (new CVEs appear after build time).
Check yourself
Check Yourself
1. Why does Kubernetes reject a container with USER appuser when runAsNonRoot: true is set?
2. Why add --ignore-unfixed when gating a CI pipeline with trivy image --exit-code 1?