Permission denied with fopen() and fwrite() in Docker LAMP stack

Hi everyone,

Excited to have found this community!
So I’ve been trying to build a LAMPstack application with Docker and Docker-compose but somehow I get stuck when I try to use php fopen() and fwrite() to basically create a new file in a subdirectory.
These are the errors I would get in the browsers:

Warning: fopen(users.txt): Failed to open stream: Permission denied in /var/www/html/filehandling.php on line 13
Fatal error: Uncaught TypeError: fwrite(): Argument #1 ($stream) must be of type resource, bool given in /var/www/html/filehandling.php:15 Stack trace: #0 /var/www/html/filehandling.php(15): fwrite(false, 'Brad') #1 {main} thrown in /var/www/html/filehandling.php on line 15

This is filehandling.php:

<?php $file = 'var/www/html/extras/users.txt'; if(file_exists($file)){ $handle = fopen($file, 'r'); $contents = fread($handle, filesize($file)); fclose($handle); echo $contents; } else { $handle = fopen($file, 'w'); $contents = 'Brad'; fwrite($handle, $contents); fclose($handle); } These are the screenshots of the Dockerfile and the Docker-compose.yml file: https://i.stack.imgur.com/K82j0.png https://i.stack.imgur.com/ckXZk.png Some say it's a PHP problem, others say it's a Docker permissions problem. One way or the other, I really need some help. Can somebody walk me through the steps to get to a solution? Thanks so much in advance!

Good morning!

To have a clean setup and to not expose my docker-compose.yml, Dockerfile, … to the outside world using the webserver I would put all the webcontent into its own subdirectory and mount this directory as /var/www/html into the container. But this is not the reason for your php-script not working as expected and displaying an error-message. It is just for a cleaner setup. In addition (and for security reasons) I would store local data (created/written/read by the PHP-application) in its own subdirectory so I can remove the write-rights from the PHP-scripts and its directory.
Above is only for security-reasons and not for the error-message itself.

An error-message normally contains at least some truth - so let’s try to find out who needs rights on which directory and how to check.
From your screenshots I assume you are using Linux.
First Docker needs rights to write to the directory on your local computer - can be checked with ls -la. Easiest way would be to set the directory-mask to 777 but this is not the most secure way as everybody on this computer is able to write to this directory. So you might set the group of this directory to the correct group. For troubleshooting you can set the directory-permissions to 777, let PHP write a file to this directory and then check the owner/group for this file on you local filesystem.
In addition you can open a shell within a running container and check within what the permissions are for the directory /var/www/html/ within the container. To open a shell you can run a command like this: docker exec -it <containername|containerid> bash. If bash is not available try ash instead. Now you are within the container and can check the directory-permissions there.

Hope this provides an idea what/where to check?