<blockquote>
<p>I have a docker container with a volume /foo defined in the docker-compose.yml. The Dockerfile then does...</p>
</blockquote>
<p>The logic here is inverted. First you build an image with a Dockerfile. Then you run that image as a container with volumes and any other configuration. So you will not be able to modify the volume during the image build. Options to solve this are:</p>
<ul>
<li>Move the logic into your entrypoint. This requires your container to start as root, but it's possible to do something like <code>gosu</code> to later drop to your user before running your app.</li>
<li>Have docker set the permissions for you as part of the named volume creation. When docker creates a new named volume, it initializes it with files from the image, including their ownership and permissions. This only happens when the named volume is empty, so once created and in use, changes to the image will not be seen in your volumes.</li>
<li>Configure the volume permissions on the host before mounting it into the container. This is probably most appropriate for a host volume (aka bind mount).</li>
</ul>

CC BY-SA 4.0
2022-02-04T13:52:11.330Z
2022-02-04T13:52:11.330Z
BMitch
<p>I have a docker container with a volume /foo defined in the docker-compose.yml. The Dockerfile then does:</p>
<pre><code>RUN useradd -d /home/newuser -m -s /bin/bash newuser &amp;&amp; \
    usermod newuser -g www-data
RUN chown -R newuser:www-data /foo
</code></pre>
<p>When the container is setup I can see it run the two commands with no errors, but the folder /foo is unchanged. When I run a shell on the container I can see the user is created. I can run chown on other directories and files fine. I'm not calling VOLUME anywhere in my Dockerfile for reference.</p>

<docker>
Can you run chown on a root volume folder in a Dockerfile setup?
Sirisian
# Can you run chown on a root volume folder in a Dockerfile setup?

(Asked by: username_0 on 2022-02-02)

I have a docker container with a volume /foo defined in the docker-compose.yml. The Dockerfile then does:
RUN useradd -d /home/newuser -m -s /bin/bash newuser && \
    usermod newuser -g www-data
RUN chown -R newuser:www-data /foo

When the container is setup I can see it run the two commands with no errors, but the folder /foo is unchanged. When I run a shell on the container I can see the user is created. I can run chown on other directories and files fine. I'm not calling VOLUME anywhere in my Dockerfile for reference.

**Tags:** docker


---

## 1 answers

### Answer by username_1 on 2022-02-04. Score: 0

> I have a docker container with a volume /foo defined in the docker-compose.yml. The Dockerfile then does...

The logic here is inverted. First you build an image with a Dockerfile. Then you run that image as a container with volumes and any other configuration. So you will not be able to modify the volume during the image build. Options to solve this are:

* Move the logic into your entrypoint. This requires your container to start as root, but it's possible to do something like `gosu` to later drop to your user before running your app.
* Have docker set the permissions for you as part of the named volume creation. When docker creates a new named volume, it initializes it with files from the image, including their ownership and permissions. This only happens when the named volume is empty, so once created and in use, changes to the image will not be seen in your volumes.
* Configure the volume permissions on the host before mounting it into the container. This is probably most appropriate for a host volume (aka bind mount).


---

