Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permission for volumes in docker-compose

I want create docker-containers with volumes and custom group. But faced with mistake with permission inside container. All file is have for example 'custom-group' and work fine, but the Document folder is have by default root group. I think this due to volumes. How to Document folder set 'custom-group'. My code is below

volumes:
  - /base/documents:/app/documents:rw
like image 626
Met Den Avatar asked Nov 30 '25 21:11

Met Den


1 Answers

The uid/gid inside of the container is typically the same as the uid/gid outside of the container, on the host (user namespaces are off by default and wouldn't solve this problem, in fact they would make it worse). Note that this is uid/gid, and not user name and group name, since containers can have their own /etc/passwd and /etc/group files.

You need to run your container with the uid/gid matching that of the files on your host. That is done with the user section of a compose file, or -u option on the docker run command line, e.g.:

docker run -u "$(id -u):$(id -g)" -v /base/documents:/app/documents:rw ...

or in the compose file:

user: "1000:1000"

If your application must run as root, then there are a lot fewer options to handle this. My own preference is to dynamically handle the uid/gid from an entrypoint that starts up as root, fixes the uid/gid inside the container to match the host (looking at the volume owner), and then drops from root to the container user for running the application. For more details on how that's done, you can see my base image repo, including the nginx example and fix-perms script.

like image 63
BMitch Avatar answered Dec 05 '25 13:12

BMitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!