1

I am learning Docker and I am stuck with this permission issue. I have created a simple React app and created its image. Dockerfile I used Dockerfile: FROM node:alpine

WORKDIR '/app'

COPY package.json .
RUN npm install

COPY . .

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

Now when starting the container I am using volume (-v) so that I can use my file system's file instead of snapshot. It works as expected when I use command -> sudo docker run -p 3000:3000 -v "$(pwd)":/app

But now I want that my container to use node_modules from snapshot only and rest files from my file system. For this I have used command -> sudo docker run -p 3000:3000 -v /app/node_modules -v "$(pwd)":/app

And I get this error -> [eslint] EACCES: permission denied, mkdir '/app/node_modules/.cache'
ERROR in [eslint] EACCES: permission denied, mkdir '/app/node_modules/.cache'

Full Error

vishal@vishal-ThinkPad-E14-Gen-2:~/implementations/Self/docker_compose/083 81-docker-compose-running-tests/frontend2$ sudo docker run -p 3000:3000 -v /app/node_modules -v "$(pwd)":/app 547ddab2b759

> frontend2@0.1.0 start
> react-scripts start

(node:25) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
(Use `node --trace-deprecation ...` to show where the warning was created)
(node:25) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
Starting the development server...

Failed to compile.

[eslint] EACCES: permission denied, mkdir '/app/node_modules/.cache'
ERROR in [eslint] EACCES: permission denied, mkdir '/app/node_modules/.cache'

webpack compiled with 1 error
  • It's a little hard for me to read your PNG file, and `docker build` won't accept it either. Can you [edit] the question to replace the two image links with the actual text of your Dockerfile and error message? – David Maze Sep 28 '22 at 10:10
  • If your Dockerfile contains `WORKDIR /app`, then the bind mount is overwriting literally the entire application with host content, so you're not really running anything in the image at all. Do you have the same permission problems if you run Node directly on the host without involving Docker? – David Maze Sep 28 '22 at 10:12
  • @DavidMaze - I have updated the image as well as added Dockerfile commands and error in question. Also I don't have any permission issue when directly using node. Also, as I already mention in the question it works well when I use 'sudo docker run -p 3000:3000 -v "$(pwd)":/app' command. But I want to make a exception for node_modules folder (that is to not use my system files) and hence have added '-v /app/node_modules' extra when starting container, but getting this error. – Vishal Gupta Sep 28 '22 at 11:52
  • What content goes in that anonymous volume? Why do you need to ask Docker to treat your application's library stack as persistent data? Can you delete the `docker run -v` options entirely, and use the code built into the image? – David Maze Sep 28 '22 at 14:27
  • Yes, it works without -v. But I have already mentioned in the question why I am doing that. Is there any other way to achieve that? – Vishal Gupta Sep 30 '22 at 06:32

0 Answers0