<p>I want to mount a volume in the docker container as a non root user. I am using the following (k8s.yaml) -</p>

<pre><code>volumeMounts:
        - name: volume-to-be-mounted
          mountPath: /location
volumes:
        - name:  volume-to-be-mounted
          hostPath:
            path: path
            type: DirectoryOrCreate
</code></pre>

<p>This volume is mounted as root inside the container. But I want to mount it as non-root. Is there any way of doing this? I can also use the <a href="https://docs.docker.com/storage/volumes/" rel="nofollow noreferrer">https://docs.docker.com/storage/volumes/</a>  but I want to mount the same volume on other container (in the same pod) as well.</p>

<p>Some of the solutions that come to mind but don't suit my use case -</p>

<ol>
<li>change the permissions of the directory in entrypoint (not viable because entrypoint will be run as a non root user.)</li>
<li><a href="https://stackoverflow.com/a/39576814/9081810">https://stackoverflow.com/a/39576814/9081810</a> I am using k8s.yaml to specify my requirements. I don't know how this solution will fit in.</li>
</ol>

<p>Possible solutions that can work but I don't know how to do it -</p>

<ol>
<li>set permissions to 777 while mounting the volume.</li>
</ol>

user2851669
<p>If you're using kubernetes you can use a <a href="https://kubernetes.io/docs/tasks/configure-pod-container/security-context/" rel="nofollow noreferrer">security context</a> and set the <code>fsGroup</code> value.</p>

<p>Example from the docs</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: security-context-demo
spec:
  securityContext:
    runAsUser: 1000
    fsGroup: 2000
  volumes:
  - name: sec-ctx-vol
    emptyDir: {}
  containers:
  - name: sec-ctx-demo
    image: gcr.io/google-samples/node-hello:1.0
    volumeMounts:
    - name: sec-ctx-vol
      mountPath: /data/demo
    securityContext:
      allowPrivilegeEscalation: false

</code></pre>

<p>If you're just using docker ... well there's been an <a href="https://github.com/moby/moby/issues/2259" rel="nofollow noreferrer">open issue</a> since 2013</p>
