Laravel Sail Docker Permissions Issue: Files Created with Incorrect Ownership and Permissions

I’m experiencing persistent permission issues with my Laravel application running in a Docker container using Laravel Sail. Despite trying various solutions, I can’t seem to resolve the problem completely.

Environment

  • Host OS: Ubuntu 20.04.6
  • Docker version: 27.2.1
  • Docker Compose version: v2.29.1-desktop.1
  • Laravel Sail

The Problem When I create new files using Artisan commands (e.g., vendor/bin/sail artisan make:view), the files are created with -rw-r--r-- permissions and inconsistent ownership. This causes issues when I try to modify these files later.

What I’ve Observed All newly created files are owned by sail:sail when within the container. All files have -rw-r--r-- permissions.

Here’s a sample output:

-rw-r--r-- 1 sail sail   118 Oct 10 22:11 Example21.blade.php
-rw-r--r-- 1 sail sail    63 Oct 10 22:37 Example22.blade.php

Questions

  1. What could be causing this inconsistent behavior with file ownership and permissions?
  2. How can I ensure that all new files are created with the correct ownership (matching my host user, UID 1000) and with writable permissions?
  3. Is there a comprehensive solution that will address both existing files and ensure correct permissions for future files?

Any insights or solutions would be greatly appreciated. I’m happy to provide any additional information that might be helpful in resolving this issue.

What I’ve Tried

  1. Adjusting the WWWUSER and WWWGROUP in my .env file to 1000 to match my UID on my system.
  2. Modifying the docker-compose.yml file to include user mapping.
  3. Creating custom scripts to fix permissions.
  4. Rebuilding and restarting containers multiple times.

Despite these attempts, the core issue persists.

1. Configure WWWUSER in Laravel Sail to Match Host UID

The default WWWUSER (user for web-related files) inside Laravel Sail might be sail:sail, but you can adjust it to match the UID of your host user.

  1. **Set WWWUSER in the .env file:**Open your .env file in the root directory of your Laravel project and set WWWUSER to your current host UID. You can find your host UID by running:
id -u

Now add this in your .env file:

WWWUSER=1000  # Replace 1000 with your actual UID if different

Rebuild the Sail Container:

After setting the WWWUSER value, you need to rebuild your Docker containers to apply the change:

vendor/bin/sail build --no-cache
vendor/bin/sail up -d

Set Correct File Permissions and Ownership

Next, make sure that the correct permissions and ownership are set for your files both in the host and inside the container.

  1. **Adjust File Permissions:**Use chmod to allow writable permissions for your files. Here’s a basic example of how to apply chmod recursively in your project folder:
sudo chmod -R 775 /path_to_your_project
  • This will give group members write access as well, which might be needed depending on the user/group setup.
  • **Adjust File Ownership:**You also need to make sure the ownership of files matches your host UID. Assuming your host user has UID 1000, use chown to adjust ownership:
sudo chown -R $USER:$USER /path_to_your_project

Ensure Correct Ownership for Future Files

To avoid permission issues when using Artisan commands in the future, you can modify your Docker configuration to run processes inside the container with the same UID as your host user.

  1. **Modify docker-compose.yml:**Edit your docker-compose.yml file to map the user inside the container to the host’s UID. Add the following to the service where you’re having permission issues (typically laravel.test):
services:
  laravel.test:
    user: "${WWWUSER}:${WWWGROUP}"
  • This ensures that the processes inside the container run as the same user as your host system.
  • **Rebuild the Containers:**After making the changes, rebuild the Docker containers:
vendor/bin/sail down
vendor/bin/sail build --no-cache
vendor/bin/sail up -d

Handling Existing Files with Incorrect Permissions

For existing files that already have the wrong permissions, you can use the following commands to update their ownership and permissions:

# Change ownership to match host user (UID 1000)
sudo chown -R $USER:$USER /path_to_your_project

# Ensure that files are writable
sudo chmod -R 775 /path_to_your_project

Alternative: Use a Permission Fix Script

You can also create a simple script that will automatically fix permissions whenever needed.

Example script (fix-permissions.sh):

#!/bin/bash
# Fix ownership
sudo chown -R $USER:$USER /path_to_your_project

# Fix permissions
sudo chmod -R 775 /path_to_your_project

Run this script as needed:

bash fix-permissions.sh