6

I was trying to run a node project container using docker volumes with -

docker run -p 3000:3000 -v /myapp/node_modules -v $(pwd):/myapp batzu/frontend 

and got an error -

EACCES: permission denied, mkdir '/myapp/node_modules/.cache'

But when I try to run the same container without the -v flags -

docker run -p 3000:3000 batzu/frontend

The container starts just fine without error.

First docker run logs-

> frontend@0.1.0 start
> react-scripts start

ℹ 「wds」: Project is running at http://172.17.0.2/
ℹ 「wds」: webpack output is served from 
ℹ 「wds」: Content not from webpack is served from /myapp/public
ℹ 「wds」: 404s will fallback to /
Starting the development server...

Failed to compile.

EACCES: permission denied, mkdir '/myapp/node_modules/.cache'

Build command used (the app is an example react-app)-

docker build -f Dockerfile.dev -t batzu/frontend .

My Dockerfile.dev -

FROM node:alpine

WORKDIR /myapp

COPY package.json .
RUN npm install
COPY . .

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

package.json file generated by the react project-

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.9",
    "@testing-library/react": "^11.2.5",
    "@testing-library/user-event": "^12.7.1",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.2",
    "web-vitals": "^1.1.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

I tried to sh into the container and execute npm run start from it but got the same error even though I am able to execute mkdir '/myapp/node_modules/.cache' from the shell without any permission error. I am not able to understand what does the -v /myapp/node_modules in the docker run exactly does as I was told it is supposed to sort of bookmark the node_modules folder before we map our working directory to the /myapp/ dir of the container. Why does this cause an error??

Can someone point me the right direction or explain what am I doing wrong or a fix....?

Many thanks in advance :)

batzu
  • 111
  • 1
  • 1
  • 6
  • Does this answer your question? [Permission denied on accessing host directory in Docker](https://stackoverflow.com/questions/24288616/permission-denied-on-accessing-host-directory-in-docker) – Max Feb 22 '21 at 20:38
  • The thing is I can run any command in the node_modules folder without any problem, its just npm run start that throws an error.. – batzu Feb 23 '21 at 05:52
  • what `npm run start` do ? Update the question with the `package.json` scripts section – Max Feb 23 '21 at 21:56
  • I've updated the question with the ```package.json``` file included. – batzu Feb 24 '21 at 12:57
  • Try to don't use the `${cwd}` value, instead use a regular folder ie. `/foo/bar`. – Max Feb 24 '21 at 20:43
  • @Max thanks for looking into this but I tried this too, entering current directory manually in place of ```$(pwd)``` but that also did not work, got the same error :( – batzu Feb 26 '21 at 10:00
  • Have you look at this [stackoverflow quetion](https://stackoverflow.com/questions/55926705/docker-error-eacces-permission-denied-mkdir-project-node-modules-cache)? – Max Feb 26 '21 at 16:08

3 Answers3

6

Your Dockerfile.dev should be as you see below, note that COPY will be executed on behalf of a node user and WORKDIR should be /home/node (for more information take a look at https://github.com/nodejs/docker-node/issues/740#issuecomment-458545074):

FROM node:alpine

WORKDIR /home/node/app
RUN chown -R node:node /home/node/app

COPY --chown=node:node package.json .

RUN npm install

COPY --chown=node:node . .

# At the end, set the user to use when running this image
USER node

CMD npm run start

Note it'll work both with docker and docker-compose CLI if you ever introduce docker-compose.yml file with test coverage service inside.

Ivan Sidaruk
  • 435
  • 3
  • 13
5

So, what I gathered from looking around and seeing other people's Dockerfiles and a bit of running commands in a bash terminal in my container, is that the node:alpine image I used as base, creates a user named node inside the container.

The npm run start is executed as a process of node user(process with the UID of node) which does not have root privileges (which I found out while looking in the htop), so in order to make node user the owner of /app directory I added this in the docker file-

RUN chown -R node.node /app

The updated Dockerfile.dev is-

FROM node:alpine

WORKDIR '/app'

COPY package.json .
RUN npm install
RUN chown -R node.node /app

COPY . .

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

This has fixed the problem for me, hope it can help someone else too. :)

batzu
  • 111
  • 1
  • 1
  • 6
0

In Dockerfile.dev Add one Line

RUN chmod 777 node_modules

Now Dockerfile.dev like this:-

FROM node:alpine
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
RUN chmod 777 node_modules
CMD ["npm","run","start"]