WSL + Docker file permission issues

Viewed 1102

I'm using docker-compose in a WSL environment. Currently Wordpress runs in the docker composer. All files in the Projects folder are at www-data group and user which allows Wordpress to create everything and upload plugins.
But vs-code has file permission errors as soon as I want to edit files.

Switching user to and www-data groups allows vs-code to edit files but Wordpress has permission errors when uploading plugins or deleting plugins/files.

wsl.conf looks like that:

# Enable extra metadata options by default
[automount]
enabled = true
root = /windir/
options = "metadata,umask=22,fmask=11"
mountFsTab = false

# Enable DNS – even though these are turned on by default, we'll specify 
here just to be explicit.
[network]
generateHosts = true
generateResolvConf = true

Using Win 11 with Ubuntu-20.04 - Kernel: 5.10.60.1 and latest docker version.

EDIT:
Still, when I want to edit stuff in Wordpress, upload Media or uploading plugins I need to sudo chown -R www-data * and if I want tot edit files in VScode I need to sudo chown -R <username> *

2 Answers

I had a similar issue on my local WordPress development setup. (Windows 10 + WSL2), here is a link to the git repo: https://github.com/dorumarginean/wordpress-docker

For uploading images / plugins I updated the owner like you mentioned and for write permissions with Visual Studio Code I edited the PHP container inside my docker-compose.yml.

user: “1000:1000”

Here's what I did when I had a similar problem: create a new group that contains both www-data and the user that runs your editor.

Let's call your local user myuser and your new group mygroup for now.

First you need to create a new group

sudo addgroup mygroup

Add both the webserver user and your user to this new group

sudo adduser myuser mygroup
sudo adduser www-data mygroup

now give the ownership of the website directory to this new group. cd to your website's directory, then

sudo chown -vR :mygroup .

grant write permission to the group

sudo chmod -vR g+w .

Check to make sure that the changes took effect with ls -ld to see the owner and the permissions of the files and groups myuser www-data to see if both users are indeed in mygroup.

Then make sure to reinitialize the user environment so the permissions coming from the new group will be updated

newgrp - mygroup

You may need to log out and log in again for the user environment to update in the GUI.

Related