sail: Storage folder write permissions fail on newly installed Sail

  • Sail Version: 1.4.9
  • Laravel Version: 8.33.1
  • PHP Version: 8.0
  • Database Driver & Version: MySQL 8.0.23-1debian10

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()

(...)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 100 (3 by maintainers)

Most upvoted comments

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.

@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.

@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.

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.

  • login to sail as root sail root-shell
  • give permission to user sail as chown sail -R storage/

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

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.

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

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.

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?

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

Thats it.It will resolve above issues.

using one of the artisan commands works for m

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

i like how this issue is “closed” yet still very active. classic laravel community.

Also experiencing this problem, Docker Desktop on Ubuntu 22. Don’t tell me not to use docker desktop, that’s not a solution. Write documentation that will allow your app/package to work where it is supposed to work: in docker. Docker desktop is an extremely common installation these days especially with it’s recent debut in Linux.

Do better.

This is still issue on my end

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

@pista7 Try this…

  1. Launch Docker Desktop (make sure it is running on your machine).
  2. Open a terminal, and type… docker context use default
  3. Get into your application’s root directory and type… docker ./vendor/bin/sail up

Let me know if that works. Oh, and send me a screenshot of the output of the following command… docker context ls

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

Hi all. I’m sorry, I unsubscribed after my original comment and didn’t see all the replies here. We’d be more than happy to accept a PR to the docs about some notes around this if anyone is willing to send one in. Sorry I didn’t give this ticket the attention it deserved.

Can’t believe this is still happening with a completely fresh and stock install of Linux and Docker.

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: @.***>

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.

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.

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.

@Dasher83 I am pretty sure this is what happened to me early this year. I was install docker on a lot of systems and early in that process I tried docker decktop for Linux and this is the only time I saw this issue. Using the Docker Engine is the way to go.

@matthiascw Same here: I did not find the time—nor the motivation—to delve into the reason(s) why that so-called « desktop-linux context » is not working with Sail. Honestly, I’m so busy at work, I can’t really get any further than this on that specific topic. But I’m still wondering why @cyrrill, the original author of this thread, has once considered that this issue was closed; I mean, even with my fix, this thread should remain open.

On the contrary, 2 years and 80+ comments later, I’m still following this thread hoping it actually gets fixed 😃

Thanks a lot @erictourigny23, changing the docker context also solved the issue for me: docker context use default

@pista7 @erictourigny23 Following your descriptions of the problem I was able to solve it here on Linux Pop OS. Thanks!

I figured out that installing Docker Desktop adds the context desktop-linux. And as @erictourigny23 already pointed out, the permission error occurs when you run docker with sail inside that context.

The reason for me why Docker is not running appeared was, that Docker Desktop is automatically changing the context to desktop-linux as soon as you start it (see https://docs.docker.com/desktop/faqs/linuxfaqs/#context). I did not find the time to dig deeper why the desktop-linux context is not working with sail though but probably the context is messing with the user.

Hello @erictourigny23! I can’t thank you enough for commenting on the solution to my problem. I installed ubuntu 22.04, laravel sail and tried to create something using sail artisan make: but encountered the Permission denied error. Your comment saying to change the docker context from desktop-linux to default solved my problem. Thank you very much again!

For anyone facing the same issue as the one described in the original posting, have a look at Docker contexts. Like images, containers, volumes, and networks, contexts constitute a fundamental entity in the Docker ecosystem.

Without delving too deeply into the details, I found out that the currently selected context affects the behaviour of Docker in many ways, especially in the way that it handles volumes.

Here’s my fix: set Docker’s context to “default”, as opposed to “desktop-linux”, through this command in your terminal…

docker context use default

Sail things up again, and everything should be working just fine.

Hence, the bash script that we curl to get a Sail-ready Laravel installation on a development host is designed to be used under the “default” context, as opposed to the “desktop-linux” context. I don’t know if that should be interpeted as a bug, but I wonder what is the logic behind having a “desktop-linux” context that is not working on a Linux desktop!!! Damn!

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

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

A similar error could occur if you cleaned the cache, not through the sail artisan config:clear command, bus used php from your system php artisan config:clear. Clear the cache using sail container.

I bumped into this as well and read this issue seeking for help. I’m running an Ubuntu 22.04, fresh install. Like most, I installed Docker through Docker desktop.

The suggestions around changing the docker context lead me to uninstall Docker Desktop and to install Docker Engine the way we used to before Docker desktop, through Docker’s apt repo (https://docs.docker.com/engine/install/ubuntu/).

I also made the known configurations to be able to run Docker Engine CLI commands without sudo.

With this, the problem went away for me. I know it’s not really a solution but until there is one, maybe this will help other people like me trying to learn Laravel from an Ubuntu machine.

Not sure if this is related, but I got some issues myself when using docker desktop on ubuntu 22.04. I found this issue on docker desktop for linux: https://github.com/docker/desktop-linux/issues/31

Which in short tells us that they mount root to the current user, and other users inside the container gets a user and group id based on some range. So even if the sail user has id 1000, and the host machine user has id 1000, it will not be the same id when mounted via docker desktop. In my case i got 100999 as the user id

My workaround (from the tips in the issue) was to create a group with the actual id (100999 in my case) and give that group to my account as well. To get it fully working with sail, i then changed the user and group to id 100999 on all the files, as well as giving group write access.

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 😃

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

Is chmod 777 really the only option we have?