title: Docker volumes : specifying permissions using mount options
body: <p>I'm trying to use a named volume mounted in a Docker container, but get a <code>Permission denied</code> error when trying to create a file in the mounted folder. So I'm trying to use <code>mount</code> options when creating my volume, but that does not work as I want.</p>
  
  <h2>Introduction</h2>
  
  <p>I'm totally aware that when mounting a volume (created by <code>docker volume create my_named_volume</code>) with the option <code>-v my_named_volume:/home/user/test</code> or <code>--mount type=volume,source=my_named_volume,target=/home/user/test</code>), the folder inside the container (<code>/home/user/test</code> will be owned by <code>root</code> user, even if <code>/home/user</code> belongs to an user <code>user</code> created in my <code>Dockerfile</code>. If I run :</p>
  
  <pre><code>docker run --rm \
      --name test_named_volume \
      --mount type=volume,source=my_named_volume,target=/home/user/test \
      test_named_volume \
      su user -c "touch /home/user/test/a"
  </code></pre>
  
  <p>Then I get :</p>
  
  <pre><code>touch: cannot touch '/home/user/test/a': Permission denied
  </code></pre>
  
  <p>I'm understanding that. That's why I'm trying to use <code>mount</code> options when creating my volume.</p>
  
  <h2><code>mount</code> options</h2>
  
  <p>I'm specifying an <code>uid</code> when creating my volume, in order to make my user <code>user</code> able to create a file in that volume :</p>
  
  <pre><code>docker volume create my_named_volume \
      --opt o=uid=1000
  </code></pre>
  
  <p><code>1000</code> is the <code>uid</code> of the user <code>user</code> created in my <code>Dockerfile</code> :</p>
  
  <pre><code>FROM    debian:jessie
  
  ENV     HOME /home/user
  
  RUN     useradd \
              --create-home \
              --home-dir $HOME \
              --uid 1000 \
              user \
   &amp;&amp;     chown -R user:user $HOME
  
  WORKDIR $HOME
  </code></pre>
  
  <p>But when running my container (with the same command <code>docker run</code> defined above), I'm getting an error (<code>missing device in volume options</code>) :</p>
  
  <pre><code>docker: Error response from daemon: error while mounting volume '/var/lib/docker/volumes/my_named_volume/_data': missing device in volume options.
  </code></pre>
  
  <p>From <a href="https://docs.docker.com/engine/reference/commandline/volume_create/#driver-specific-options" rel="noreferrer">the docs</a>, I see that options <code>--device</code> and <code>--type</code> are missing from my volume creation :</p>
  
  <pre><code>docker volume create my_named_volume \
      --opt device=??? \
      --opt type=??? \
      --opt o=uid=1000
  </code></pre>
  
  <p>But I cannot see why I must give these options. <code>device</code> needs to be a block device, and from what I read, <code>type</code> should be something like <code>ext4</code>. But what I want is basically <strong>just</strong> set the <code>uid</code> option to my volume. It looks like creating a block device should work, but it seems too much configuration for a "simple" problem.</p>
  
  <p>I have tried to use <code>tmpfs</code> for <code>device</code> and <code>type</code>, that works fine (file <code>/home/user/test/a</code> is created)... until my container is stopped (the data is not persisted, and that's logical because it's <code>tmpfs</code>). I want to persist that data written in the volume when the container exits.</p>
  
  <p>What is the simplest way to specify permissions when mounting a named volume in a container? I don't want to modify my Dockerfile to use some magic (entrypoint that <code>chown</code> and then execute the command for example). It seems possible using <code>mount</code> options, I feel like I'm close to the solution, but maybe I'm in the wrong way.</p>