Docker: EACCES: permission denied, mkdir '/app/node_modules/.cache'

I encountered a permission error while trying to build a docker container in a React app.
I tried to make use of the community answers, but didn't help.
Following related discussion I tried:

  • I get the current user: id -un
  • tried this: sudo chown -R myUser:myUser /usr/local/lib/node_modules
  • this also threw the same error: sudo chown -R ownerName: /usr/local/lib/node_modules
  • same with this: sudo chown -R $USER /usr/local/lib/node_modules
  • adding a user didn't help: sudo chown -R $USER /app/node_modules
  • tried to give permission installing this: sudo npm install -g --unsafe-perm=true --allow-root
  • another try was to remove node_modules and install specifying sudo: sudo npm install

Adding this to docker-compose file, didn't help either:

environment:
      - CHOKIDAR_USEPOLLING=true

I am trying to build with this command: docker-compose up --build

Dockerfile.dev file

FROM node:alpine

WORKDIR '/app'

COPY package.json .
RUN npm install

COPY . .

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

docker-compose.yml file

version: "3"
services:
  web:
    stdin_open: true
    environment:
      - CHOKIDAR_USEPOLLING=true
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - "4000:4000"
    volumes:
      - /app/node_modules
      - .:/app

error

Building web
Step 1/6 : FROM node:alpine
 ---> 50389f7769d0
Step 2/6 : WORKDIR '/app'
 ---> Using cache
 ---> bb4d4ffdeb02
Step 3/6 : COPY package.json .
 ---> Using cache
 ---> c4e5fce6b1e0
Step 4/6 : RUN npm install
 ---> Using cache
 ---> 9383aea4aba6
Step 5/6 : COPY . .
 ---> Using cache
 ---> 98ea3037694f
Step 6/6 : CMD ["npm", "run", "start"]
 ---> Using cache
 ---> 6edf3365a6db
Successfully built 6edf3365a6db
Successfully tagged app_web:latest
Starting app_web_1 ... 
Starting app_web_1 ... done
Attaching to app_web_1
web_1  | 
web_1  | > [email protected] start
web_1  | > react-scripts start
web_1  | 
web_1  | ℹ 「wds」: Project is running at http://172.20.0.2/
web_1  | ℹ 「wds」: webpack output is served from 
web_1  | ℹ 「wds」: Content not from webpack is served from /app/public
web_1  | ℹ 「wds」: 404s will fallback to /
web_1  | Starting the development server...
web_1  | 
web_1  | Failed to compile.
web_1  | 
web_1  | EACCES: permission denied, mkdir '/app/node_modules/.cache'

Any help will be appreciated


try this Docker - EACCES: permission denied, mkdir '/app/node_modules/.cache'

this worked for me

RUN chown -R node:node /app/node_modules

or sh into docker container and run

chown -R node:node /app/node_modules