I'm trying to mount a volume in docker-compose to apache image. The problem is, that apache in my docker is run under www-data:www-data
but the mounted directory is created under root:root
. How can I specify the user of the mounted directory?
I tried to run command setupApacheRights.sh
. chown -R www-data:www-data /var/www
but it says chown: changing ownership of '/var/www/somefile': Permission denied
services: httpd: image: apache-image ports: - "80:80" volumes: - "./:/var/www/app" links: - redis command: /setupApacheRights.sh
I would prefer to be able to specify the user under which it will be mounted. Is there a way?
For some development applications, the container needs to write into the bind mount so that changes are propagated back to the Docker host. At other times, the container only needs read access to the data. Multiple containers can mount the same volume.
Though both methods are similar, there is a slight difference. Docker manages Volumes and is usually not affected by other processes running on the same host. In contrast, Bind Mounts are just a directory on the host file system and may be modified by other processes other than docker.
Bind mounts have been around since the early days of Docker. Bind mounts have limited functionality compared to volumes. When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine.
To achieve the desired behavior without changing owner / permissions on the host system do the following steps.
get the ID of the desired user and or group you want the permissions to match with executing the id
command on your host system - this will show you the uid and gid of your current user and as well all IDs from all groups the user is in.
$ id
add the definition to your docker-compose.yml
user: "${UID}:${GID}"
so your file could look like this
php: # this is my service name user: "${UID}:${GID}" # we added this line to get a specific user / group id image: php:7.3-fpm-alpine # this is my image # and so on
set the values in your .env
file
UID=1000 GID=1001
Now your user in the container has the id 1000 and the group is 1001 and you can set that differently for every environment.
Note: Please replace the IDs I used with the user / group IDs you found on your host system. Since I cannot know which IDs your system is using I gave some example group and user IDs.
If you don't use docker-compose or want to know more different approaches to achieve this have a read through my source of information: https://dev.to/acro5piano/specifying-user-and-group-in-docker-i2e
The bad news is there's no owner/group/permission settings for volume
😢. The good news is the following trick will let you bake it into your config, so it's fully automated 🎉.
In your Dockerfile, create an empty directory in the right location and with desired settings.
This way, the directory will already be present when docker-compose mounts to the location. When the server mounts during boot (based on docker-compose), the mounting action happily leaves those permissions alone.
Dockerfile:
# setup folder before switching to user RUN mkdir /volume_data RUN chown postgres:postgres /volume_data USER postgres
docker-compose.yml
volumes: - /home/me/postgres_data:/volume_data
source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With