152

Example below creates dbdata named volume and references it inside db service:

version: '2'
services:
  db:
    image: mysql
    volumes:
      - dbdata:/var/lib/mysql
volumes:
  dbdata:
    driver: local

(from https://stackoverflow.com/a/35675553/4291814)

I can see the path for the volume defaults to:

/var/lib/docker/volumes/<project_name>_dbdata

My question is how to configure the path on host for the dbdata volume?

Ricky Sixx
  • 581
  • 1
  • 10
  • 25
misha
  • 1,521
  • 2
  • 10
  • 5

2 Answers2

149

With the local volume driver comes the ability to use arbitrary mounts; by using a bind mount 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-data'

I 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 does not indicate, that it should not work.

I've also not tested it on Windows...

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Christian Ulbrich
  • 3,212
  • 1
  • 23
  • 17
  • 7
    You can use a bind mount with far simpler syntax than that: volumes: - ./dbdata:/var/lib/mysql. The only 'problem' with a bind mount is that it won't copy the container contents to the host automatically, unlike a named volume. https://docs.docker.com/compose/compose-file/#volumes – mikey Jun 14 '18 at 06:45
  • 41
    OP specifically asked about _named_ volumes, **not** how to use a bind mount in the easiest way. If you want to configure the path for a named volume, you can achieve this with a bind mount, just like shown above. Using named volumes has the advantage that you can simply reuse them across your docker-compose.yml. – Christian Ulbrich Jun 15 '18 at 23:46
  • Sorry Christian, didn't grok that this /was/ using a named volume. I thought this was presenting how to use a bind mount. This does seem like the correct solution (see: stackoverflow.com/a/45039609/3080207). – mikey Jun 16 '18 at 02:33
  • what do you mean by arbitrary mounts? is it a random path over the docker container itself or over the host machine? Thanks. – Amr Afifi Jan 06 '20 at 21:41
  • You can use arbitrary mounts on the host, eg. this way you could also mount something with say sshfs and bind-mount it to a named volume. – Christian Ulbrich Jan 08 '20 at 22:17
  • 2
    @mikey I posted a question requesting clarity about your first comment here: https://stackoverflow.com/questions/65176940/a-bind-mount-wont-copy-the-container-contents-to-the-host-automatically-unlik . Would highly appreciate if you could reply. Thanks. – thanks_in_advance Dec 07 '20 at 07:55
  • Is there a way to set `rw` permissions to `others`? – Marinos An Dec 07 '20 at 16:23
  • 1
    @thanks_in_advance - looks like I've been beaten to it! tyvm for tagging me – mikey Dec 08 '20 at 04:53
  • 1
    @mikey feel free to add to the discussion there via comments. – thanks_in_advance Dec 08 '20 at 08:57
  • @MarinosAn - see the docs here: https://docs.docker.com/compose/compose-file/compose-file-v2/#volumes, you should be able to, although I have never actually tried this. One wonders why you wouldn't just use a typical named volume without specifying the host location, as this is tantamount to r/o. I'm sure there is a use case for what you're asking - just thought I'd mention that in case a standard named volume does the trick. – mikey Dec 10 '20 at 01:51
  • 1
    @mikey I cannot find an example in the above link for setting for `group` or `others`. Can you provide an example? Regarding your question, In my case I want to share containerA's data, both with host and containerB (and, not sure if it matters but, the share point of each container is a volume). – Marinos An Dec 10 '20 at 09:50
  • @MarinosAn sorry - I misinterpreted the question. I've been looking into this and there are different approaches you could take, but none are great imo, and it really depends on your exact use case. I'd recommend raising a question, and be as clear as possible about your desired end state, whether you want host mounted into container with other:rw, or container to host, whether you expect a custom UID/GID or not. – mikey Dec 11 '20 at 00:33
  • @Christian Ulbrich as you have the top voted answer, perhaps you can weigh in on my answer? If you think it's valuable maybe just add it to yours as an option. I can't believe the downvotes it got tbh. – FreeSoftwareServers Apr 22 '21 at 20:15
15

The location of named volumes is managed by docker; if you want to specify the location yourself, you can either "bind mount" a host directory, or use a volume plugin that allows you to specify a path.

You can find some details in another answer I posted recently; https://stackoverflow.com/a/36321403/1811501

Community
  • 1
  • 1
thaJeztah
  • 27,738
  • 9
  • 73
  • 92