How to declare a Named Volume in a Dockerfile?

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

This is because the Dockerfile isn't tied to a particular host and can't be sure the host volume path would exist. You need to do that in the run (or equivalent) statement. You can use the api or docker inspect to find out where the volume is located once the container is created if you needed to use that info in a script or similar.

Declaring the volume in the Dockerfile insures that the data will persist and will be available to the host -- even if the location isn't preset.


It's not possible. I think the docs are worded maybe misleadingly.

“The specified name” refers to the path / directory name at which the volume will be created.