0

I'm studying using dockers in create-react-app. I don't want node_modules to be installed in my local directory because I'm running npm install inside the docker container, so I only need package.json in my directory, right?

But every time I build an image, node_modules is automatically installed in the local directory. I think the build takes a long time to COPY this folder.

How can I prevent this? I created the React app with CRA, I didn't touch any code, I deleted the node_modules folder myself. And I've only created 'Dockerfile.dev' and 'docker-compose.yml'.

FROM node:alpine

WORKDIR /usr/src/app

COPY package.json ./

RUN npm install

COPY ./ ./

ENV CHOKIDAR_USEPOLLING=true

CMD ["npm","run","start"]
version: "3"
services:
    react:
        build:
            context: .
            dockerfile: Dockerfile.dev
        ports:
            - "3333:3000"
        volumes:
            - /usr/src/app/node_modules
            - ./:/usr/src/app
        stdin_open: true


If I do 'docker-compose up' like this, it automatically creates 'node_modules' in the local directory. How can I prevent this?

  • 1
    If you're doing initial development of the application – you've just run create-react-app and nothing else – why bring a complex deployment system into it now? Can you use an ordinary host-based Node environment, and not Docker? – David Maze Aug 08 '21 at 10:18
  • You are bind mounting the current compose dir to `/usr/src/app` inside the container and then a subpath `/usr/src/app/node_modules` to a docker volume. Docker needs that mount point and creates it. if you have a look from your local machine, that dir stays empty. It only contains the files created in the volume when you look at it from the container. – Zeitounator Aug 08 '21 at 10:20
  • I've linked this question to two other questions. The first is about how to keep Docker from sending itself the host's `node_modules` directory, which will speed up the image build; the second is about why that empty directory gets created. – David Maze Aug 08 '21 at 10:23

0 Answers0