laravel / sail

Docker files for running a basic Laravel application.
https://laravel.com/docs/sail
MIT License
1.65k stars 473 forks source link

Storage folder write permissions fail on newly installed Sail #81

Closed cyrrill closed 3 years ago

cyrrill commented 3 years ago

Description:

Unable to write to storage folder.

image

Steps To Reproduce:

I have installed Sail into an existing Laravel 8 application as per the installation guide. The container and associated services come up and yield no errors.

image

When I attempt to access the application from the browser, the exception mentioned above shows up.

Normally, all you need to do is chmod and/or chown the /storage/ folder in order for Laravel to run.

When I try to publish the Dockerfiles (in order to attempt to set permissions), I get this other exception:

 ./vendor/bin/sail artisan sail:publish -vvv

   League\Flysystem\Exception 

  Impossible to create the root directory "/var/www/html/docker/7.4".

  at vendor/league/flysystem/src/Adapter/Local.php:112
    108▕             clearstatcache(false, $root);
    109▕ 
    110▕             if ( ! is_dir($root)) {
    111▕                 $errorMessage = isset($mkdirError['message']) ? $mkdirError['message'] : '';
  ➜ 112▕                 throw new Exception(sprintf('Impossible to create the root directory "%s". %s', $root, $errorMessage));
    113▕             }
    114▕         }
    115▕     }
    116▕ 

  1   vendor/league/flysystem/src/Adapter/Local.php:133
      League\Flysystem\Adapter\Local::ensureDirectory()

  2   vendor/league/flysystem/src/Filesystem.php:102
      League\Flysystem\Adapter\Local::write()

  3   vendor/league/flysystem/src/MountManager.php:597
      League\Flysystem\Filesystem::put()

  4   vendor/laravel/framework/src/Illuminate/Foundation/Console/VendorPublishCommand.php:254
      League\Flysystem\MountManager::put()

  5   vendor/laravel/framework/src/Illuminate/Foundation/Console/VendorPublishCommand.php:238
      Illuminate\Foundation\Console\VendorPublishCommand::moveManagedFiles()

  6   vendor/laravel/framework/src/Illuminate/Foundation/Console/VendorPublishCommand.php:203
      Illuminate\Foundation\Console\VendorPublishCommand::publishDirectory()

  7   vendor/laravel/framework/src/Illuminate/Foundation/Console/VendorPublishCommand.php:166
      Illuminate\Foundation\Console\VendorPublishCommand::publishItem()

(...)
cyrrill commented 3 years ago

I've rebuilt the images to the PHP 7.4 version, used sail build --no-cache and the same permissions issue persists.

driesvints commented 3 years ago

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.

cyrrill commented 3 years ago

@driesvints,
"Please only use this issue tracker for reporting bugs with the library itself."

I installed Sail exactly step by step as instructed in the official guide, and it reports a malfunction upon first use, I would classify this as a bug.

driesvints commented 3 years ago

@cyrrill I can write to the storage folder just fine and this is the first report we've gotten on it. Please try a support channel.

cyrrill commented 3 years ago

Currently on the host the storage folder is at 775, and when I chmod 777 -R storage, Sail works, but that carries the obvious security issues.

The issue appears possibly to be with the WWWGROUP & WWWUSER env vars.

WWWUSER is set to 1000 and WWWGROUP shows undefined.

Don't really have time to delve into making this work at the moment, was hoping it would work out of the box. (and it likely does for freshly installed L8 apps)

Thanks for getting back to me though, will try this another time or check on Laracasts as you suggest :+1:

cyrrill commented 3 years ago

For reference this is not an isolated issue and has been reported before:

https://laracasts.com/discuss/channels/laravel/groupadd-invalid-group-id-sail

https://laracasts.com/discuss/channels/general-discussion/laravel-sail-cannot-mkdir-permission-denied

MaybeThisIsRu commented 3 years ago

@cyrrill Are you running Docker in rootless mode?

