I have a very simple node js app and the project structure looks like this.
FROM node:12.18.2-alpine WORKDIR /test-app COPY package.json package-lock.json ./ RUN npm i COPY . ./ EXPOSE 3000 ENTRYPOINT [ "node", "index.js" ]
version: '3.2' services: test-app: build: . ports: - "3000:3000" volumes: - .:/test-app - "test_app_node_modules:/test-app/node_modules" volumes: test_app_node_modules: driver: local
If you look at the
volumes section in
docker-compose.yml file, first I’m bind mounting my current directory on the host machine to the
test-app directory on the container. This means :
- whatever files or directories that were inside my current dir will get reflected on the container dir and any changes made to the container dir will also get reflected back to the host dir.
- this means
node_modulesthat were installed in the
test-appdir of the container, during docker build, were overwritten as well.
and the next step in the
volumes section is named volumes. This means:
- when the volume doesn’t exist, running for the first time, It should copy everything from
test-app/node_modulesinside the container to
test_app_node_modulesvolume. But the
test-app/node_modulesis empty because step 1 overwrote it.
- which means we created an empty volume and mounted it to the container.
If this is so, it should be causing missing dependency error but my app is running properly. I’m not sure where I’m getting node_modules from.
Also, I see an empty node_modules folder in the host directory. I assume the reason behind this is
"test_app_node_modules:/test-app/node_modules" looks for the
node_modules in the container but it doesn’t exist so it creates one and as a result, it gets reflected back to the host dir.
I’m not able to grasp the idea of volume mounting. What is the flow here? How node_modules are begin stored into the volumes when there are none?