Permission Denied Error using Laravel & Docker

97,137

Solution 1

Make your Dockerfile something as below -

FROM php:7-fpm
WORKDIR /var/www
RUN apt-get update && apt-get install -y libmcrypt-dev mysql-client && docker-php-ext-install mcrypt pdo_mysql
ADD . /var/www
RUN chown -R www-data:www-data /var/www

This makes directory /var/www owned by www-data which is the default user for php-fpm.

Since it is compiled with user www-data.

Ref-

https://github.com/docker-library/php/blob/57b41cfc2d1e07acab2e60d59a0cb19d83056fc1/7.0/jessie/fpm/Dockerfile

Solution 2

I found similar problem and I fixed it by

chown -R www-data:www-data /var/www

chmod -R 755 /var/www/storage

Solution 3

When using bind mounts in Docker, the original permissions in the Docker host are preserved in the container. This enables us to set appropriate permissions on the Docker host, to be used inside the container.

First, you should find the uid and gid of the nginx, for example:

docker-compose exec nginx id www-data

The output of this command would be something like this:

uid=33(www-data) gid=33(www-data) groups=33(www-data)

Then, you should use these uid and gid to set permissions on Docker host, which will be used by the container too. So, run the following command on the Docker host:

sudo chown -R 33:33 site

Now everything must be working.

Solution 4

You can do 3 things as I used/using

  1. Grant 755 permission to storage folder (sudo chmod 755 storage/ storage/* )

  2. RUN chown -R www-data:www-data /var/www RUN chmod 755 /var/www

  3. Move project folder to /home which is no need of special permission to write on that directory(Personally Recommend this) (In mine I have used /home/projects/ and all of them placed there).


Note: If the project is in /var/www/ then if you wrote file upload also you need permission to that folders too. this will avoid such error when you moved that to /home

Solution 5

I tried all of these, and it didn't work. My Laravel storage folder had access. But /var/www/storage still didn't had permission.

After reading multiple suggestions, with this in particular. I did the following (kindly note that php is the name of my container image, and i linked my laravel project directly to /var/www):

  1. From my docker, I ran docker-compose exec php ls -al /var/www/storage
    • I saw that only root had access but i need www-data to have access.
  2. Then I ran docker-compose exec php chown -R $USER:www-data /var/www/storage
    • This gives www-data access to the storage folder
  3. Then I ran docker-compose exec php chown -R $USER:www-data /var/www/bootstrap/cache
    • This gives www-data access to the bootstrap/cache folder

If you run item 1 again, www-data should have access, and your Laravel app should be fine.

Share:
97,137
Anand Naik B
Author by

Anand Naik B

Updated on December 23, 2021

Comments

  • Anand Naik B
    Anand Naik B over 2 years

    I have two docker containers: Nginx and App.
    The app container extends PHP-fpm and also has my Laravel Code.

    In my docker-compose.yml I'm doing:

    version: '2'
    services:
        nginx:
            build:
                context: ./nginx
                dockerfile: ./Dockerfile
            ports:
                - "80:80"
            links:
                - app
        app:
            build:
                context: ./app
                dockerfile: ./Dockerfile
    

    In my Nginx Dockerfile i'm doing:

    FROM nginx:latest
    WORKDIR /var/www
    ADD ./nginx.conf /etc/nginx/conf.d/default.conf
    ADD . /var/www
    EXPOSE 80
    

    In my App Dockerfile I'm doing:

    FROM php:7-fpm
    WORKDIR /var/www
    RUN apt-get update && apt-get install -y libmcrypt-dev mysql-client && docker-php-ext-install mcrypt pdo_mysql
    ADD . /var/www
    

    After successfully running docker-compose up, I have the following error when I try localhost

    The stream or file "/var/www/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied

    From my understanding, the storage folder needs to writable by the webserver.
    What should I be doing to resolve this?