0

As I read in this tutorial https://mherman.org/blog/dockerizing-a-react-app/ it's possible to mount an anonymous volume into a subpath of a mounted volume in order to prevent the changes in that subpath to be reflected back to the actual volume. Is that correct? Because it's not working for me.

I'm trying to dockerize a react app by mounting the src folder into the container and building everything their. But I don't want the node_modules folder that is created during the build step to be reflected back in the mounted src.

My Dockerfile.dev looks like this:

FROM node:15.4.0-alpine3.12

# set working directory
ARG APP_PATH=/app
WORKDIR $APP_PATH

# add `/app/node_modules/.bin` to $PATH
ENV PATH $APP_PATH/node_modules/.bin:$PATH

# install app dependencies
COPY package*.json ./
RUN npm install --silent

# add app
COPY . ./

# start app
EXPOSE 3000
CMD [ "npm", "run", "start" ]

This is my docker-compose.yaml:

version: "3.8"
services:
    frontend:
        build: 
            context: ./frontend
            dockerfile: ../docker/Dockerfile.dev
        image: frontend_react
        ports: 
            - 4000:3000
        volumes:
            - ./frontend:/app
            - /app/node_modules
        environment: 
            - NODE_ENV=development

How can I prevent that the node_modules folder shows up on my local machine? Should I even prevent the node_modules folder from being reflected back to my machine?

winklerrr
  • 13,026
  • 8
  • 71
  • 88
  • You can't: inside the container the `/app/node_modules` must exist as a mount point, and the parent `/app` directory is a bind mount from the host. – David Maze Dec 21 '20 at 15:49
  • @DavidMaze but in my case the `node_modules` folder is not empty? – winklerrr Dec 22 '20 at 09:50
  • Oh, I just figured out that it's a good thing that the `node_modules` folder is reflected back to my mounted volume. Because otherwise `Prettier` and other dev dependencies wouldn't work in my IDE. Cool! – winklerrr Dec 22 '20 at 10:44

0 Answers0