58

I'm new to Docker and I was playing around with docker volume. I wanted to specify the location where docker volume stores the data. Much like when we provide the -v option when we execute docker run. Ex : -v /somefolder/:/var/somefolder

How can we set a custom Mountpoint when we create a docker volume. I didn't find any options on docs.

When I inspect the volume

[                                                                                        
    {                                                                                    
        "Name": "sampleproject_mysql_data",                                              
        "Driver": "local",                                                               
        "Mountpoint": "/mnt/sda1/var/lib/docker/volumes/sampleproject_mysql_data/_data", 
        "Labels": null,                                                                  
        "Scope": "local"                                                                 
    }                                                                                    
]   

I get something like above.

Is there a way we can set custom Mountpoint. Through docker volume command or through docker-compose.yml?

A0__oN
  • 8,740
  • 6
  • 40
  • 61

1 Answers1

98

If you need a named volume that's pointing to a host filesystem location (which is a bit of reinventing the wheel since you can do a host mount, but there appear to be a lot of people asking for it), there's the local persist filesystem driver. This is included in Docker's list of plugins.


Update: It's also possible to do a bind mount with a named volume to any directory on the host using the default local volume driver. This allows you to take advantage of automatic initialization of named volumes, which is missing from host volumes, but has the one downside that docker doesn't create the host directory if it is missing (instead the volume mount would fail). Here are a few ways you can create this named volume:

  # create the volume in advance
  $ docker volume create --driver local \
      --opt type=none \
      --opt device=/home/user/test \
      --opt o=bind \
      test_vol

  # create on the fly with --mount
  $ docker run -it --rm \
    --mount type=volume,dst=/container/path,volume-driver=local,volume-opt=type=none,volume-opt=o=bind,volume-opt=device=/home/user/test \
    foo

  # inside a docker-compose file
  ...
  test:
    ...
    volumes:
      - bind-test:/var/lib/grafana

  volumes:
    bind-test:
      driver: local
      driver_opts:
        type: none
        o: bind
        device: /home/user/test
  ...
Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 1
    This is exactly what I was looking for.. Any way we can enable this on Windows. Description there are for linux and doesn't work for Windows. – A0__oN Sep 14 '16 at 18:07
  • Are you on a 2016 beta trial? Otherwise, you're running Linux in a VM. – BMitch Sep 14 '16 at 18:20
  • is --opt device the path to the folder on the host? – Zuabi Oct 17 '18 at 08:08
  • @Zuabi yes, it's the path on the remote nfs server. – BMitch Oct 17 '18 at 09:53
  • 7
    How does the bind mount work? After creating the volumes (in a docker-compose scenario), I still see them in /var/lib/docker/volumes, though inside them now there is an opts.json file with the options I specify in the compose file. It seems the files in the _data folder in the volumes mirrors the ones I specified in the options. Is `/home/user/test` mounted in `bind-test/_data`? – GPhilo Aug 11 '21 at 10:11
  • 1
    It will be worth to mention how to mount this volume at compose file – Eugen Konkov Feb 19 '22 at 20:34
  • @EugenKonkov the answer includes the compose file lines. – BMitch Feb 19 '22 at 21:22
  • @BMitch: I mean that (see edited answer) – Eugen Konkov Feb 23 '22 at 11:30