TTT TTT - 9 months ago 164
Linux Question

Single file volume mounted as directory in Docker

Docker documentation says that it's possible to mount a single file into a Docker container:

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:/.bash_history ubuntu /bin/bash

This will drop you into a bash shell in a new container, you will have your bash history from the host and when you exit the container, the host will have the history of the commands typed while in the container.

When I try that however the file mounts as a directory:

tom@u ~/project $ docker run --rm -it -v file.json:/file.json test
total 80K
drwxr-xr-x 9 root root 4.0K Dec 7 12:58 .
drwxr-xr-x 63 root root 4.0K Dec 7 12:58 ..
drwxr-xr-x 2 root root 4.0K Dec 4 16:10 file.json

My Dockerfile looks like this:

FROM ubuntu:14.04
CMD ["ls", "-lah", "/test"]

Docker version is 1.9.1, build a34a1d5.

Is this a documentation issue, a misunderstanding on my side, or is there something else going on?


test is the name of your image that you have built with 'docker build -t test', not a /test folder.

Try a Dockerfile with:

CMD ["ls", "-lah", "/"]
CMD ["cat", "/file.json"]


docker run --rm -it -v $(pwd)/file.json:/file.json test

Note the use of $(pwd) in order to mount a file with its full absolute path (relative paths are not supported)