Interview Prep/Docker/Image Security & Hardening

Why should containers not run as root, and how do you enforce it?

BeginnerProductionImage Security & Hardening
Answer

Root in a container is still UID 0 against the shared kernel; any escape bug or writable host mount turns into host-level damage. In production we create a numeric non-root user in the Dockerfile and also enforce it at runtime.

RUN groupadd -g 10001 app && useradd -u 10001 -g app -s /usr/sbin/nologin app
USER 10001:10001

On Kubernetes add runAsNonRoot: true; with plain Docker use --user 10001:10001.

All Docker questions Roadmap: Docker & containers

More on Image Security & Hardening