Getting permission denied on /var/www/storage/logs/laravel.log for a Laravel app in Docker
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Hello.

I'm trying to build a docker image for Laravel.

You can get the entire project here: https://bitbucket.org/danygiguere/docker_image/src/master/
Check the readme for easy installation in one command.

In my dockerfile, I'm building the image from nginx:alpine, adding php to it.

You can view the entire Dockerfile here https://bitbucket.org/danygiguere/docker_image/src/master/Dockerfile

My problem is that I get this error:
The stream or file "/var/www/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied

This is the part of the Dockerfile where I have to change something:

COPY . /var/www
RUN chown -R www-data:www-data /var/www
RUN chmod -R 755 /var/www/storage
RUN chmod -R 755 /var/www/storage/logs
RUN chmod -R 755 /var/www/storage/logs/laravel.log

The only way I could make it work is with this line:
RUN chmod -R 777 /var/www/storage
but that's not secure.

awarded to TheOsch

Crowdsource coding tasks.

2 Solutions

Winning solution

Hello,

Try to replace those 5 lines with

COPY --chown=www-data:www-data . /var/www  
#RUN chown -R www-data:www-data /var/www
#RUN chmod -R 777 /var/www/storage
RUN chmod -R 755 /var/www/storage
#RUN chmod -R 755 /var/www/storage/logs
#RUN chmod -R 755 /var/www/storage/logs/laravel.log

I did so and didn't get that error.

Thanks

I still get the same error.
randomhelprequested 1 month ago
Just a moment.
TheOsch 1 month ago
Can you try two things please:
1) create an empty lavarel.log file in docker_image/storage/logs/
2) replace www-data with nginx: I looked at your PHP-FPM configuration, and this user is specified there.
TheOsch 1 month ago
ya it works. How did you figure this out ?
randomhelprequested 1 month ago
By adding RUN cat /etc/php7/php-fpm.d/www.conf near the end of the Dockerfile.
TheOsch 1 month ago
Thank TheOsch.
randomhelprequested 1 month ago
Thank you too
TheOsch 1 month ago

Using chown is more secure than using chmod, delete all your chmod and add:

RUN chown -R $USER /var/www/storage

I still get the same error. Thanks for trying.
randomhelprequested 1 month ago
View Timeline