docker volume custom mount point [duplicate]

2020-05-14 22:39发布

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?

1条回答
地球回转人心会变
2楼-- · 2020-05-14 23:01

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
  ...
  volumes:
    bind-test:
      driver: local
      driver_opts:
        type: none
        o: bind
        device: /home/user/test
  ...
查看更多
登录 后发表回答