3

I already saw the solution from this post: "https://stackoverflow.com/questions/50552970/laravel-docker-the-stream-or-file-var-www-html-storage-logs-laravel-log-co

But this solution is not sufficient

The problem is that if I do what is said in the solution, i.e. to run chown -R www-data:www-data * inside the Docker container - it also changes the permission on the actual folder in the Ubuntu host, not just the container, because I set this folder in the docker-compose.yml file:

php:        
    build:
        context: ./laravel
        dockerfile: Dockerfile
    container_name: laravel
    volumes:
        - ./laravel:/var/www/html

and this is the Dockerfile:

FROM php:fpm-alpine

RUN docker-php-ext-install pdo pdo_mysql

and my user in the Ubuntu host is myuser so when I run the chown -R www-data:www-data *, myuser no longer has permissions on the host, and I can't save files.

So I either get Permission denied on the localhost URL (as seen in the other post), or I get Permission Denied to save files on VS Code on my Ubuntu host! (I am using WSL2, that's why I can use VS Code)

To sum it up:

  1. To be able to save files on my mounted volume, I have to save it as the Ubuntu user, i.e. myuser so I have to run sudo chown -R myuser ~/myproject
  2. But because certain files in Laravel expects writable permission by www-data, I can't get to my website at localhost - as seen in the post above.
  3. If I change the permissions in the Docker container using chown -R www-data:www-data /var/www/, I lose myuser permissions in the host and can't save files again, and vice versa.
2
  • 1
    What is the base image for your Dockerfile? Commented Aug 21, 2021 at 11:44
  • php-fpm, edited the post as well Commented Aug 21, 2021 at 11:56

1 Answer 1

9

In your docker-compose.yml file, add the user: <uid>:<gid>

php:        
    build:
        context: ./laravel
        dockerfile: Dockerfile
    container_name: laravel
    user: "1000:1000" #type the "id" command in your terminal and look for uid and gid if you don't know what they are
    volumes:
        - ./laravel:/var/www/html

This way, php-fpm will be executed as a user with these identifiers:

  • when you save a file from your host (with myuser), it will have the same identifiers as php-fpm
  • when php write a file, it will be written with the same identifiers too
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you! What exactly is the issue then? Some posts say Laravel expects www-data writable permissions on this laravel.log file, and some posts don't mention it. And with your solution, I don't even set the user to www-data, so how can it work, if it expects www-data?
Laravel doesn't "need" the user to be www-data, it just needs the user who runs php-fpm to have read and write perm. on the files of your project. The easiest way to solve issue is then to set your project user to www-data since it is the default user of php-fpm. However, this approach has some cons, and you encountered one of them. In a dev env, using the user option as we did here is perfectly fine. Quick note, if you ssh into the container, the user will be "I have no name!". It is ok, you can ignore that. This is because there is no actual user with the id you provided.
Thank you! So you mean that setting the project user to www-data in the container is probably for production where the container is not touched, and the project files are copied to it rather than symlinked?
Yes that's the idea :)
You helped me many times already, you are a real expert, thank you!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.