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:
myuser
so I have to run sudo chown -R myuser ~/myproject
www-data
, I can't get to my website at localhost
- as seen in the post above.chown -R www-data:www-data /var/www/
, I lose myuser
permissions in the host and can't save files again, and vice versa.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:
myuser
), it will have the same identifiers as php-fpm