Permission Denied Error using Laravel & Docker
When you launch a fresh Laravel application inside a Docker container, encountering Permission denied errors immediately after deployment is almost always a symptom of ownership mismatch rather than code logic issues. The specific error message regarding /var/www/storage/logs/laravel.log indicates that the process attempting to write to the file lacks the necessary permissions. In your specific setup, this manifests because the PHP-FPM container and the Nginx container are running as different users, yet they share a mounted volume or directory structure where files have been created by one user but accessed by another.
In your current configuration, both containers define their WORKDIR as /var/www. When you execute ADD . /var/www in your Dockerfiles, the files are copied into the container filesystem with the ownership of the user running the build command (typically root). However, once the container starts, the PHP-FPM process runs inside a non-root user context. By default, this user is often www-data. Consequently, when Laravel attempts to log an error or write to the cache directory, the www-data user tries to access a file owned by root, triggering the stream open failure.
The core issue lies in how Docker handles filesystem permissions across isolated environments. Since your Nginx and App containers are separate entities communicating via TCP/IP (or Unix sockets), they do not inherently share a single kernel namespace where permission changes propagate automatically. The Nginx container creates or modifies files with its own user context, while the PHP-FPM container expects to write to those same locations. If the initial directory permissions are set to root:root, any process running as a non-root user will be denied access unless explicitly granted via file system attributes.
To resolve this without relying on Docker volume mounts that might persist across container rebuilds, you should adjust the ownership within your PHP-FPM container's initialization script. The most robust method is to update your App/Dockerfile to change the ownership of the storage, bootstrap/cache, and public directories immediately after the image build completes but before the application runs.
You can achieve this by adding an chown command to your PHP-FPM Dockerfile. Replace the final line in your App Dockerfile with the following:
RUN chown -R www-data:www-data /var/www/storage \
&& chown -R www-data:www-data /var/www/bootstrap/cache \
&& chmod -R 775 /var/www/storage \
&& chmod -R 775 /var/www/bootstrap/cache
This command ensures that the www-data user owns the directories and groups, while setting the directory permissions to 775. This grants read, write, and execute permissions to the owner and group, which is sufficient for Laravel's internal processes. It is crucial to specify the paths relative to your working directory (/var/www) rather than assuming they are in the root of the filesystem, as your Dockerfile explicitly sets the working directory.
Alternatively, if you prefer managing permissions at the host level using named volumes, you could mount a host directory to /var/www/storage in your docker-compose.yml. However, this approach introduces complexity regarding how files are created and modified between containers. If you use this method, ensure that the host directory has permissions that allow both the Nginx and PHP-FPM user IDs (UIDs) to write to it.
Another consideration involves the Nginx configuration. While your current setup uses a default.conf, ensure that the location block serving the storage logs or cache is not restricted by file permissions on the host machine if you are using bind mounts. If you stick to copying files into the container images (as shown in your Dockerfile), the chown approach within the image is generally preferred because it encapsulates the permission logic within the build process, ensuring consistency regardless of how many times you rebuild the containers.
By aligning the ownership of the writable directories with the user running the PHP-FPM worker processes, you eliminate the barrier that prevents Laravel from writing logs and cache files, resolving the Permission denied error effectively.
