Fairwinds | Blog

Should Containers Run as Root in Kubernetes?

Written by Adam Zahorscak | Dec 1, 2026, 6:09:36 PM

Almost never. Most applications don't need root privileges, and a container running as root gives an attacker far more to work with if the container is compromised. Set runAsNonRoot: true and a specific non-root runAsUser in each workload's security context, build images that run as a non-root user, and reserve root for the small set of system components that genuinely need it.

It's an old piece of advice that still matters, because many container images still default to root. Unless someone changes it, a container built from one of those images runs as root in your cluster too. This guide covers why that's risky, the few real exceptions, and how to find and fix root containers across a cluster.

Why Is Running as Root Risky?

Containers share the host's Linux kernel. By default, the root user inside a container is the same user ID as root on the node. Container isolation keeps that root user fenced in most of the time, but if anything breaks the fence, root inside the container becomes much closer to root on the node.

That matters in a few concrete ways:

  • Container escapes are worse. If an attacker exploits a vulnerability that lets them break out of the container, they arrive on the node with root's power instead of an ordinary user's.
  • Misconfigurations compound. Root combined with another risky setting, such as a mounted host path, added Linux capabilities, or privilege escalation left enabled, can give a container real control over the node.
  • Compromised apps can do more inside the container. An attacker who takes over an app running as root can install tools, change files, and tamper with the container in ways a non-root process can't.

The NSA's Kubernetes Hardening Guide specifically advises running applications as non-root users for these reasons, as we covered in our tutorial on keeping containers from running as root. Running as non-root doesn't make a container secure by itself, but it shrinks what goes wrong when something else fails.

When Does a Container Need Root?

A few kinds of workloads have a real case for root, and a few common reasons turn out to have better alternatives.

Real exceptions:

  • Node-level system components. Some networking plugins, storage drivers, or node-level monitoring and logging agents need host access to do their jobs. Keep them in dedicated system namespaces, document why each one needs root, and exempt them from your policies explicitly instead of loosening the policy for everyone.
  • Tools that need root inside the container. Some image build tools and legacy software expect to be root. For these, configuring user namespaces (below) can give them root inside the container without root on the node.

Reasons with better alternatives:

  • "It needs to listen on port 80 or 443." Run the app on a higher port and let the Service map the port. If you are on a very current version of containerd or really know what you are doing, you could also grant only the NET_BIND_SERVICE capability instead of full root.
  • "It needs to own files on a mounted volume." Set fsGroup in the pod's security context so Kubernetes handles volume permissions, instead of running a root init container to change ownership.
  • "The image runs as root by default." Rebuild it with a non-root USER, or set runAsUser in the workload. Many popular images publish non-root variants.

What about user namespaces?

User namespaces, generally available since Kubernetes 1.36, let a container run as root inside while mapping that root user to an unprivileged user on the node. You turn them on by setting hostUsers: false in the pod spec. That makes a container escape far less damaging, and it's a good fit for workloads that genuinely need root inside the container.

They have limits, though. Pods using them can't share the host's network, process, or IPC namespaces, some storage setups don't work with them yet, and every container on the node still shares the same kernel. User namespaces are a strong extra layer, and running as non-root is still the right default.

How Do I Run Containers as Non-Root?

Start in the image, then enforce it in Kubernetes.

In the image, add a USER instruction to the Dockerfile with a numeric user ID, such as USER 10001. Use a number rather than a name: when runAsNonRoot is set, Kubernetes can only verify a numeric ID, and it refuses to start a container whose image specifies the user by name unless the workload also sets a numeric runAsUser.

In the workload, set a security context that requires non-root and removes the other privileges most apps never use:

securityContext:

runAsNonRoot: true

runAsUser: 10001

runAsGroup: 10001

allowPrivilegeEscalation: false

readOnlyRootFilesystem: true

capabilities:

drop: ["ALL"]

seccompProfile:

type: RuntimeDefault

With runAsNonRoot: true, the kubelet refuses to start the container if it would run as root, so a misconfigured image fails loudly instead of quietly running with full privileges. The other settings close off the usual paths a process takes to regain privileges.

At the namespace level, Kubernetes's built-in Pod Security Admission can enforce the restricted Pod Security Standard, which enforces most of these settings, though not a read-only root filesystem. Label a namespace to apply it, and Kubernetes rejects pods that don't comply. Start in warn or audit mode to see what would fail before you enforce it.

Node level workaround

Fairwinds regularly deploys applications that would otherwise need root privileges or need to create additional user namespaces, by enabling user namespaces in Bottlerocket only on dedicated EC2NodeClasses. These node pools are tainted so only approved workloads can schedule there. This lets us host a wide range of applications in a client cluster while keeping tight control over third-party application security configuration and ensuring other workloads don’t have this ability.

For example, this node class hosts a legacy application that isolates git builds in subprocesses and is configured with hostUsers: false:

userData: |

[settings.kernel]

lockdown = "integrity"

[settings.host-containers.admin]

enabled = false

[settings.host-containers.control]

enabled = false

[settings.kernel.sysctl]

"user.max_user_namespaces" = "16384"

How Do I Find Root Containers Across a Cluster?

In a cluster with more than a handful of workloads, checking security contexts by hand doesn't scale. Open source Polaris checks every workload against a set of configuration policies, including whether containers are allowed to run as root, run as privileged, or allow privilege escalation. Its runAsRootAllowed check fails any container that doesn't set runAsNonRoot: true.

You can run Polaris in three ways, depending on where you want to catch problems:

  • As a dashboard, to see which workloads in a running cluster are allowed to run as root today.
  • From the command line in CI, to check manifests before they're deployed.
  • As an admission controller, to reject non-compliant workloads at deploy time, or automatically add the missing settings.

For existing manifests, polaris fix can add the missing security context for you, as described in Kubernetes Mutations with Polaris.

A practical rollout looks like this: run the audit, fix the easy cases first (usually images that just need a USER line or a runAsUser), document and exempt the system components that truly need root, then turn on enforcement so new workloads can't add to the list.

If you want this audit running continuously across every cluster, with results tracked over time, Fairwinds Insights runs Polaris alongside its other checks in one place. For more on related settings, see How You Can Avoid Common Kubernetes Misconfigurations & Vulnerabilities.

Frequently Asked Questions

Do Kubernetes containers run as root by default?

They run as whatever user the image specifies, and many images default to root. Kubernetes only changes this when the workload's security context or a namespace policy says otherwise.

What does runAsNonRoot do?

It tells the kubelet to refuse to start a container that would run as root. It doesn't choose a user for you, so pair it with a non-root USER in the image or a numeric runAsUser in the workload.

Is a privileged container the same as running as root?

No it isn’t, and using privileged containers is less secure than running as root. A privileged container gets nearly all of the host's capabilities and device access, close to having root on the node itself. Running as root without privileged mode is still risky, but less so.

Do user namespaces make running as root safe?

They make it much safer. With hostUsers: false, root inside the container maps to an unprivileged user on the node, so a container escape doesn't hand over node level root. But containers still share the node's kernel, and some features don't work with user namespaces yet, so non-root remains the better default.

How do I check whether a running container is root?

Run kubectl exec <pod-name> -- id. A uid of 0 in the output means the process is running as root. To check a whole cluster at once, use an audit tool like Polaris.