Skip to content

Question: Pod Fails Due to SecurityContext - runAsNonRoot#2

Open
felix-codexyz wants to merge 12 commits intokubeskills:mainfrom
felix-codexyz:main
Open

Question: Pod Fails Due to SecurityContext - runAsNonRoot#2
felix-codexyz wants to merge 12 commits intokubeskills:mainfrom
felix-codexyz:main

Conversation

@felix-codexyz
Copy link

@felix-codexyz felix-codexyz commented Jul 9, 2025

🧠 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

Format your PR title like one of the following:

  • lab: add nginx pod with securityContext
  • reflection: week2 submission
  • question: FluxCD error in apply

✍️ What is this PR for?

  • I’m updating or adding new lab work
  • I’m submitting a reflection or weekly journal
  • I have a question for a KubeSkills instructor

📝 Description


📎 Related Files


🙋‍♂️ What do you need help with?

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?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants