Make Dockerfile VOLUME behave like docker-compose volumes
I am trying to setup a
Dockerfile to mount my host directory to my specified docker container directory (so code changes will be reflected immediately). When I run
docker run -p 3000:3000 -d -v ~/Projects/test:/app test the files are mounted and everything works fine. However, once I close the container, the file changes don’t stay within the container like the changes would if I were running
volumes from a
Here is my Dockerfile:
FROM node:onbuild EXPOSE 3000 RUN mkdir /app VOLUME ./:/app/ WORKDIR /app/ RUN npm install -g nodemon CMD nodemon server.js
Here is a working docker-compose.yml:
test: build: ./ volumes: - ./:/app/ working_dir: /app/ command: npm start ports: - "3000:3000"
One Solution collect form web for “Make Dockerfile VOLUME behave like docker-compose volumes”
VOLUME command in Dockerfile has a slightly different meaning:
- you cannot define mounted volumes within a Dockerfile
- it’s only used for specifying inter-container volumes
So in your Dockerfile,
VOLUME ./:/app/ must be changed to
VOLUME /app/. Your
docker-compose definition will tell docker to mount local dir
/app in the container.
-v option and the
volumes tag in
docker-compose work the same way. The host directory is mounted to a container directory. Any file which was present in the image in the container directory will be replaced by the files from the host directory.