Setting correct permission for Docker PHP-FPM on mounted folder

Now, if you have followed my guide on setting up Docker with PHP-FPM then you'll most likely face this issue where your files and directories permission will have to set to 777 in order for docker to write files to your mounted folder.

In order to resolve this, you'll need to reset your 777 mistakes using the command given in my reset files/directories permission article.

Once you've done that, you'll be back to your square one where your application can't write to your mounted folder.

Now, in your mounted folder assuming its in /root/www you'll need to look for the user that exec your php script in your php-fpm docker. By default its www-data (dahhh). So let's find out what this user id is on the parent machine by firing the following docker command

docker exec phpfpm id www-data

where phpfpm is the docker name of your PHP-FPM container. If you are not using PHP-FPM on a separate container, you can easily just replace phpfpm to your LEMP/LAMP docker container name.

and the above will show you something like this

root@php:~# docker exec phpfpm id www-data
uid=82(www-data) gid=82(www-data) groups=82(www-data),82(www-data)

the above means that on the parent machine, the user id for www-data is 82. Now, go ahead and change the user permission on your mounted folder to 82 with the following command

chown 82:82 -r /root/www

where /root/www is the example mounted folder used in this article.

Now, with the correct user permission, your application should be able to write correctly without the need to set your directories permissions to 777 which is pretty insecure.

Hope this helps.