How to Fix NPM Install Creating Root-Owned Files in Docker on WSL?

0
5
Asked By CuriousCoder92 On

I'm currently using Windows 10 with Ubuntu 24.04.1 in WSL, and I'm facing a frustrating issue with Docker. I initially had Docker Desktop but removed it and cleaned up my WSL. Now, I've set up Docker directly in WSL according to their documentation, including the necessary plugins for Docker Compose.

I have a straightforward Docker Compose file to run a Laravel project, and everything works well for the back end and database. However, I'm having trouble setting up React for the front end. When I run the command `docker compose run --rm npm install react`, it hangs for a while before finally completing the installation. The verbose output shows it's stalling at a dependency related to Tailwind CSS.

The major problem is that the `node_modules` folder and the generated `package-lock.json` file get created with `root:root` ownership on my host machine. This leads to permission issues when I try to execute commands like `npm run vite` on the Docker containers since they can't access these files. While I can change ownership afterward, it's frustrating that new files are always set to root.

I experimented with running Docker in rootless mode, which did fix the NPM ownership issue. However, this change now prevents my web service from accessing the mounted project files, which are also owned by root. I've double-checked that the user IDs and group IDs in the host and container are both set to 1000:1000.

Does anyone have suggestions on how to resolve this issue so that permissions are handled correctly?

2 Answers

Answered By DevDynamo On

It sounds like you're running into a common issue with file permissions in Docker. You might want to try changing your npm container's user to `node` in the Docker Compose file. This way, the files created during the npm install should belong to the `node` user rather than root. Just add `user: node` to your npm service configuration in your Docker Compose file and see if that helps!

FileFixerJake -

I tried that modification, but it throws a permission error saying `EACCES: permission denied, mkdir '/app/node_modules'`. Any thoughts on that?

Answered By TechTinker On

Another option would be to bind mount a directory from your local machine into the Docker container with the correct permissions already set. You could create the `node_modules` directory ahead of time and make sure it has the right ownership. Then mount that into your container. You could also specify the user ID in the Dockerfile for npm to use during installation, which might solve the ownership issue too.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.