Answers: <p>Your mkdir and chown commands are RUN when building the image, whereas you then mount <code>/var/www/app</code> from a VOLUME, so you are replacing the created and chowned folder with the contents of <code>./app</code> on the outside (and thus it's as if the chown didn't work).</p>
  
  <p>If you want to chown the contents of the volume <em>after it is mounted</em>, you should place those instructions on a script that is called as the CMD.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-08-12T23:10:35.740Z
Answers: 2019-08-12T23:10:35.740Z
Answers: Ángel
Answers: <p>I found the reason for the problem.
  In Docker</p>
  <blockquote>
  <p><a href="https://blog.container-solutions.com/understanding-volumes-docker" rel="nofollow noreferrer">anything after the VOLUME instruction in a Dockerfile will not be able to make changes to that volume.</a></p>
  </blockquote>
  <p>I define a volume with <code>&quot;./app:/var/www/app&quot;</code> and, after that, manipulate it, so it doesn't work.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-08-12T10:21:46.510Z
Answers: 2021-06-14T16:22:54.503Z
Answers: 2021-06-14T16:22:54.503Z
Answers: Log
Body: <p>I have the following docker-compose file:</p>
  
  <pre><code>version: '2'
  
  networks:
    default:
      driver: bridge
  
  services:
  
    yii2-app:
      build:
        context: .
        dockerfile: ./Dockerfile-app
      ports:
        - "80:80"
      networks:
        - default
      depends_on:
        - yii2-db
      volumes:
        - "./app:/var/www/app"
        - "./nginx:/etc/nginx/sites-available"
      ...
  </code></pre>
  
  <p>Dockerfile-app:</p>
  
  <pre><code>FROM richarvey/nginx-php-fpm
  
  ADD app /var/www/app
  
  RUN rm -Rf /etc/nginx/sites-enabled/*
  
  ADD nginx/site.conf /etc/nginx/sites-available/site.conf
  
  RUN ln -s /etc/nginx/sites-available/site.conf /etc/nginx/sites-enabled/site.conf
  
  RUN cd /var/www/app &amp;&amp; \
      composer install
  
  RUN cd /var/www/app &amp;&amp; chmod +x yii &amp;&amp; \
      cd web &amp;&amp; mkdir -p uploads &amp;&amp; \
      cd /var/www &amp;&amp; chown nginx:nginx -R app/
  </code></pre>
  
  <p>My last command RUN has no effect: chown does not set the nginx files owner. The folder "uploads" also is not created.</p>
  
  <p>When I run <code>docker-compose build --no-cache</code> that step is passed:</p>
  
  <pre><code>Step 7/7 : RUN cd /var/www/app &amp;&amp; chmod +x yii &amp;&amp;     cd web &amp;&amp; mkdir -p uploads &amp;&amp;     cd /var/www &amp;&amp; chown nginx:nginx -R app/
   ---&gt; Running in 26a918bece47
  Removing intermediate container 26a918bece47
   ---&gt; 00db026a461c
  Successfully built 00db026a461c
  Successfully tagged passport-app_yii2-app:latest
  </code></pre>
  
  <p>However, when I run the workload in the "common" way using <code>docker-compose up -d</code> the change is not applied.</p>
  
  <p>Why does my last RUN command not work?</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-08-12T09:44:13.573Z
Comments: I think you could try to debug those statements by executing them one by one and see if there is an issue.
Comments: profesor79
Comments: CC BY-SA 4.0
Comments: 2019-08-12T10:00:12.347Z
Comments: @profesor79 It seems like in Docker I can not create file (folder, make chown) in /var/www/app, because it's volume.
Comments: Log
Tags: <docker><docker-compose>
Title: Why does chown not work in RUN command in Docker?
UserDisplayName: Log
ThreadText: # Why does chown not work in RUN command in Docker?
  
  (Asked by: username_0 on 2019-08-12)
  
  I have the following docker-compose file:
  
  ```
  version: '2'
  
  networks:
    default:
      driver: bridge
  
  services:
  
    yii2-app:
      build:
        context: .
        dockerfile: ./Dockerfile-app
      ports:
        - "80:80"
      networks:
        - default
      depends_on:
        - yii2-db
      volumes:
        - "./app:/var/www/app"
        - "./nginx:/etc/nginx/sites-available"
      ...
  
  ```
  
  Dockerfile-app:
  
  ```
  FROM richarvey/nginx-php-fpm
  
  ADD app /var/www/app
  
  RUN rm -Rf /etc/nginx/sites-enabled/*
  
  ADD nginx/site.conf /etc/nginx/sites-available/site.conf
  
  RUN ln -s /etc/nginx/sites-available/site.conf /etc/nginx/sites-enabled/site.conf
  
  RUN cd /var/www/app && \
      composer install
  
  RUN cd /var/www/app && chmod +x yii && \
      cd web && mkdir -p uploads && \
      cd /var/www && chown nginx:nginx -R app/
  
  ```
  
  My last command RUN has no effect: chown does not set the nginx files owner. The folder "uploads" also is not created.
  
  When I run `docker-compose build --no-cache` that step is passed:
  
  ```
  Step 7/7 : RUN cd /var/www/app && chmod +x yii &&     cd web && mkdir -p uploads &&     cd /var/www && chown nginx:nginx -R app/
   ---> Running in 26a918bece47
  Removing intermediate container 26a918bece47
   ---> 00db026a461c
  Successfully built 00db026a461c
  Successfully tagged passport-app_yii2-app:latest
  
  ```
  
  However, when I run the workload in the "common" way using `docker-compose up -d` the change is not applied.
  
  Why does my last RUN command not work?
  
  **Tags:** docker, docker-compose
  
  > **Comments:**
  > **username_1**: I think you could try to debug those statements by executing them one by one and see if there is an issue.  
  > **username_0**: @username_1 It seems like in Docker I can not create file (folder, make chown) in /var/www/app, because it's volume.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2019-08-12. Score: 6
  
  Your mkdir and chown commands are RUN when building the image, whereas you then mount `/var/www/app` from a VOLUME, so you are replacing the created and chowned folder with the contents of `./app` on the outside (and thus it's as if the chown didn't work).
  
  If you want to chown the contents of the volume *after it is mounted*, you should place those instructions on a script that is called as the CMD.
  
  
  ---
  
  ### Answer by username_0 on 2019-08-12. Score: 5
  
  I found the reason for the problem.
  In Docker
  
  > [anything after the VOLUME instruction in a Dockerfile will not be able to make changes to that volume.](https://blog.container-solutions.com/understanding-volumes-docker)
  
  I define a volume with `"./app:/var/www/app"` and, after that, manipulate it, so it doesn't work.
  
  
  ---
  
  