reactjsdockerpermissionsdockerfilenpm-start

EACCES: permission denied, open "file-path"


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

Solution

  • 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