<p>I just created image using Docker file and for changing user I just used:</p>

<pre><code>USER myuser
</code></pre>

<p>We are using a directory to store data, we change that directory permission using:</p>

<pre><code>chown -R myuser:myuser /data-dir
</code></pre>

<p>This Docker file is for etcd, where we want /data-dir use by etcd to store data. Now, we map the /data-dir to efs volume using kubernetes yml file.</p>

<p>With the below code:</p>

<pre><code>volumeMounts:
        - name: etcdefs
          mountPath: /data-dir
      volumes:
      - name: etcdefs
        persistentVolumeClaim:
          claimName: efs-etcd
</code></pre>

<p>After this, I expect, that mapped directory /data-dir should have permission as myuser:myuser but it making the directory as root:root</p>

<p>Can any one suggest what I am doing wrong here ?</p>

<p>This is because of docker. It mounts volume with only root permission and you can change it with <code>chmod</code> but only after the container is started.</p>

<p>You can read more about it here <a href="https://github.com/moby/moby/issues/2259" rel="noreferrer">https://github.com/moby/moby/issues/2259</a>
This issues is here for a long time.</p>

<p>What you can do in kubernetes is use <code>fsGroup</code> and <strong>force</strong> that volume is writable by GID specified. This is working solution and documented as well. More information here <a href="https://kubernetes.io/docs/tasks/configure-pod-container/security-context/" rel="noreferrer">https://kubernetes.io/docs/tasks/configure-pod-container/security-context/</a></p>

<p>Here is an example deployment:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: alpine
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: alpine
    spec:
      securityContext:
        fsGroup: 1000
      containers:
        - name: alpine
          image: alpine
          volumeMounts:
              - mountPath: /var/alpine
                name: alpine
      volumes:
        - name: alpine
          awsElasticBlockStore:
            volumeID: vol-1234567890
            fsType: ext4
</code></pre>