cyrrill commented 3 years ago

Hi @hirusi, thanks for following up on this. On first attempt I was running it as default which requires root, given that the Dockerfile requires access to port 80 which is privileged on Linux. After seeing the issue with the permissions, I installed rootless mode and allowed port 80 via net.ipv4.ip_unprivileged_port_start=0 in sysctl. The issue persists: a storage folder with 775 permissions on the host appears non-writable to the sail user inside the container. So far, the only way to allow writes to storage is by setting it to 777 on the host.

MaybeThisIsRu commented 3 years ago

I'm writing down my discoveries and attempts to make 775 work over here, but unfortunately, like you, I haven't had a breakthrough on a clean solution yet.

I'm not sure how far I'll push my attempts, but I have subscribed to the thread here and would love to keep up with what anyone else is attempting.

Edit: I saw you mentioned somewhere up that "it probably works fine on a fresh L8 app." That's not the case for me.

Martijn-Bos commented 3 years ago

This bug is also present here. I can't write to the laravel.log in the storage folder after going through the Laravel installation guide. I'm using WIN10,docker desktop+ ubuntu20.04 WSL2.

This is not an isolated incident

NicolasPereira commented 3 years ago

I have this problem whenever I use laravel sail, both in new and old applications!

I use Linux Ubuntu 20.04

I fix this problem these problems using inside the project repository:

$ sudo chmod -R 777 storage

I tried to write this command using sail bash and was unsuccessful!

This is not an isolated incident

peterbabic commented 3 years ago

I also have many many issues with permissions with:

Not sure which user or group are these files/folders meant to belong to. No issues on a machine running Arch however.

cyrrill commented 2 years ago

Hi @driesvints,

There have been 4 other people having this issue. Any chance you could consider having a look again? Or at the very least update the documentation to explain how this is supposed to work?

cyrrill commented 2 years ago

Is chmod 777 really the only option we have?

tomheadifen commented 2 years ago

I've had this issue with WSL2.

The Problem

Laravel Sail executes all commands using the 'sail' user and you can see this in the vendor/laravel/sail/bin/sail file. The sail user is setup by default when the container is created. The issue is that the html folder has the group and user set to 'root' so whenever you try and run a sail command that needs permissions you run into an error.

The solution I did was to change the owners and groups of the html folder to their respective group so that the sail command will work.

Temp fix

You should now stop running into permission errors on any sail command you run such as when you install jetstream.

Potential Permanent Solution

My guess is that on mac this problem doesn't happen because the user groups are set up correctly however on WSL2 they are set as root. Perhaps someone on a mac can let me know what the users and groups are set to for the html folder in the docker container. If they aren't set to root by default that could be the bug.

Edit: This ended out being related to me running WSL2 as a root user instead of my regular user.

joveice commented 2 years ago

I have installed docker the recommended way from docker docs. Followed https://laravel.com/docs/8.x/installation#getting-started-on-linux Sail Version: 1.11.0 Laravel Version: 8.63.0 PHP Version: 7.4.24 OS: Fedora 34

After the install was done I did sail up and headed to localhost and was presented with the same message.

Tmp fix from @tomheadifen by changing the owner of the project worked for me

klipitkas commented 2 years ago

This happens to me (Ubuntu 20.04 - docker Server Version: 20.10.1) as well when using sail, there is definitely something wrong with permissions here.

tomheadifen commented 2 years ago

Ok I've figured out my issue. I was running as wls2 as a root user and sail is not built to be running as the root user.

Solutions

  1. Change back to your regular user instead of root.
  2. Do the above steps in my previous post to continue running as root.
NicolasPereira commented 2 years ago

Hi guys, how are you?

I believe I have figured out the problem to solve the problems, at least on linux!

For MacOS and Windows it should have similar configuration but I didn't perform the tests.

When we are running something with docker, we need to inform that our user "external" to the service container has permission to create, change files inside the service container.

To do this, just run this comand in your terminal before run sail up

