Docker Community Forums

Share and learn in the Docker community.

Dockerfile and volumes

docker

(Magick) #1

Hello

Im trying to understand how to work with volumes using dockerfiles.

On https://docs.docker.com/engine/tutorials/dockervolumes/ it says:

Note: You can also use the VOLUME instruction in a Dockerfile to add one or more new volumes to any container created from that image.

And thats it. No example.

https://docs.docker.com/engine/reference/builder/#/volume says:

The VOLUME instruction creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers. The value can be a JSON array, VOLUME ["/var/log/"], or a plain string with multiple arguments, such as VOLUME /var/log or VOLUME /var/log /var/db. For more information/examples and mounting instructions via the Docker client, refer to Share Directories via Volumes documentation.

So, no examples of how to map a host directory to a directory in the container using dockerfile.
The referenced link to Share Directories via Volumes does not contain any examples on how to use volumes with Dockerfiles.

How do I map a local directory to a directory in the container in a dockerfile?


(David Maze) #2

Right, you can’t do that. The VOLUME instruction instructs Docker to create persistent storage for the specified directory, which will be in the Docker-private volume space (docker volume ls will show it after you’ve started a container from the image). It’s just like the docker run -v option with the variant that only specifies a directory inside the container.

As a general rule, a Dockerfile can’t specify any details of how the host system runs it. In addition to not being able to forcibly access specific host directories, you can’t force an image on to the host’s network, or forcibly claim specific capabilities, or have the image always claim privileged mode. If you need to consistently run an image with a specific set of options a Docker Compose YAML file is probably the easiest way to do it.