Mounting host file to docker container creates directory in the container instead of file

Expected behaviour

On Docker for Mac when i do
~/tmp> echo “hello world” > test.txt
docker run -v ~/tmp/test.txt:/test/test.txt alpine cat /test/test.txt
I’ve got:
hello world
as result

Actual behaviour

On Docker for Windows when i do:
echo “Hello world” > test.txt

docker run -v /C/Users/survdemo/test.txt:/test/test.txt alpine cat /test/test.txt

I’ve got:
cat: read error: Is a directory
so /test/test.txt is a directory.

Information

Client:
Version: 1.12.0
API version: 1.24
Go version: go1.6.3
Git commit: 8eab29e
Built: Thu Jul 28 21:15:28 2016
OS/Arch: windows/amd64

Server:
Version: 1.12.0
API version: 1.24
Go version: go1.6.3
Git commit: 8eab29e
Built: Thu Jul 28 21:15:28 2016
OS/Arch: linux/amd64

Do anybody can help me with this different behaviour Mac and Windows?

Thank you in advance, Vladimir

I’m pretty sure, you should mount directories and not files. see: https://docs.docker.com/engine/reference/commandline/run/#/mount-volume-v-read-only

You do not have to mount a directory. I mount files all the time and it works just fine. The problem is that you are using a relative path. Try using a full path instead.

It’s a quite old issue, but one of my colleagues faced the same problem today.
On Windows, antivirus and similar softwares might prevent docker to access the file system, leading docker to the creation of an empty folder rather then mounting the specified file.

Hopefully this solves for you as well.
The latest update caused this for me.
I ended up going semi-nuclear, deleting all my images and containers and then docker-compose up.

Stop all containers
docker stop $(docker ps -a -q)
Delete all containers
docker rm $(docker ps -a -q)
Delete all images
docker rmi $(docker images -q)

Hope that helps

I had this same issue - previously I could mount files, then it started mounting all files and folders. Turned out I had to go to settings > Shared Drives, Reset credentials, and then share the volumes again.

1 Like

I experienced that when I mounted a folder, only one of the files got mounted, and it got mounted as an empty folder. I solved it like the user below:

Afterwards when I mounted a folder, the contents were properly mounted as files.

just solved using a relative path. thank you