'How to handle permission inside a volume from docker?

I have a container running a PHP application and the php-fpm service can't write the cache files inside an folder provided by a docker volume.

I gave 777 permissions on the folder I need to write, from the host machine, but it works just for a while. Files created by php-fpm doesn't have necessary permissions. Furthermore it's not possible to change the owner and group with chown command.

This is my docker-composer.yml file:

web:
    image: eduardoleal/nginx
    external_links:
        - proxy
    links:
        - php:php
    container_name: "app-web"
    environment:
        VIRTUAL_HOST: web.app
    volumes_from:
        - data 
    volumes:
        - ./src/nginx-vhost.conf:/etc/nginx/sites-enabled/default
php:
    image: eduardoleal/php56
    container_name: "app-web-php"
    volumes_from:
        - data
data:
    container_name: "app-web-data" 
    image: phusion/baseimage
    volumes:
        - /Users/eduardo.leal/Code/vidaclass/web:/var/www/public

I'm running docker on OSX with VirtualBox.



Solution 1:[1]

I had this problem too. Docker machine uses docker user and staff group on mounted volumes, which have UID=1000 and GID=50 respectively. You need to modify your php-fpm config and replace default user (I suppose it's nobody), with username which have UID=1000 inside container. In case you don't have such user, you'll need to create such user. Do the same trick for group with GID=50. It's very-very dirty hack, but I didn't found better solution yet.

Solution 2:[2]

Add RUN usermod -u 1000 www-data somewhere before EXPOSE into Dockerfile for eduardoleal/php56 image, it will fix problem with permissions.

Solution 3:[3]

If you are using MacOSx. you have to change permission of staff user (this user is created by docker). Default staff user only read permission. So webserver cannot write into your cache folder in docker container. You can do and see the picture below

chmod -R 777 cache_folder on your mac.

Hope this answer is useful for you

Anyway, you can also use docker-machine-nfs to fix this problem

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SLY
Solution 2 ashatrov
Solution 3 Ousmane