Answers: <p>I had a similar problem, this worked for me:</p>
  
  <ol>
  <li><p>Write Docker file with:</p>
  
  <pre><code># Create app layer:
  FROM python:3.4
  # Create app user &amp; group "testuser" with IDs:
  RUN groupadd -r testuser --gid 1234 &amp;&amp; useradd -d /home/testuser -ms /bin/bash -r -g testuser testuser --uid 1234
  # Create "testuser" working dir:
  WORKDIR /home/testuser
  # Make working dir known to Python    
  ENV PYTHONPATH "${PYTHONPATH}:/home/testuser"
  # Create &amp; mount shared storage:
  RUN mkdir /var/run/testuser-storage
  VOLUME ["/var/run/testuser-storage"]
  # Start container as "testuser":
  ENV NAME testuser
  ENV HOME /home/testuser
  USER testuser
  </code></pre></li>
  <li><p>Run these bash commands:</p>
  
  <pre><code># Create the same user &amp; group "testuser" with IDs on host:
  getent group testuser &gt; /dev/null || /usr/sbin/groupadd -r testuser --gid 1234
  getent passwd testuser &gt; /dev/null || /usr/sbin/useradd -r -g testuser -d /var/lib/testuser -s /bin/nologin testuser --uid 1234
  # Create shared storage dirs on host:
  mkdir /var/run/testuser-storage
  chown -R testuser.testuser /var/run/testuser-storage
  # Build and run "testuser" Docker image:
  docker build . -t testuser
  docker run --net host --name testuser -v /var/run/testuser-storage:/var/run/testuser-storage -d testuser
  # Change ownership of shared volume dir to the be the GID of "testuser"
  chown -R 1234:1234 /var/run/testuser-storage
  </code></pre></li>
  </ol>
  
  <p>Note the names, UID's ,GID's must be the same for the Docker user and the host user. The last bash command tells the Docker image that that host user is the same as the Docker shared volume dir user, so that file dir then becomes owned by the "testuser" in the Docker container.</p>
  
Answers: CC BY-SA 4.0
Answers: 2018-11-09T06:59:32.200Z
Answers: ALSO, in some environments, SELinux can prevent file access. This can be resolved by either temporarily or permanently disabling SELinux (though I strongly advised against permanent disabling). A guide on this can be found here: https://www.tecmint.com/disable-selinux-temporarily-permanently-in-centos-rhel-fedora/
Answers: CubeBot88
Answers: CC BY-SA 4.0
Answers: 2018-11-07T07:46:40.373Z
Answers: 2018-11-07T07:46:40.373Z
Answers: CubeBot88
Answers: <p>As stated in the <a href="https://docs.docker.com/engine/reference/builder/#volume" rel="noreferrer">documentation</a>, VOLUME instruction inherits the directory content and permissions existing in the container, so you can workaround the problem with a dockerfile like this:</p>
  
  <pre><code>FROM ubuntu:xenial
  RUN useradd -d /home/ubuntu -ms /bin/bash -g root -G sudo -p ubuntu ubuntu
  RUN mkdir /opt/myvolume  &amp;&amp; chown ubuntu /opt/myvolume
  WORKDIR /home/ubuntu
  VOLUME /opt/myvolume
  </code></pre>
  
  <p>The creation of the directory has to be done as root (to be able to write within /opt).</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-06-21T20:28:48.817Z
