How to solve Docker permissions in WSL under Windows

How to solve Docker permissions in WSL under Windows

I recently started doing some coding on Windows, and the reason is simple: comfort. When I’m on my Windows machine, I sometimes get a request from a client. If I have the time, I’ll jump into the code right away so I can reply quickly and stay responsive. This means I need to be able to work easily on both my Mac and Windows machines.

Your setup may differ, but I’ll share the example I use, and you can adapt it as needed.

Side note: I did try setting the user on the docker service as well as setting permissions on the files when the container starts. None of them fully worked: either editing files worked or the app itself.

For local development on Laravel projects, I use my custom starting setup: docker-nginx-php-mysql.

In this setup, when you bring up the container and access it, you’ll notice that the files are assigned ID of the owner and group (in my case, it was 1000) as it will pick up the user from WSL which does not exist in the container. You'll need to grab that number, open the FPM configuration file, and update it to use the same user and group. Specifically, modify these two lines in the file: www.conf.dist.

FROM:

user = www-data 
group = www-data

TO

user = 1000 
group = 1000

Then, restart the services, and it should work. If you still encounter permission issues, you might need to configure other services to use the same user you set for FPM.

Hope this helps!