Search code examples
dockerdockerfiledocker-volumesynology

How to copy files from host to a new docker image using dockerfile, that are shared as a volume on the container?


How can I use a dockerfile to copy files from my host to the image at create time, and make these file available to the host machine when the container is created? I'm lost between 1) VOLUME=[..] in the Dockerfile 2) --volume in the run command 3) docker volume create

I tried creating a dockerfile to copy the files to the image. though if I run the container with the -v option, the target on the container seems to get cleared?? example:

FROM node:latest
WORKDIR /usr/src/myapp
COPY package*.json ./
RUN npm install
COPY index.js /usr/src/myapp/index.js
EXPOSE 8080
CMD [ "node", "/usr/src/myapp/index.js" ]

Running it without volume works great

docker run -p 8080:8080 --name Test test:latest

With volume however, it seems the target dir on the container is cleared (Error: Cannot find module '/usr/src/myapp/index.js')

docker run -p 8080:8080 -v /mynasvolume1/test:/usr/src/myapp --name Test test:latest

I know my alternative is to create a named volume on the host and include that as VOLUME in the Dockerfile. That is not my preference though as it doesn't allow me to select the volume location on my host machine (Synology NAS doesn't give me solutions to change the default location from /volume1/@docker/volumes to a location I can share or mount)


Solution

  • Yes it seems cleared, because mounting /mynasvolume1/test to /usr/src/myapp will show only files from /mynasvolume1/test and override the files you COPYed in you Dockerfile. It's unclear what you're trying to achieve, but try mounting to a specific directory like: /mynasvolume1/test:/usr/src/myapp/test