Search code examples
dockerdockerfilecontainersvolume

docker volumes in dockerfiles


I want to run my cloud in a docker container and store the data outside the container in a volume. So that when my container is destroyed or removed, the data is still persistent.

When passing the volume parameter at docker container run -v /path/to/src:/path/to/target imageName:tag it runs fine without any issues.

How can I pass the src and target destinations into the dockerfile?

VOLUME /path/to/target will create a volume which has its source somewhere in var/lib/docker which I do not want.


Solution

  • Short answer: You can't.

    Long Answer: what you are trying to do here is essentially against the portability of images. The Docker documentation specifically states this is not possible, as seen here: https://docs.docker.com/engine/reference/builder/

    "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."