<p>l want to launch a container with non-root user, but l cannot modify the origin Dockerfile, Or l know l can do something like <code>Run useradd xx</code> then <code>User xx</code> in Dockerfile to achieve that.
What l am doing now is modifying the yaml file like the following:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-pod
    image: xxx
    command:
        - /bin/sh
        - -c
        - |
          useradd xx -s /bin/sh;
          su -l xx; // this line is not working
          sleep 1000000;

</code></pre>
<p>when l exec into the pod, the default is still the root user, anyone can help with that? Thanks in advance!</p>

<p>You need to use security context as like below</p>
<pre><code>   securityContext:
     runAsUser: 1000
     runAsGroup: 3000
     fsGroup: 2000
</code></pre>
<p><strong>Reference:</strong> <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#podsecuritycontext-v1-core" rel="nofollow noreferrer">https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#podsecuritycontext-v1-core</a></p>
<p><a href="https://i.stack.imgur.com/eFywb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eFywb.png" alt="enter image description here" /></a></p>
<hr />
<blockquote>
<p>EDIT:</p>
</blockquote>
<p>If you wanted to change the user in you container than, you can add extra layer of dockerfile, check below</p>
<p>Add dockerfile layer,</p>
<pre><code>   FROM &lt;your_image&gt;
   RUN adduser newuser
   USER newuser
   :       
   :
</code></pre>
<p>Now use above custom image in your kubernetes.</p>
