phplaraveldockeruser-permissions

How do I switch users in a Laravel 12 docker container?


I use provided by Laravel 12 docker container version 8.3 and run it on Ubuntu 22.04.

When I run php from command line and create files (php artisan console commands create and read some files and folders), the owner of these files is root. But when I do the same from web (from routes or API) the owner of created files and folders is sail.

How I can force this container to use same user for both cli and web?

I need created files and folders to have same permissions and owner regardless they were created from CLI or route requests.


Solution

  • I've hit the same problem myself, and generally I use chmod and chown to correctly set permissions after I've run the command inside my container. This is only ok because I'm running the container locally and don't care about the side effects, I wouldn't do it in production.

    The easiest way to solve it is to use the su command in your docker container to switch user contexts like this:

    su sail
    

    This will cause future commands to be run as the sail user. You can hit CTRL + D afterward to exit and return to your previous root shell.