When I build docker image, i want to prevent installing node_modules in my local directory

736 Views Asked by At

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?

0

There are 0 best solutions below