title: Docker: mounting local directories as a non-root user in a container
body: <p>I've read quite a few threads on the internet about how to best mount local (project) directories into a Docker container so that the directories are not owned by the <code>root</code> user. Unfortunately, I've not found a precise answer.</p>
  
  <p>I'm building my development stack with this <code>docker-compose.yml</code> (<a href="https://github.com/advancingu/SfDocker" rel="nofollow">SfDocker</a>) file:</p>
  
  <pre><code>db:
      image: mysql:latest
      ports:
      - "3306:3306"
      environment:
          MYSQL_ROOT_PASSWORD: symfonyrootpass
          MYSQL_DATABASE: symfony
          MYSQL_USER: symfony
          MYSQL_PASSWORD: symfonypass
  worker:
      image: symfony/worker-dev
      ports:
      - "8080:80"
      environment:
          XDEBUG_HOST: 192.168.1.194
          XDEBUG_PORT: 9000
          XDEBUG_REMOTE_MODE: req
      links:
      - db
      volumes:
      - "var/nginx/:/var/log/nginx"
      - symfony-code:/var/www/app
  </code></pre>
  
  <p>Volumes are mounted at runtime only after the images are built. I've added a new user by <code>RUN groupadd -r luqo33 &amp;&amp; useradd -r -g luqo33 luqo33</code> in the <code>symfony/worker-dev</code> image, but I was not able to <code>chmod</code> the mounted volumes so that it is owned by <code>luqo33:www-data</code>. I've tried to do it by:</p>
  
  <ol>
  <li><p>Copying and running an <code>entrypoint.sh</code> with <code>chmod</code> command:</p>
  
  <p>COPY entrypoint.sh /entrypoint.sh
  RUN chmod +x /entrypoint.sh
  ENTRYPOINT ["/entrypoint.sh"]</p></li>
  </ol>
  
  <p>The container would start and then shut down with no apparent reason.</p>
  
  <ol start="2">
  <li>Executing <code>CMD chown -R luqo33:www-data</code> while starting containers - this could not work because at the time of starting the <code>worker-dev</code> container, the volumes seem not to be mounted yet.</li>
  </ol>
  
  <p>I did not manage to set the ownership of the mounted directories to users other than <code>root</code>. How can I achieve this?</p>