Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permission Denied Error using Laravel & Docker

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?

like image 966
Anand Naik B Avatar asked Feb 05 '18 09:02

Anand Naik B


2 Answers

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

like image 186
vivekyad4v Avatar answered Sep 21 '22 01:09

vivekyad4v


I found similar problem and I fixed it by

chown -R www-data:www-data /var/www  chmod -R 755 /var/www/storage 
like image 44
Khachornchit Songsaen Avatar answered Sep 21 '22 01:09

Khachornchit Songsaen