Docker Community Forums

Share and learn in the Docker community.

Why does Docker -v create a directory when a file does not exist?

docker run -d --name Bla -v /home/user/file.conf:/etc/file.conf author/image

If /home/user/file.conf does not exists, a directory “/home/user/file.conf/” will be created. Why?

From within the container I have to store data in (/home/user/file.conf). Does this file on the host really have to be created (touch /home/user/file.conf) before starting the container or is there a workaround?

Hi

So /etc/file.conf dosnt exist either?
But yes, this is normal behaviour.

Can you try and be a bit more specific on what you’re trying to do?

Correct, the file does not exist on both sides.

The user of the container either has the file (then it does exist and there is no problem) or an application inside the container will generate it on first run by user interaction, and for the sake of keeping personal data out of a container I have the mapping.

okay, so if the file is empty it will break your application, or the generation of the file?

but im afraid that this is normal behaviour, im not sure what will happend if you delete the file ( via the container ) at startup if empty, else i would go around it by checking if the file is empty, then override

I am pretty sure it won’t break, as in, the consequences are manageable, however having to do a touch file.conf on the host is an extra step. Unnecessary if Docker would be more logical on that aspect.

If no logical explanation exists, I will propose via https://github.com/moby/moby/issues:
-v /home/user/file.conf:/etc/file.conf --> creates (empty) file
-v /home/user/file.conf:/etc/file.conf/ --> creates (empty) dir