12

I just started working with docker today and am blocked on a permissions issue. I don't know what I should be entering to switch the permission. I am assuming it's a chown thing. There are other questions on stack overflow but they did not help me as they were either not Docker specific or did not have a selected answer to the question.

Below is the error

client_1  | 
client_1  | > client@0.1.0 start
client_1  | > react-scripts start
client_1  | 
client_1  | ℹ 「wds」: Project is running at http://172.19.0.2/
client_1  | ℹ 「wds」: webpack output is served from 
client_1  | ℹ 「wds」: Content not from webpack is served from /app/public
client_1  | ℹ 「wds」: 404s will fallback to /
client_1  | Starting the development server...
client_1  | 
client_1  | Failed to compile.
client_1  | 
client_1  | EACCES: permission denied, mkdir '/app/node_modules/.cache'

My docker-compose.yml looks like this:

version: "3"
services:
  client:
    stdin_open: true
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - "3000:3000"
    volumes:
      - "/app/node_modules"
      - "./:/app"

My Dockerfile.dev looks like this:

FROM node:alpine
RUN apk update && apk add git && apk add python make g++

WORKDIR /app

COPY package.json /app

# To Fix Permissions for Packages
RUN npm config set unsafe-perm true

RUN npm install --force

COPY . /app

CMD ["npm", "run", "start"]
Michael Paccione
  • 2,467
  • 6
  • 39
  • 74

5 Answers5

7

I have once had a similar problem. My interpretation was that when the app is run, it is not run as root, so when it tries to create the .cache directory, it does not have the right credentials to do so anymore. I was using a very similar image (node:lts-alpine), and it already came with a node user. What I did, but I am not sure if that is in accordance with the best practices, was creating a file named .cache where it was supposed to be found, changing its ownership so that node could access it and ran the app as node. I actually run the application as node because this I was deploying to heroku, and there the containers are not run as root. In your case, it would look like:

FROM node:alpine
RUN apk update && apk add git && apk add python make g++

WORKDIR /app

COPY package.json /app

# To Fix Permissions for Packages
RUN npm config set unsafe-perm true

RUN npm install --force

COPY . /app

RUN chown -R node /app/node_modules

USER node

CMD ["npm", "run", "start"]
  • Thanks for the attempt here Andre but I'm getting the same error :( I did this cmd... sudo docker-compose build --no-cache followed by sudo docker-compose up – Michael Paccione Apr 21 '21 at 00:54
  • try `RUN chown -R node /app/node_modules` instead. It might take a bit longer, but not that much. Then you can take out the line `RUN mkdir -p /app/node_modules/.cache`. – André Guimarães Aragon Apr 21 '21 at 01:02
  • 1
    Try the adding the following: `RUN chown -R node:node /app/node_modules` instead of `RUN chown -R node /app/node_modules` and `RUN chmod -R 744 /app/node_modules` If this does not work, the your container is probably not being run as user `node`. In this case, you could try `RUN chmod -R 777 /app/node_modules` just for your container to start, than go try to figure out with user is beinf used to run the container, and then give permissions to write an execute to this user. Let me know if this works!!! – André Guimarães Aragon Jun 02 '21 at 13:17
  • 3
    I realized this affected ubuntu users who had not set up non-sudo docker. If I `RUN mkdir -p /app/node_modules` and then `RUN chown node:node /app/node_modules` before `npm install` then it worked equally well but much faster. – thadk Nov 04 '21 at 05:12
  • yes thadk, this is MUCH faster this way – NickC Nov 17 '22 at 19:44
4

I got the same error and putting the docker file as below solved it.

RUN npm config set unsafe-perm true

RUN npm install --force

COPY ./ /usr/app/

RUN chown -R node /usr/app/node_modules

USER node
Lin Du
  • 88,126
  • 95
  • 281
  • 483
0

Adding CHOKDIR_USERPOOLING=true to docker-compose.yaml worked for me.

environment:
    - CHOKDIR_USERPOOLING=true
James Risner
  • 5,451
  • 11
  • 25
  • 47
0

Just in case somebody else lands here searching the same error, but with similar Dockerfile.

Check that you if Dockerfile installs node_modules in the app directory and not in parent directory:

RUN npm install --production --silent
# RUN npm install --production --silent && mv node_modules ../

This is common with VScode docker extension as illustrated in this issue:

https://github.com/microsoft/vscode-docker/issues/2071

There are advantages to install in parent, but perhaps the permissions in parent are not setup correctly.

Wtower
  • 18,848
  • 11
  • 103
  • 80
0

Had same issue, I was using docker desktop in WSL2.

I tried the npm unsafe perm option, didn't work.

Then I deleted the node_modules folder from windows and then did npm i from inside of container. Didn't even had to do npm force install.

That worked

vishwakarma09
  • 254
  • 3
  • 17