A container gives weaker isolation than a virtual machine. It is a process on the host kernel wearing a set of namespaces and cgroups. So when that process runs as root, which it does by default, you are one kernel bug or one misconfiguration away from root on the host. Running as root remains the most common container hardening failure because nothing forces you to fix it and everything works until it does not.
When you write a Dockerfile with no USER instruction, the resulting container runs its main process as UID 0. That UID is the same UID 0 the kernel knows on the host; user namespaces, which would remap it, are off by default in most runtimes. Inside the container the process has a restricted capability set, but a restricted capability set is still a set, and the attack surface it shares with every other tenant is the host kernel itself.
What root inside a container actually buys an attacker
Suppose an attacker achieves remote code execution in your application, such as a deserialization bug or an SSRF that reaches an internal admin endpoint. If the process is root, they inherit its capabilities. They can write to any bind-mounted host path the container was granted, read secrets mounted into the filesystem, install tooling, and probe for a kernel or runtime flaw to break out entirely. Container escapes such as the 2019 runc overwrite (CVE-2019-5736), which needs the attacker to run commands as root inside a container, show why root in the container matters. The 2024 'Leaky Vessels' runc flaw (CVE-2024-21626) is a separate container escape that a non-root user does not fix on its own, so patch the runtime too.
Run the same process as an unprivileged UID and most of that collapses. The attacker cannot write to root-owned host mounts, cannot bind low ports, and hits permission denials on the very syscalls an escape needs. This does not guarantee safety, but it removes the cheapest paths first.
Fixing it in the image and at admission
- Set a numeric
USERin the Dockerfile. Prefer a high, explicit UID (for exampleUSER 10001) over a username, so admission policies that checkrunAsNonRootcan evaluate it without resolving/etc/passwd. - Enforce it in the pod spec. Set
securityContext.runAsNonRoot: trueand a concreterunAsUser; the kubelet will refuse to start a container that tries to run as UID 0. - Drop capabilities to the floor. Add
capabilities.drop: ["ALL"]and add back only what the workload provably needs. Most web services need none. - Make the root filesystem read-only.
readOnlyRootFilesystem: trueplus explicitemptyDirwritable mounts stops an attacker planting a binary where the app will re-execute it. - Block privilege escalation.
allowPrivilegeEscalation: falseprevents a setuid binary inside the image from regaining capabilities the runtime dropped.
RUN adduser -D -u 10001 app
USER 10001
# pod spec
securityContext:
runAsNonRoot: true
runAsUser: 10001
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities: { drop: [ "ALL" ] }
Why it persists
Root-by-default survives because of friction. A base image expects to write to /var; an init script wants to chown; a legacy service binds port 80. Each is solvable: pre-create writable directories, bind a high port and remap it at the service, or use file capabilities instead of full root. Each takes a few minutes, and 'it works' dulls the urge to fix it. Make non-root the default your platform ships, so every new image inherits it and opting back into root is an exception that gets reviewed.
None of this helps the workloads you do not know are running. A team that spun up a container on a forgotten node, as root, with a host mount, is exactly the blind spot an attacker finds first, and you cannot harden what you have not discovered.