0

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.

1 Answer 1

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.