Why Containers Should Not Run as Root
Most containers run as root because the image never set a user, so one flaw hands over every secret. How to switch in Dockerfile, docker run and Kubernetes.
Most containers run as root because the image never said otherwise. A Dockerfile without a USER instruction starts its process as UID 0, and that process keeps root inside the container for as long as it runs. When an application flaw gives an attacker code execution, root means they own every file, every environment variable and every mounted secret in that container immediately. The LiteLLM proxy, whose guardrail sandbox escape entered CISA's KEV catalog in November 2026, ships that way by default. Changing it is a one-line fix in the image or the deployment, and it should be a release gate.
Why do containers run as root by default?
Containers run as root by default because the Dockerfile USER instruction is optional and the default user is root. Many official and third-party images never set it, either because the build installs packages that need root or because the maintainers assumed the container boundary was enough.
Docker's documentation describes the instruction plainly: USER sets the user name or UID (and optionally the group or GID) used for the rest of the build stage and for the ENTRYPOINT and CMD at runtime. If it is absent, nothing changes the identity, and the application starts with UID 0. The convenience is real: a root process can bind low ports, write anywhere, and install dependencies at runtime. The cost is that the application's privilege and the container's privilege become the same thing.
What can an attacker do with root inside a container?
Root inside a container can read and modify every file in the container filesystem, including configuration, application code and any secret mounted as a file. It can read every environment variable of every process, which is where API keys usually live, and it can install tools, start listeners and reach whatever network the container can reach.
The container boundary still stands at this point. Turning root inside a container into root on the host needs one more thing: a kernel vulnerability, a privileged container, a mounted Docker socket, or a writable host path. Those are not rare. Kernel privilege escalation bugs reach the KEV catalog regularly, including the Fragnesia Linux kernel flaw used in a root chain in October 2026, and every one of them is far easier to exploit from UID 0 than from an unprivileged user with no capabilities.
Even without a host escape, root in the container is a complete application compromise. For an AI gateway that stores upstream provider keys, virtual keys and a master key, the attacker leaves with every credential the service was built to protect.
The LiteLLM case
X41 D-Sec's advisory for CVE-2026-40217 confirmed that the LiteLLM proxy's default Docker image runs the process as root, and listed running the container as an unprivileged user with the --user flag among the mitigations. The vulnerability itself was an application-level sandbox escape (explained in what a sandbox escape is); the root default decided how bad the outcome was. Had the proxy run as an ordinary user, the same bug would still have exposed the application's secrets, but it would not have handed over the container. Details of the flaw, the patch and the detection steps are in LiteLLM CVE-2026-40217 sandbox escape added to CISA KEV.
How to run a container as a non-root user
There are three places to set the identity. Use the one closest to the image you control, and enforce the others as backstops.
In the Dockerfile. Add a user and switch to it before the entrypoint. Docker's reference gives the two forms:
USER <user>[:<group>]
USER <UID>[:<GID>]
The documentation adds a warning worth reading twice: when the user has no primary group, the remaining instructions run with the root group. Create the user with an explicit group.
At docker run. The --user flag overrides the image's user without rebuilding it. Docker documents the accepted forms:
--user=[ user | user:group | uid | uid:gid | user:gid | uid:group ]
A numeric UID does not need to exist inside the container; a user name does. For a vendor image you cannot rebuild, a numeric UID and GID is the dependable choice.
In Kubernetes. The pod or container securityContext controls the identity. The Kubernetes task documentation uses this shape:
securityContext:
runAsUser: 1000
runAsNonRoot: true
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
runAsNonRoot: true makes the kubelet refuse to start a container whose image would run as UID 0, which turns the policy into an enforced check rather than a convention. allowPrivilegeEscalation: false prevents the process from gaining more privileges than its parent through setuid binaries. The Pod Security Standards Restricted profile requires both runAsNonRoot set to true and runAsUser set to a non-zero value, so applying the Restricted profile to a namespace enforces this for every pod in it.
What breaks when you drop root
Expect three classes of failure, all fixable.
- Ports below 1024. An unprivileged process cannot bind port 80 or 443 directly. Listen on a high port and let the reverse proxy or the Kubernetes Service map it, which is how most deployments already work.
- Volume permissions. Files written by the old root process are owned by UID 0, and the new user cannot write them. Fix ownership on the volume once, and set the user's UID consistently across the Dockerfile, the run flag and the orchestrator so the files stay readable.
- Runtime installs. Images that install packages or write to system paths at startup fail without root. Move those steps into the build, where they belong, and keep the runtime filesystem read-only.
Test the change in staging with the application's full feature set. A proxy that starts fine but fails the first time it writes a log file or a cache is a common outcome.
Why this belongs in the release gate
The decision to run as root is made once, in the image, and inherited by every deployment afterwards. Making non-root a hard requirement at build time, with the Kubernetes runAsNonRoot check or an admission policy as the enforcement point, removes the decision from every future incident. It also changes the severity of the next application flaw. A sandbox escape, deserialization bug or command injection in a process running as UID 1000 is a serious incident contained to that application. The same bug at UID 0 is a container compromise with a straightforward path to the host.
Pair the identity change with the other container hardening settings: a read-only root filesystem, dropped capabilities, no privilege escalation, and a seccomp profile. Each one is a boundary that holds when the application's own protections fail, which is the lesson of every sandbox escape. And when a vendor advisory names a vulnerable endpoint, block that path at the reverse proxy while you patch; the non-root user limits what an attacker gets if they reach it first.