简体   繁体   中英

Docker file mount understanding

I'am using docker and I have a strange behaviour when I try to mount a container file on a host file.

docker run -v /var/tmp/foo.txt:/var/tmp/foo.txt myapp 

The command above runs myapp container which creates a foo.txt file into the /var/tmp directory into the container. Because I need to keep this file on host after myapp dies, I create a mounting point.

My problem is that instead of creating foo.txt as a file on host, I end up with an empty directory named "foo.txt" (and nothing inside).

But, if I create an empty text file foo.txt on host and if I run myapp again, it works as expected.

So, my question is, Do I need to create the file on host before starting the container when I use file mount with docker?

I think I missed something. Thank you for your explanations.

In fact as you discovered, to mount a host file as a data volume the file must exists otherwise docker will create a directory and mount it.

From: https://docs.docker.com/engine/tutorials/dockervolumes/

Mount a host file as a data volume

The -v flag can also be used to mount a single file - instead of just directories - from the host machine.

$ docker run --rm -it -v ~/.bash_history:/root/.bash_history ubuntu /bin/bash

Note it never says that the file doesn't exists on host.

As suggested in the comment it is better to mount a directory if you want the container writes into it.

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM