CreateContainerConfigError: runAsNonRoot & Image Runs as Root
Fix CreateContainerConfigError when Pod Security Standards Restricted enforces runAsNonRoot: true on a root-default image.
Root Cause
Kubelet reports CreateContainerConfigError with 'container has runAsNonRoot and image will run as root' whenever pod or container securityContext sets runAsNonRoot: true, but the container image metadata specifies USER root (or omits USER) and no numeric runAsUser is set in YAML.
Why It Happens
Note that Kubernetes surfaces this as CreateContainerConfigError, NOT RunContainerError, because kubelet rejects the container configuration before the OCI runtime ever spawns the process. Fixing it does not require weakening Pod Security Standards Restricted: assign a non-zero numeric UID in the Dockerfile or Pod manifest and make sure the process binds an unprivileged port above 1024 (or keeps NET_BIND_SERVICE).
What It Looks Like in kubectl describe
Name: api-7f9-abc
Namespace: shop
Containers:
api:
State: Waiting
Reason: CreateContainerConfigError
Events:
Warning Failed 12s (x3 over 30s) kubelet Error: container has runAsNonRoot and image will run as root (pod: "api-7f9-abc_shop(1234)", container: api)
How to Fix
- Add a numeric USER (for example USER 10001:10001) to the Dockerfile, or set securityContext.runAsUser: 10001 and fsGroup: 10001 in the Pod manifest.
- Configure the service to listen on an unprivileged port such as 8080 instead of 80/443 (or retain NET_BIND_SERVICE under capabilities.add).
- Mount an emptyDir volume at /tmp or cache directories when readOnlyRootFilesystem: true is also enabled.
Diagnostic Commands
kubectl get pod <pod> -n <ns> -o jsonpath='{.spec.securityContext}'
kubectl describe pod <pod> -n <ns>