Docker: npm run start causing error, EACCES: permission denied, open '/home/node/node_modules/.cache/.eslintcache

Viewed 6161

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
6 Answers

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

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 

Create a .eslintignore file and put * in it.

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

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.

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/

Related