wangdev87
wangdev87

Reputation: 8751

Docker-compose: node_modules mouting as volume

I have an app with the following services:

I have a question about frontend

frontend/Dockerfile

FROM node:12.16.3-alpine

RUN mkdir -p /app
WORKDIR /app

COPY package.json yarn.lock /app/

RUN yarn install --pure-lockfile
COPY . /app

CMD ["yarn", "run", "start"]

docker-compose.yml

    frontend:
        ...
        volumes:
            - ./frontend:/app
            - /app/node_modules

The question is /app/node_modules is same meaning with ./frontend/node_modules:/app/node_modules ?

If not, what is the difference?

Upvotes: 2

Views: 940

Answers (1)

cam
cam

Reputation: 5198

/app/node_modules creates a directory inside the container and the Docker Engine automatically creates an anonymous volume for this (i.e. it should will probably be empty). This is from the docs about the compose file spec in the "Short Syntax" section.

./frontend/node_modules:/app/node_modules creates a bind mount. The ./frontend/node_modules directory from your host machine will be shared with the container.

In response to followups regarding why using /app/node_modules works but the other syntax does not:

Your yarn install command creates a node_modules folder inside the Docker image. This created folder conflicts with the existing frontend/node_modules folder you have locally when trying to run with ./frontend/node_modules:/app/node_modules. When you specify /app/node_modules, the container uses the directory created during the build step.

Upvotes: 2

Related Questions