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.
More on Image Security & Hardening
What is a distroless image and what are the trade-offs?BeginnerHow do you scan an image with Trivy and fail a CI build on serious findings?BeginnerHow do you manage Trivy false positives and accepted risks?IntermediateWhich `docker run` flags make up a hardened runtime baseline?BeginnerWhat does `no-new-privileges` protect against?BeginnerHow should secrets be handled in container workloads?Beginner