How to set a path on host for a named volume in docker | docker compose volume path
Withthelocalvolumedrivercomestheabilitytousearbitrarymounts;byusingabindmount[1]youcanachieveexactlythis.Forsettingupanamedvolumethatgetsmountedinto/srv/db-data,yourdocker-compose.ymlwouldlooklikethis:version:2services:db:image:mysqlvolumes:-dbdata:/var/lib/mysqlvolumes:dbdata:driver:localdriver_opts:type:noneo:binddevice:/srv/db-dataIhavenottesteditwiththeversion2ofthecomposefileformat,buthttps://docs.docker.com/compose/compose-file/compose-versioning/#version-2[2]doesnotindicate,thatitshou...
With the local volume driver comes the ability to use arbitrary mounts; by using a bind mount[1] you can achieve exactly this.
For setting up a named volume that gets mounted into /srv/db-data, your docker-compose.yml would look like this:
version: 2 services: db: image: mysql volumes: - dbdata:/var/lib/mysql volumes: dbdata: driver: local driver_opts: type: none o: bind device: /srv/db-dataI have not tested it with the version 2 of the compose file format, but https://docs.docker.com/compose/compose-file/compose-versioning/#version-2[2] does not indicate, that it should not work.
Ive also not tested it on Windows...
References bind mount (docs.docker.com) https://docs.docker.com/compose/compose-file/compose-versioning/#version-2 (docs.docker.com)