Answers: The trick seems to be specifying `VOLUME` after the directory is created: _If any build steps change the data within the volume after it has been declared, those changes will be discarded._
Answers: Jesse Glick
Answers: CC BY-SA 4.0
Answers: 2019-06-22T08:27:56.233Z
Answers: The dir creation is unrelated, a volume will be mounted out of the container so anything done within the container before mount (container launched) will be discarded. I don't really understand your comment
Answers: Tensibai
Answers: CC BY-SA 4.0
Answers: 2018-07-19T07:25:55.653Z
Answers: 2018-07-19T07:45:26.340Z
Answers: 2018-07-19T07:45:26.340Z
Answers: Tensibai
Body: <p>I've got the following <code>Dockerfile</code>:</p>
  
  <pre><code>FROM ubuntu:xenial
  RUN useradd -d /home/ubuntu -ms /bin/bash -g root -G sudo -p ubuntu ubuntu
  WORKDIR /home/ubuntu
  USER ubuntu
  VOLUME /opt/myvolume
  </code></pre>
  
  <p>Which I built it:</p>
  
  <pre><code>$ docker build -t vol-test .
  Sending build context to Docker daemon  2.048kB
  Step 1/5 : FROM ubuntu:xenial
   ---&gt; 0b1edfbffd27
  Step 2/5 : RUN useradd -d /home/ubuntu -ms /bin/bash -g root -G sudo -p ubuntu ubuntu
   ---&gt; Using cache
   ---&gt; d82e3ecc5fe8
  Step 3/5 : WORKDIR /home/ubuntu
   ---&gt; Using cache
   ---&gt; ab1db29ee8bf
  Step 4/5 : USER ubuntu
   ---&gt; Using cache
   ---&gt; 129393a35d9e
  Step 5/5 : VOLUME /opt/myvolume
   ---&gt; Running in 691a4cbd077e
  Removing intermediate container 691a4cbd077e
   ---&gt; 11bc9e9db9d3
  Successfully built 11bc9e9db9d3
  Successfully tagged vol-test:latest
  </code></pre>
  
  <p>However, when run, the <code>/opt/myvolume</code> directory is owned by <code>root</code>, not <code>ubuntu</code>:</p>
  
  <pre><code>$ docker run vol-test id
  uid=1000(ubuntu) gid=0(root) groups=0(root),27(sudo)
  $ docker run vol-test find /opt/myvolume -ls
      66659      4 drwxr-xr-x   2 root     root         4096 Jul 18 23:02 /opt/myvolume
  $ docker run -u ubuntu vol-test find /opt/myvolume -ls
      66940      4 drwxr-xr-x   2 root     root         4096 Jul 18 23:12 /opt/myvolume
  </code></pre>
  
  <p>because it's created during the run.</p>
  
  <p>Is it possible to define or change the default owner of <code>VOLUME</code> directory in <code>Dockerfile</code>?</p>
  
  <p>I'm running it on macOS and Linux.</p>
  
