6

I have created a react app and trying to run it over the docker container with volumes (mapping content inside the container with outside files), everything was working fine earlier but now facing an issue as shared. Can anyone help me with that? This is a permission issue but doesn't know how to resolve that. root user has access of node_modules folder. How to give access to node user ?

My docker file

FROM node:alpine

USER node
WORKDIR '/home/node'

COPY package.json .
RUN npm install

COPY . .

CMD ["npm", "run", "start"]

Commands used:

docker build -t frontend -f Dockerfile.dev .
docker run -p 3000:3000 -v /home/node/node_modules -v $(pwd):/home/node frontend:latest

project structure

Error: enter image description here

Access in container:

~ $ ls -l
total 1488
-rw-rw-r--    1 node     node           124 Jun 20 08:37 Dockerfile.dev
-rw-rw-r--    1 node     node          3369 Jun 17 18:25 README.md
drwxr-xr-x    3 node     node          4096 Jun 17 18:45 build
-rw-rw-r--    1 node     node           230 Jun 20 06:56 docker-compose.yml
drwxrwxr-x 1041 root     root         36864 Jun 20 19:15 node_modules
-rw-rw-r--    1 node     node       1457680 Jun 18 18:28 package-lock.json
-rw-rw-r--    1 node     node           811 Jun 17 18:26 package.json
drwxrwxr-x    2 node     node          4096 Jun 17 18:25 public
drwxrwxr-x    2 node     node          4096 Jun 17 18:25 src
Rishabh Sharma
  • 341
  • 1
  • 2
  • 9
  • try running `chmod 777 /app/node_modules`, and also please do not post pictures in your questions, copy the text over here – Mogi Jun 20 '21 at 07:40
  • This command is not working, showing operation not permitted. I have added few more details to question if it helps to resolve. – Rishabh Sharma Jun 20 '21 at 19:21
  • did you try it with `sudo`? like `sudo chmod 777 /app/node/node_moduels` – Mogi Jun 21 '21 at 09:36
  • Do you have a `.dockerignore` file that excludes the host's `node_modules` directory from the image? The `docker run -v` options replace the image content with things from other places, which makes this setup not especially reproducible; does deleting these options help? – David Maze Jun 21 '21 at 09:41
  • None of the above-mentioned approaches worked. – Rishabh Sharma Jun 22 '21 at 11:29

7 Answers7

18

It is clear that node_modules folder in container is built by root user during the step npm install, therefore has root as user. This is the reason we don't have access to that folder when we set up our node user. To resolve this what we have to do is firstly using the root user we have to give permission to the node user while copying files from local directory to image and then later set up node as the user as shown below:

COPY --chown=node:node package.json .
RUN npm install

COPY --chown=node:node . .
USER node
Rishabh Sharma
  • 341
  • 1
  • 2
  • 9
1

This file is not a very important file that can cause major application failures if removed. its just a cache file created by external dependencies using eslinter. You can just safely remove by running

sudo rm /home/$USER/path-to-your-project/node_modules/.cache/.eslintcache 
Andrew Mititi
  • 933
  • 12
  • 11
  • In my case the server wouldn't start if the EACCESS error occured during build... I had to fix the permissions. It seems ignoring it is not always an option! – JM Lord Dec 07 '21 at 16:00
0

Create a .eslintignore file and put * in it.

Omar Elweshy
  • 140
  • 3
  • 8
0

if you'r running docker-compose, changing the Dockerfile when npm installing worked for me after a lot of investigation

RUN cd /usr/src/app && npm install
geuxor
  • 21
  • 7
0

Just insert below line in your Dockerfile:

RUN chmod 777 /app/node_modules

before line:

CMD ["npm", "run", "start"]

Rebuild it. Do not need to touch anything else.

helvete
  • 2,455
  • 13
  • 33
  • 37
Levent
  • 1
  • Granting 777 is more a work-around. Definitely works. Cause is that the file owner and user in context are different. Appreciate your participation. – nashter Aug 06 '22 at 14:27
0

This error was haunting me while I was developing a react web app , So here the eslint is asking for few permission that I was not able to find what kind of permission is required so i decided to give all permission available , and that worked for me .

sudo chmod -R 777 /yourProjectDirectoryName

Here the project directory is your directory from home to your current folder.

If this didn't work try going through this, https://idqna.madreview.net/

Manishyadav
  • 1,361
  • 1
  • 8
  • 26
0

I have easily solved the issue using "sudo" in the command:

sudo npm start