Search code examples
dockerdockerfiledocker-machine

Dockerfile VOLUME does not mount host


So I have a dockerfile with a VOLUME line, where I want to mount a directory from my host to my ubuntu container. The container builds fine, but then when it's run, the directory I want to mount is empty.

I'm trying to mount this directly in the dockerfile, so I don't want to use docker run -v

How would I properly do this?


Solution

  • As specified in the docs for the Dockerfile VOLUME instruction:

    The host directory is declared at container run-time: The host directory (the mountpoint) is, by its nature, host-dependent. This is to preserve image portability, since a given host directory can’t be guaranteed to be available on all hosts. For this reason, you can’t mount a host directory from within the Dockerfile. The VOLUME instruction does not support specifying a host-dir parameter. You must specify the mountpoint when you create or run the container.

    So no, you are not able to specify a host directory in the Dockerfile. You will need to use a bind mount and either the --mount or --volume flag as shown in the bind mount documentation.

    If you're trying to avoid copying extra data in the build context, consider using a .dockerignore file.