Task: it is necessary when saving a container to save files from an image to a host, provided that the folder to be mounted is empty. For example, the mongodb image (from the documentation) also works.

docker run --name some-mongo -v /my/own/datadir:/data/db -d mongo

I tested this construction (I work from under Windows), it works fine, the database is saved on the host machine. Making attempts on my image to turn a similar scheme, I get an empty folder in the container, the data from the image completely overlap. I did various tests with Dockerfile and VOLUME, the result is always the same. Dockerfile example:

FROM ubuntu
RUN mkdir /myfolder
RUN echo "hello world" > /myfolder/greeting
VOLUME /myfolder

I build an image, I launch the container, waiting to get the contents of the folder /myfolder in D:/Docker/, but there is no file.

docker run -it -v D:/Docker:/myfolder -d app

Folder is empty ...


The RUN command is executed during the image construction, so its output is part of the filesystem that defines the image. Your volume is used when the container is run:

mkdir /tmp/volume
docker run -v /tmp/volume:/volume ubuntu touch /volume/proof.dat

will create a /tmp/volume/proof.dat file on the host system.

So in all likelihood you application is not writing to /data/db. Once the container is started you can use:

docker exec -it {container} bash

to execute command in the container environment and so check what files are there. You can also start you container without -d to see the application log (or use docker logs to obtain the output).

  • It turns out, I need to write a BASH script and put it as ENTRYPOINT. Will the script copy files into the mounted folder after launching the container? – RUS312 Jul 5 at 8:34
  • Yes, this should work. – xenoid Jul 5 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.