2

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.
pileup
  • 1
  • 2
  • 18
  • 45

1 Answers1

6

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
Anthony Aslangul
  • 3,589
  • 2
  • 20
  • 30
  • 1
    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`? – pileup Aug 21 '21 at 12:05
  • 2
    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. – Anthony Aslangul Aug 21 '21 at 12:12
  • 1
    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? – pileup Aug 21 '21 at 12:23
  • 1
    Yes that's the idea :) – Anthony Aslangul Aug 21 '21 at 12:26
  • 1
    You helped me many times already, you are a real expert, thank you! – pileup Aug 21 '21 at 12:27
  • 1
    Oh you are the guy from the other day with the Nextjs app, I remember now :p You know there is no magic, I also had a hard time with all these (many many many) little traps. I wish you good luck in your docker journey. – Anthony Aslangul Aug 21 '21 at 12:30
  • Yes! Thank you again, now I have a properly running dev environment using 4 Docker containers with nginx, mysql, laravel as api and nextjs as frontend – pileup Aug 21 '21 at 12:45