$ export WWWUSER=${WWWUSER:-$UID}
$ export WWWGROUP=${WWWGROUP:-$(id -g)}

I found this solution in this question on StackOverflow

If you don't want to export these variables, you can use the following configuration within the service that runs the application in your docker-compose.yml

        environment:
            WWWUSER: '1000'

A point of attention, the value 1000 is my user's UID, follow the article to understand about UID and how to find your user's UID

If I knew how Sail works I would create a PR with these updates, it would be interesting if Laravel's team look my solution

ps: Sorry for my English, I'm learning and I used google translate to help me

Regards,

Nicolas

LiamKarlMitchell commented 2 years ago

Solves it, and if your user id is not 1000 just set it in .env file.

    environment:
      WWWUSER: '${WWWUSER:-1000}'
maritaria commented 2 years ago

For me the problem in the end was that I was using WSL2 with root as default user. The runtime avoids updating the sail user id if it is zero (code). So if you are hell-bound on using root user, then you'll have to publish the sail files and patch the start-container script of the php runtime you are using.

I'm just going back to a regular user account instead of keeping on using root.

LoneWolf1227 commented 2 years ago

I also have this problem with permissions in sail(

Kane-R-G commented 2 years ago

using one of the artisan commands works for m

php artisan optimize <--- this did not work php artisan optimise:clear <-- this one does work.

xpharsh commented 2 years ago

If you are using Sail (Docker) then try this command

sail artisan storage:link

It's Solve my storage folder access issue, I am using Laravel 9.

aldesrahim commented 2 years ago

Hi, I have been struggling with this issue recently, I am using windows 10 and WSL2 and start/run sail container from Docker Desktop, turns out it was a mistake, I should have run it from WSL by running sail up -d command since when you look at script file it contains what @NicolasPereira mentions, and it's been running fine.

techinfocomp commented 2 years ago

Hi, I have been struggling with this issue recently, I am using windows 10 and WSL2 and start/run sail container from Docker Desktop, turns out it was a mistake, I should have run it from WSL by running sail up -d command since when you look at script file it contains what @NicolasPereira mentions, and it's been running fine.

Have you tried this ? sail artisan storage:link

aldesrahim commented 2 years ago

Yes, and it worked !

On Thu, Mar 24, 2022, 2:30 PM Techinfocomp @.***> wrote:

Hi, I have been struggling with this issue recently, I am using windows 10 and WSL2 and start/run sail container from Docker Desktop, turns out it was a mistake, I should have run it from WSL by running sail up -d command since when you look at script file it contains what @NicolasPereira https://github.com/NicolasPereira mentions, and it's been running fine.

Have you tried this ? sail artisan storage:link

— Reply to this email directly, view it on GitHub https://github.com/laravel/sail/issues/81#issuecomment-1077319758, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI3BT3BAMLIZ5GFYFY53FXTVBQK2PANCNFSM4ZYXY44Q . You are receiving this because you commented.Message ID: @.***>

xpharsh commented 2 years ago

Yes, and it worked ! On Thu, Mar 24, 2022, 2:30 PM Techinfocomp @.> wrote: Hi, I have been struggling with this issue recently, I am using windows 10 and WSL2 and start/run sail container from Docker Desktop, turns out it was a mistake, I should have run it from WSL by running sail up -d command since when you look at script file it contains what @NicolasPereira https://github.com/NicolasPereira mentions, and it's been running fine. Have you tried this ? sail artisan storage:link — Reply to this email directly, view it on GitHub <#81 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI3BT3BAMLIZ5GFYFY53FXTVBQK2PANCNFSM4ZYXY44Q . You are receiving this because you commented.Message ID: @.>

That's Nice. Have a good day.

aldesrahim commented 2 years ago

Glad to hear it!

On Thu, Mar 24, 2022, 10:00 PM Harsh Gupta @.***> wrote:

Yes, and it worked ! … <#m4874753885818855053> On Thu, Mar 24, 2022, 2:30 PM Techinfocomp @.> wrote: Hi, I have been struggling with this issue recently, I am using windows 10 and WSL2 and start/run sail container from Docker Desktop, turns out it was a mistake, I should have run it from WSL by running sail up -d command since when you look at script file it contains what @NicolasPereira https://github.com/NicolasPereira https://github.com/NicolasPereira https://github.com/NicolasPereira mentions, and it's been running fine. Have you tried this ? sail artisan storage:link — Reply to this email directly, view it on GitHub <#81 (comment) https://github.com/laravel/sail/issues/81#issuecomment-1077319758>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI3BT3BAMLIZ5GFYFY53FXTVBQK2PANCNFSM4ZYXY44Q https://github.com/notifications/unsubscribe-auth/AI3BT3BAMLIZ5GFYFY53FXTVBQK2PANCNFSM4ZYXY44Q . You are receiving this because you commented.Message ID: @.>

That's Nice. Have a good day.

— Reply to this email directly, view it on GitHub https://github.com/laravel/sail/issues/81#issuecomment-1077725091, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI3BT3B6B7A5A6557E2AX33VBR7RJANCNFSM4ZYXY44Q . You are receiving this because you commented.Message ID: @.***>

jobnomade commented 2 years ago

I've had this issue with WSL2.

The Problem

Laravel Sail executes all commands using the 'sail' user and you can see this in the vendor/laravel/sail/bin/sail file. The sail user is setup by default when the container is created. The issue is that the html folder has the group and user set to 'root' so whenever you try and run a sail command that needs permissions you run into an error.

The solution I did was to change the owners and groups of the html folder to their respective group so that the sail command will work.

Temp fix

  • In WSL2 jump into the docker container as root by running this command vendor/bin/sail root-shell.
  • Move up one directory cd...
  • Recursively change the owner and group of the html folder to sail chown -R sail:sail html.
  • Confirm the change ls -la. You should see the group and user sail on all the files and folders.
  • Exit the container exit.

You should now stop running into permission errors on any sail command you run such as when you install jetstream.

Potential Permanent Solution

My guess is that on mac this problem doesn't happen because the user groups are set up correctly however on WSL2 they are set as root. Perhaps someone on a mac can let me know what the users and groups are set to for the html folder in the docker container. If they aren't set to root by default that could be the bug.

Edit: This ended out being related to me running WSL2 as a root user instead of my regular user.

I am on MacOs. I am currently using Homestead and wanted to try out Sail for collaboration purposes with other developers. However, I receive also permission errors on the storage folder when I run my tests. I followed your steps and see that on my MacOs all folders are set to roo:root.

My tests still doesn't have access to write into the storage cache folder

file_put_contents(/storage/framework/cache/data/39/11/3911a04b5aa25edb5fea9459cf7640e0db99522c): failed to open stream: No such file or directory

Looks like some other problems under the hood.

profesone commented 2 years ago

I changed the web root to have 'sail' user permissions. I answered this here.

ali-shabani commented 2 years ago

If you are using sudo to call the sail command, setting WWWUSER in the environment file (.env in your work directory) with your user id will fix your problem.

To get your user id in the command line, type echo $UID or id -u.

if you are using docker in rootless mode there is no workaround to solve this (actually, it is but not a straight solution, and I don't think maintainers will change anything according to that)

sajendramanandhar commented 2 years ago
MeepMeeps commented 2 years ago

This is still an issue. I had a windows dev env where I was working previously; Attempted to pull the project and "sail up" on my ubuntu 22 laptop, has all the issues listed above. Created a new project and tried again, same problem. Got a hold of one the spare laptops at work and retested with ubuntu 22 on a clean machine, same issue. Swapped the OS to arch all works as it should.

This is a bug.

tonywei92 commented 2 years ago

This is still issue on my end

valentin-morice commented 2 years ago

Still have the issue with Docker Desktop on Ubuntu, using docker-engine installed per the docs (https://docs.docker.com/engine/install/ubuntu/) and running as non-root (https://docs.docker.com/engine/install/linux-postinstall/) solves it, although now I do not have a GUI for docker.

articleten-andy commented 2 years ago

Still seems to be an issue when installing a fresh on Ubuntu running on WSL.

The site reports that it can't write to log file until i chmod to 777.

But I also can't run ./vendor/bin/sail composer update as that complain about

In Filesystem.php line 851: file_put_contents(/var/www/html/vendor/composer/installed.php): Failed to open stream: Permission denied

marensas commented 2 years ago

@articleten-andy just use @sajendramanandhar answer above by changing vendor folder owner, not storage.

xpharsh commented 2 years ago

Solution for Linux System

OS: Ubuntu 22.04 PHP 8.1.2 (cli)

Freshly install Laravel with sail, It can also work on previously install system.

Open Terminal and move to your Laravel working directory.

Start the server with sail

type:

./vendor/bin/sail up -d

type:

./vendor/bin/sail root-shell

when you are inside the container which is something look like this

root@33376b066b99:/var/www/html#

type:

php artisan storage:link

my issue resolved with above steps

nastoychev commented 2 years ago

OS: Ubuntu 22.04.1 If i run sail using Docker Desktop context the files mounted are owned by root:root, but when i start sail with docker engine default context, everything works fine and permissions are correct mapped to sail user, so I think maybe it can be a Docker Desktop Issue

graphicalx commented 1 year ago

Still have the issue with Docker Desktop on Ubuntu, using docker-engine installed per the docs (https://docs.docker.com/engine/install/ubuntu/) and running as non-root (https://docs.docker.com/engine/install/linux-postinstall/) solves it, although now I do not have a GUI for docker.

Ladies and gentlemen, this ^ is THE solution. Make sure you properly remove Docker Desktop and remove it from .docker/config.json, and install Docker Engine + follow the Post install and log in to your Linux again. For good measure I installed Docker Compose again too, but that might not be needed

warmwhisky commented 1 year ago

Still have the issue with Docker Desktop on Ubuntu, using docker-engine installed per the docs (https://docs.docker.com/engine/install/ubuntu/) and running as non-root (https://docs.docker.com/engine/install/linux-postinstall/) solves it, although now I do not have a GUI for docker.

Ladies and gentlemen, this ^ is THE solution. Make sure you properly remove Docker Desktop and remove it from .docker/config.json, and install Docker Engine + follow the Post install and log in to your Linux again. For good measure I installed Docker Compose again too, but that might not be needed

It does say to uninstall all versions of docker, but it doesn't tell you how? I've spent the last 7 hours trying to solve this and believe I may have different/incorrect versions installed, but no idea how to uninstall. Maybe they don't tell you because there are a billion ways to do this?

valentin-morice commented 1 year ago

Still have the issue with Docker Desktop on Ubuntu, using docker-engine installed per the docs (https://docs.docker.com/engine/install/ubuntu/) and running as non-root (https://docs.docker.com/engine/install/linux-postinstall/) solves it, although now I do not have a GUI for docker.

Ladies and gentlemen, this ^ is THE solution. Make sure you properly remove Docker Desktop and remove it from .docker/config.json, and install Docker Engine + follow the Post install and log in to your Linux again. For good measure I installed Docker Compose again too, but that might not be needed

It does say to uninstall all versions of docker, but it doesn't tell you how? I've spent the last 7 hours trying to solve this and believe I may have different/incorrect versions installed, but no idea how to uninstall. Maybe they don't tell you because there are a billion ways to do this?

Try this: https://askubuntu.com/questions/935569/how-to-completely-uninstall-docker

martinmurciego commented 1 year ago

Greetings I want to consult with those who know. I recently started adding functionality to a project developed in laravel, which has sail. And I thought it was great to use docker-compose to mount it in test as well as production if you could. But I ran into lots of permission denied errors and especially in storage folder or log file etc. The only thing that worked for me is sometimes to put 777 to storage. What helped me not to find those permission errors was to move the project folder with laravel/sail to the /tmp directory --on linux of course, I didn't try on windows 10-- and everything works there.

AkselMeola commented 1 year ago

So maybe people are like me and after running curl https://laravel.build/foo | bash I went to make coffee until the docker image builds. After which I was greeted with a shiny line: Thank you! We hope you build something incredible

But before that there was a line sudo: timed out reading password, which I obviously ignored since the last line seemed like success :) So perhaps handling the return of this could be improved ..

Anyway...

If you used to install Laravel using the curl https://laravel.build/foo | bash method, then at the very end this script, it should set the owner permissions.

Install script

....

if sudo -n true 2>/dev/null; then
    sudo chown -R $USER: .
    echo -e "${BOLD}Get started with:${NC} cd foo && ./vendor/bin/sail up"
else
    echo -e "${BOLD}Please provide your password so we can make some final adjustments to your application's permissions.${NC}"
    echo ""
    sudo chown -R $USER: .
    echo ""
    echo -e "${BOLD}Thank you! We hope you build something incredible. Dive in with:${NC} cd foo && ./vendor/bin/sail up"
fi

...

So try running sudo chown -R $USER: . inside project dir manually. From what I can see the sail up should pick up your user on start and hopefully all should be well :)

lupinitylabs commented 1 year ago

Hi guys, how are you?

I believe I have figured out the problem to solve the problems, at least on linux!

For MacOS and Windows it should have similar configuration but I didn't perform the tests.

When we are running something with docker, we need to inform that our user "external" to the service container has permission to create, change files inside the service container.

To do this, just run this comand in your terminal before run sail up

$ export WWWUSER=${WWWUSER:-$UID}
$ export WWWGROUP=${WWWGROUP:-$(id -g)}

I found this solution in this question on StackOverflow

If you don't want to export these variables, you can use the following configuration within the service that runs the application in your docker-compose.yml

        environment:
            WWWUSER: '1000'

A point of attention, the value 1000 is my user's UID, follow the article to understand about UID and how to find your user's UID

If I knew how Sail works I would create a PR with these updates, it would be interesting if Laravel's team look my solution

ps: Sorry for my English, I'm learning and I used google translate to help me

Regards,

Nicolas

For anyone facing this issue with the owner being 1000:sail instead of sail:sail, you have probably started the container using Docker (Desktop) instead of using sail up.

Use sail up and the owner will be set correctly - no need to manually set owners or change the WWWUSER.

legendarylion commented 1 year ago

I was experiencing the same issue on windows, and posted this same answer on a topic I found during my search on SO.

Do we know why Docker Desktop doesn't set up the permissions correctly? Do the Docker files for Sail need to be updated to handle this issue?

nteej commented 1 year ago

Simply: $ sail shell chmod 777 -R storage chmod 777 -R bootstrap/cache

Thats it.It will resolve above issues.

GonzaloGPF commented 1 year ago

I'm facing same issue. Problems with permissions when running any artisan command. I'm on Windows 11 with WSL2. I'm new with Laravel Sail, I've always worked with Laravel Homestead and I decided give a try to Laravel Sail, however, it's not working :( Please someone help

legendarylion commented 1 year ago

Don't run it with the Docker Desktop. Run it from command line, and you should be good.

On Tue, Nov 22, 2022, 4:27 PM Zalo @.***> wrote:

I'm facing same issue. Problems with permissions when running any artisan command. I'm on Windows 11 with WSL2. I'm new with Laravel Sail, I've always worked with Laravel Homestead and I decided give a try to Laravel Sail, however, it's not working :( Please someone help

— Reply to this email directly, view it on GitHub https://github.com/laravel/sail/issues/81#issuecomment-1324256715, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFTAHIMZOEWHLKLHS4ZRXCLWJU3D7ANCNFSM4ZYXY44Q . You are receiving this because you commented.Message ID: @.***>