<p>How can I give the non-root user full access to the mounted volume path in Kubernetes (pod)?</p>

<p>I'm using a volume on the host (/workspace/projects path) and writing to the directory as below.</p>

<pre><code>volumeMounts:
-name: workspace
 mountPath: /workspace/projects
</code></pre>

<p>Since, I'm copying the git repository content to the <strong>/projects</strong> directory, the git sets the permission to 755 by default. I want to set the permission to 775 as unable to write to the <strong>/project</strong> directory.</p>

<p>Could you please let me know what is the best way to do this? I saw <strong>InitContainers</strong> and not sure whether there is any better solution.Appreciate any help! Thanks in advance!</p>

testbg testbg
<p>When you have to run process inside container as non-root user and you mount a volume to pod. But the volume have <code>root:root</code> permission.</p>

<p>To give access to specific user initContainer is one way, like following</p>

<pre><code>initContainers:
- name: volume-mount-permission
  image: busybox
  command: ["sh", "-c", "chmod 775 /workspace/projects &amp;&amp; chown -R &lt;user&gt; /workspace/projects"]
  volumeMounts:
  -name: workspace
   mountPath: /workspace/projects
</code></pre>

<p>You can also use <a href="https://kubernetes.io/docs/tasks/configure-pod-container/security-context/" rel="nofollow noreferrer">security context</a>. Create user and group, add user to the group in Dockerfile and set following in spec</p>

<pre><code>spec:
  securityContext:
    runAsUser: &lt;UID&gt;
    runAsGroup: &lt;GID&gt;
    fsGroup: &lt;GID&gt;
</code></pre>
