How to declare a named volume in a Docker file?

If I use VOLUME in the VOLUME file, it creates an anonymous volume. Is there a way to create a named volume from a docker file?

I am looking for the equivalent of Dockerfile

 docker run -v my-named-volume:/mnt/something repo/my-img 

All I managed to get through the Dockerfile is equivalent

 docker run -v /mnt/something repo/my-img 

I would think that this is simply not supported; however the document says so

The VOLUME instruction creates a mount point with the specified name and notes that it contains external volumes from its own host or other containers.

There seems to be a way to name volumes, but it doesn't say how

+6
source share
2 answers

It's impossible. I think that the documents formulated, perhaps, are misleading.

+2
source

A bit unclear. It creates a mount point using this name, but the actual file path does not use this name. If you run docker inspect {container-name} , you will see a type name: "Destination": "/mnt/something", and the actual location, for example: "Source": "/var/lib/docker/volumes/cb80c7802244dd3669eed8afb7d94b61366844d80677eb180fa12002db04ea7c/_data", .

This is because the Docker file is not bound to a specific host and cannot be sure that the host volume path will exist. You need to do this in the run statement (or equivalent). You can use api or docker inspect to find out where the volumes are after creating the container if you need to use this information in a script or similar.

Declaring a volume in a Dockerfile ensures that data is saved and available to the host โ€” even if no location is specified.

+3
source

All Articles