I have a docker with a php application on it
I have a share volume, for example
/home/me/dev/site <=> /var/www/site
I can write something in my host, it will be sync with the container
if I launch
sudo docker exec test touch /var/www/site/test.txt
It works
But if my server is trying to create a file as www-data
this is not working because of the rights.
Is there a way to give access to my shared volumes to www-data
?
I am using boot2docker
Add the following lines to your dockerfile and rebuild your image
(Bind-mounted) volumes in Docker will maintain the permissions that are set on the Docker host itself. You can use this to set the permissions on those files and directories before using them in the container.
Some background;
Permissions in Linux are based on user and group ids (
'uid'
/'gid'
). Even though you see a user- and group name as owner, those names aren't actually important in Linux, they are only there to make it easier for you to see who's the owner of a file (they are looked up from the/etc/passwd
file).You can set any
uid
/gid
on a file; a user doesn't have to exist when setting those permissions. For example;Checking permissions (inside and outside a container)
As mentioned above, Docker maintains ownership of the host when using a volume. This example shows that permissions and ownership in the volume are the same outside and inside a container;
Setting the permissions
As explained, a user doesn't have to exist in order to use them, so even if we don't have a
www-data
user on the Docker host, we can still set the correct permissions if we know the "uid" and "gid" of that user inside the container;Let's see what the uid and gid of the www-data user is inside the container;
First check the state before changing the permissions. This time we run the nginx container as user
www-data
;Next, set the permissions on the local directory, and see if we are able to write;
Success!