Question: Pod Fails Due to SecurityContext - runAsNonRoot#2
Question: Pod Fails Due to SecurityContext - runAsNonRoot#2felix-codexyz wants to merge 12 commits intokubeskills:mainfrom
Conversation
… won't run due to runAsNonRoot error
Reconcile local and remote
| I expected the image to run securely, but it defaults to root. What’s the best practice when securing containers without customizing the image manually? | ||
|
|
||
| ## My confusion: | ||
| I understand that security is a priority but what is the use of security when it prevents pods from running? |
There was a problem hiding this comment.
Great question! I was hoping someone would catch this :)
The reason this happens is that the nginx:latest image runs as UID 0 (root) by default.
When runAsNonRoot: true is set, Kubernetes enforces that the container must not run as UID 0. If the image does not specify a non-root user and defaults to root, the pod will fail to start—even if the container does not request elevated privileges.
In images built this way, you can’t override the behavior cleanly unless you explicitly set a different runAsUser. However, this may still fail if the container process requires root privileges or lacks appropriate file ownerships.
For production environments, it’s best to avoid the official nginx:latest image and instead use a hardened or unprivileged variant designed for non-root execution (e.g., nginxinc/nginx-unprivileged).
There was a problem hiding this comment.
Thank you Chad, your suggestion fixed the issue.
Image: nginx:latest has ben replaced by: nginxinc/nginx-unprivileged
The pods are now all in the running state.
Now to the next steps!
🧠 GROW Notebook Contribution or Question
Thanks for contributing to your student notebook or reaching out with a question!
Please complete the relevant section(s) below.
📝 Suggested PR Title
✍️ What is this PR for?
📝 Description
📎 Related Files
🙋♂️ What do you need help with?