<p>How can I make every container run as non-root in Kubernetes?</p>
<p>Containers that do not specify a user, as in this example, and also do not specify a SecurityContext in the corresponding deployment, should still be able to be executed in the cluster - but without running as root. What options do you have here?</p>
<pre><code>FROM debian:jessie
RUN apt-get update &amp;&amp; apt-get install -y \
    git \
    python \
    vim
CMD [&quot;echo&quot;, &quot;hello world&quot;]
</code></pre>
<pre><code>---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world
  namespace: mynamespace
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
        - image: hello-world
          name: hello-world
</code></pre>

<p>you can add Pod Security Policy to your cluster, there is an option (below) you can add to prevent any deployment from running without specifying a non-root user:</p>
<pre><code>spec:
  runAsUser:
    rule: MustRunAsNonRoot
</code></pre>
<p>for more info about Pod Security Policy  please go to this link:
<a href="https://kubernetes.io/docs/concepts/security/pod-security-policy/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/security/pod-security-policy/</a></p>
