Adding runAsNonRoot for Restricted Pod Security Stops Containers Whose Image USER Is a Name, Not a UID
Overview
runAsNonRoot passes admission but the kubelet refuses an image whose USER is a name. Give the container a numeric UID before enforcing restricted.
At a glance
- Symptom
- New pods from the updated Deployment stay in CreateContainerConfigError and never start, while the old pods keep serving.
- Likely cause
- When runAsNonRoot is true and runAsUser is not set, the kubelet takes the user from the image and accepts it only if it is…
- Impact
- Each Deployment that gets the field stops rolling out until its pods have a numeric UID, and any pod that is rescheduled can't come back.
- Verification signal
- The rollout completes, the new pods are Running, and the events show no "non-numeric user" error.
- Safe correction
- Give the container a numeric, non-zero UID that matches the image's user, and keep runAsNonRoot: true.
- Rollback or recovery
- If the corrected pods fail for another reason, roll the Deployment back to the revision that was serving before the change: kubectl rollout undo deployment/web -n payments That…
Adding runAsNonRoot: true to a pod’s security context, the usual first change when a namespace moves to the restricted Pod Security Standard, stops any container whose image sets USER to a name such as appuser instead of a numeric UID. The API server accepts the change, because the restricted profile only checks that the field is true. The kubelet then refuses to start the container: it can only prove a user is not root from a number, so a name fails the check. Give the container a numeric UID, either with runAsUser in the pod spec or with USER 10001 in the Dockerfile, and keep runAsNonRoot.
Symptom
New pods from the updated Deployment stay in CreateContainerConfigError and never start, while the old pods keep serving. kubectl describe pods shows a warning event from the kubelet that names the problem exactly:
Error: container has runAsNonRoot and image has non-numeric user (appuser), cannot verify user is non-root (pod: "web-7c9d8f6b5-x2lqp_payments(...)", container: web)
There is no exit code to look up and no log to read, because the container process was never created. That separates it from a CrashLoopBackOff, where the container starts and then fails. With the default rolling update the Deployment controller stops scaling up the new ReplicaSet, so the rollout stalls, and kubectl rollout status waits on new replicas that never become ready.
Why adding the field looks safe
The image already runs as a non-root user, so declaring that in the pod spec looks like writing down a fact. The restricted profile asks for exactly this field, admission accepts it, and the warn label stops reporting that field. The assumption is that the kubelet checks the same thing admission checked. It doesn’t: admission reads the pod spec, and the kubelet reads the image.
Root cause
When runAsNonRoot is true and runAsUser is not set, the kubelet takes the user from the image and accepts it only if it is a non-zero numeric UID. The Kubernetes API reference says that with runAsNonRoot set, the kubelet validates the image at runtime and fails to start the container if it would run as UID 0. A user name cannot be checked that way, so the kubelet’s code returns the “non-numeric user” error and the container is never created; its own comment reads “Non-root verification only supports numeric user”. An image whose Dockerfile says USER appuser stores the name, not the UID, in its configuration, which the OCI image specification allows. An image with no USER at all fails with a different message, “image will run as root”.
If runAsUser is set to a non-zero value, the kubelet uses that number and skips the image check, which is why the same Deployment works the moment a UID is added.
Impact
Each Deployment that gets the field stops rolling out until its pods have a numeric UID, and any pod that is rescheduled can’t come back. The old ReplicaSet keeps the service up for now, but only until something replaces those pods: a node drain, an eviction or the next rollout. A Job or CronJob whose pod template gets the same field never starts its pods at all.
The workaround that gives the protection back
Under pressure the quick fix is to delete runAsNonRoot again, or to relabel the namespace to baseline so the restricted profile no longer asks for it. Both get the pods running and both remove the only runtime check that the container is not root. Dropping the namespace label weakens every pod in it, not just this one, and an exemption has the same effect for whatever it matches, as the trap where Pod Security admission exemptions override namespace enforce labels shows. Setting runAsUser: 0 does not help either: the kubelet rejects it with “container’s runAsUser breaks non-root policy”, and the restricted profile forbids it.
How to confirm it without changing anything
- Read the pod’s events and container state:
kubectl describe pods ${POD_NAME} -n paymentsThe “non-numeric user” message confirms this trap; “image will run as root” means the image sets no user.
- Read the user the image declares. A name such as
appuseris the trap; a number is not:docker image inspect --format '{{.Config.User}}' registry.example.com/web:2.4.1 - Check how far the rollout got:
kubectl rollout status deployment/web -n payments kubectl get rs -n payments - See which pods in the namespace would fail the restricted profile, without changing the label:
kubectl label --dry-run=server --overwrite ns payments pod-security.kubernetes.io/enforce=restricted
Correction
Give the container a numeric, non-zero UID that matches the image’s user, and keep runAsNonRoot: true. The cleanest fix is in the image: change the Dockerfile to USER 10001:10001 (the form USER <UID>[:<GID>]), with the same UID the image’s account was created with, and rebuild. Every pod that uses the image then passes without extra fields. Where you can’t rebuild yet, set the UID in the pod spec instead:
spec:
template:
spec:
securityContext:
runAsNonRoot: true
runAsUser: 10001
runAsGroup: 10001
seccompProfile:
type: RuntimeDefault
containers:
- name: web
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]
Use the order that never leaves the namespace stricter than its pods:
- Precondition: the old ReplicaSet is still serving, and you know the UID the image’s account uses (from the Dockerfile line that creates it, or from the image’s owner). A different UID runs the process as a user who may not own the application’s files.
- Apply the corrected Deployment and watch
kubectl rollout status deployment/web -n paymentsuntil every replica is updated. - Run the server-side dry run of the restricted label again. Only when it returns no warnings for the namespace’s pods set
pod-security.kubernetes.io/enforce=restricted, keeping thewarnandauditlabels atrestrictedas well.
The admission side of this move, pods refused once the label is on, is covered in the runbook for pods rejected after a namespace is set to enforce the restricted standard.
Validation
The rollout completes, the new pods are Running, and the events show no “non-numeric user” error. kubectl rollout status should report that the Deployment was successfully rolled out, and kubectl get rs should show the new ReplicaSet at its full replica count with the old one at zero. Run the restricted dry run once more: it should return no warnings for this Deployment’s pods.
Rollback
If the corrected pods fail for another reason, roll the Deployment back to the revision that was serving before the change:
kubectl rollout undo deployment/web -n payments
That restores the pod template, including the version without runAsNonRoot, so treat it as a holding position and not the fix. If you had already set the namespace to enforce restricted, take enforce back to its previous level before the undo, or the restored pods may be refused at admission; leave warn and audit at restricted so the gap stays visible.
Prevent it in the next image build
Make the numeric USER part of the image build: a base image or build check that rejects a USER line with a name stops the problem before any pod spec is touched. When a namespace is moving to restricted, set the warn and audit labels first and run the server-side dry run, which shows admission problems. It cannot show this one, because the image check only happens on the node, so roll a single Deployment through with runAsNonRoot before changing the rest.
Apply the safer control
Before you change production
Confirm the affected scope, export the current configuration, and test the replacement control in a non-production environment first.
Fix commands and configuration
runAsNonRoot: trueUSERappuserWhen to escalate
Validation and rollback are covered in the sections above.
Escalate
Escalate when the blast radius is uncertain, the control cannot be tested safely, or remediation requires an outage or security exception.
Fixed? Get the next one before it hits you: The KBY Brief
Practical fixes for the systems you run, by email about twice a week. Unsubscribe at any time.
About this page
Credited to
Dates
Published
Corrections
No corrections are listed for this page. Some review sweeps corrected pages without naming each one; those are in the corrections log.
Was this useful?
Reports go to the site owner and are used to correct pages. Corrections are listed at /corrections.