Comments: CC BY-SA 4.0
Comments: 2018-07-20T23:30:26.120Z
Comments: Related: [Sharing volumes to Docker as the right user](https://chapeau.freevariable.com/2014/08/docker-uid.html).
Comments: kenorb
Comments: CC BY-SA 4.0
Comments: 2018-07-20T23:39:03.710Z
Comments: Related: [How to manage volume permissions to share data between host and container?](https://stackoverflow.com/q/35291520/55075)
Comments: kenorb
Tags: <docker><dockerfile><permissions>
Title: How to change the owner of VOLUME directory in Dockerfile?
UserDisplayName: kenorb
ThreadText: # How to change the owner of VOLUME directory in Dockerfile?
  
  (Asked by: username_0 on 2018-07-18)
  
  I've got the following `Dockerfile`:
  
  ```
  FROM ubuntu:xenial
  RUN useradd -d /home/ubuntu -ms /bin/bash -g root -G sudo -p ubuntu ubuntu
  WORKDIR /home/ubuntu
  USER ubuntu
  VOLUME /opt/myvolume
  
  ```
  
  Which I built it:
  
  ```
  $ docker build -t vol-test .
  Sending build context to Docker daemon  2.048kB
  Step 1/5 : FROM ubuntu:xenial
   ---> 0b1edfbffd27
  Step 2/5 : RUN useradd -d /home/ubuntu -ms /bin/bash -g root -G sudo -p ubuntu ubuntu
   ---> Using cache
   ---> d82e3ecc5fe8
  Step 3/5 : WORKDIR /home/ubuntu
   ---> Using cache
   ---> ab1db29ee8bf
  Step 4/5 : USER ubuntu
   ---> Using cache
   ---> 129393a35d9e
  Step 5/5 : VOLUME /opt/myvolume
   ---> Running in 691a4cbd077e
  Removing intermediate container 691a4cbd077e
   ---> 11bc9e9db9d3
  Successfully built 11bc9e9db9d3
  Successfully tagged vol-test:latest
  
  ```
  
  However, when run, the `/opt/myvolume` directory is owned by `root`, not `ubuntu`:
  
  ```
  $ docker run vol-test id
  uid=1000(ubuntu) gid=0(root) groups=0(root),27(sudo)
  $ docker run vol-test find /opt/myvolume -ls
      66659      4 drwxr-xr-x   2 root     root         4096 Jul 18 23:02 /opt/myvolume
  $ docker run -u ubuntu vol-test find /opt/myvolume -ls
      66940      4 drwxr-xr-x   2 root     root         4096 Jul 18 23:12 /opt/myvolume
  
  ```
  
  because it's created during the run.
  
  Is it possible to define or change the default owner of `VOLUME` directory in `Dockerfile`?
  
  I'm running it on macOS and Linux.
  
  **Tags:** docker, dockerfile, permissions
  
  > **Comments:**
  > **username_0**: Related: [Sharing volumes to Docker as the right user](https://chapeau.freevariable.com/2014/08/docker-uid.html).  
  > **username_0**: Related: [How to manage volume permissions to share data between host and container?](https://stackoverflow.com/q/35291520/55075)  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2018-07-19. Score: 28 | Accepted answer
  
  As stated in the [documentation](https://docs.docker.com/engine/reference/builder/#volume), VOLUME instruction inherits the directory content and permissions existing in the container, so you can workaround the problem with a dockerfile like this:
  
  ```
  FROM ubuntu:xenial
  RUN useradd -d /home/ubuntu -ms /bin/bash -g root -G sudo -p ubuntu ubuntu
  RUN mkdir /opt/myvolume  && chown ubuntu /opt/myvolume
  WORKDIR /home/ubuntu
  VOLUME /opt/myvolume
  
  ```
  
  The creation of the directory has to be done as root (to be able to write within /opt).
  
  > **Comments:**
  > **username_3**: The trick seems to be specifying `VOLUME` after the directory is created: _If any build steps change the data within the volume after it has been declared, those changes will be discarded._  
  > **username_2**: The dir creation is unrelated, a volume will be mounted out of the container so anything done within the container before mount (container launched) will be discarded. I don't really understand your comment  
  
  ---
  
  ### Answer by username_1 on 2018-11-07. Score: 5
  
  I had a similar problem, this worked for me:
  
  1. Write Docker file with:
  
     ```
     # Create app layer:
     FROM python:3.4
     # Create app user & group "testuser" with IDs:
     RUN groupadd -r testuser --gid 1234 && useradd -d /home/testuser -ms /bin/bash -r -g testuser testuser --uid 1234
     # Create "testuser" working dir:
     WORKDIR /home/testuser
     # Make working dir known to Python    
     ENV PYTHONPATH "${PYTHONPATH}:/home/testuser"
     # Create & mount shared storage:
     RUN mkdir /var/run/testuser-storage
     VOLUME ["/var/run/testuser-storage"]
     # Start container as "testuser":
     ENV NAME testuser
     ENV HOME /home/testuser
     USER testuser
  
     ```
  2. Run these bash commands:
  
     ```
     # Create the same user & group "testuser" with IDs on host:
     getent group testuser > /dev/null || /usr/sbin/groupadd -r testuser --gid 1234
     getent passwd testuser > /dev/null || /usr/sbin/useradd -r -g testuser -d /var/lib/testuser -s /bin/nologin testuser --uid 1234
     # Create shared storage dirs on host:
     mkdir /var/run/testuser-storage
     chown -R testuser.testuser /var/run/testuser-storage
     # Build and run "testuser" Docker image:
     docker build . -t testuser
     docker run --net host --name testuser -v /var/run/testuser-storage:/var/run/testuser-storage -d testuser
     # Change ownership of shared volume dir to the be the GID of "testuser"
     chown -R 1234:1234 /var/run/testuser-storage
  
     ```
  
  Note the names, UID's ,GID's must be the same for the Docker user and the host user. The last bash command tells the Docker image that that host user is the same as the Docker shared volume dir user, so that file dir then becomes owned by the "testuser" in the Docker container.
  
  > **Comments:**
  > **username_1**: ALSO, in some environments, SELinux can prevent file access. This can be resolved by either temporarily or permanently disabling SELinux (though I strongly advised against permanent disabling). A guide on this can be found here: https://www.tecmint.com/disable-selinux-temporarily-permanently-in-centos-rhel-fedora/  
  
  ---
  
  