title: How to manage permissions for a volume mounted into a docker container?
body: <p>I'm developing a Wordpress theme, and want to use Docker in my dev setup. What I've done is pretty simple:</p>
  
  <ul>
  <li>create a <code>database</code> service running MySQL 5.7</li>
  <li>create a <code>wordpress</code> service, where I mount my theme folder as a volume into <code>/var/www/html/wp-content/themes</code></li>
  </ul>
  
  <p>I'm struggling with the volume permissions, however.</p>
  
  <p>I'm working with the following project folder structure:</p>
  
  <pre><code>.
  ├── docker-compose.yml
  └── my-theme
  </code></pre>
  
  <p>My <strong><code>docker-compose.yml</code></strong> file looks like this:</p>
  
  <pre><code>version: '3.2'
  
  services:
    database:
      image: mysql:5.7
      volumes:
        - my_data:/var/lib/mysql
      restart: always
      environment:
        MYSQL_ROOT_PASSWORD: root
        MYSQL_DATABASE: wordpress
        MYSQL_USER: wordpress
        MYSQL_PASSWORD: root
  
    wordpress:
      depends_on:
        - database
      image: wordpress:php7.3-apache
      ports:
        - '8000:80'
      restart: always
      environment:
        WORDPRESS_DB_HOST: database:3306
        WORDPRESS_DB_USER: wordpress
        WORDPRESS_DB_PASSWORD: root
      working_dir: /var/www/html
      volumes:
        - type: volume
          source: ./my-theme
          target: /var/www/html/wp-content/themes/my-theme
  volumes:
    my_data: {}
  </code></pre>
  
  <p>When I run <code>docker-compose up</code>, everything works as expected: containers are created, and I can access Wordpress in the browser. However, the theme I mounted as a volume doesn't render anything when I activate it. </p>
  
  <p>When I <code>sh</code> into the <code>wordpress</code> container (<code>docker-compose exec wordpress sh</code>) I can see that the <code>wp-content/themes</code> folder is owned by <code>root</code>. So I figured that was the problem.</p>
  
  <p>I verified this being a permissions issue by manually and recursively <code>chown</code>ing the <code>wp-content</code> folder in the container:</p>
  
  <pre class="lang-sh prettyprint-override"><code>chown -R www-data:www-data /var/www/html/wp-content
  </code></pre>
  
  <p>Once that was done, my theme rendered as expected. So now I'm looking for a way to avoid this <code>chown</code> process (the idea is that any other dev can clone this project, simply run <code>docker-compose up</code> and start working).</p>
  
  <p>The first thing I tried was to make a <strong>Dockerfile</strong> where I would build a slightly customized Wordpress image:</p>
  
  <pre><code>FROM wordpress:php7.3-apache
  RUN mkdir -p /var/www/html/wp-content/themes/test-theme \
    &amp;&amp; chown -R /var/www/html/wp-content
  </code></pre>
  
  <p>My reasoning behind this was that by creating the directory and <code>chown</code>ing it beforehand, the volume would inherit the user:group mapping. Alas, no such thing; mounting the volume overrides this mapping and sets it back to <code>root:root</code>.</p>
  
  <p>After that, I tried to set the <code>APACHE_RUN_USER</code> and <code>APACHE_RUN_GROUP</code> environment variables in my <code>docker-compose.yml</code> file:</p>
  
  <pre><code>version: '3.2'
  
  services:
    database:
      ...
  
    wordpress:
      ...
      environment:
        WORDPRESS_DB_HOST: database:3306
        WORDPRESS_DB_USER: wordpress
        WORDPRESS_DB_PASSWORD: root
        APACHE_RUN_USER: '$(id -u)'
        APACHE_RUN_GROUP: '$(id -g)'
      working_dir: /var/www/html
      volumes:
        - type: volume
          source: ./my-theme
          target: /var/www/html/wp-content/themes/my-theme
  volumes:
    my_data: {}
  </code></pre>
  
  <p>However, this threw a bunch of apache errors on build. </p>
  
  <p>I'm at a bit of a loss here now. Is there any best practice for managing permissions of mounted volumes in Docker? I've googled a lot for this, but the solutions I do find go a little bit over my head.</p>