Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker volume custom mount point [duplicate]

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?

like image 660
A0__oN Avatar asked Sep 14 '16 17:09

A0__oN


People also ask

Can two Docker containers mount same volume?

For some development applications, the container needs to write into the bind mount so that changes are propagated back to the Docker host. At other times, the container only needs read access to the data. Multiple containers can mount the same volume.

How do I change Docker volume location?

First stop the docker service. Then the volumes can be moved from the default location at /var/lib/docker to the new location. Next the configuration of the docker daemon is edited to point to the new location of the volumes. The next step may not be necessary, but it is a good habit to do it anyway.

What is Docker Mountpoint?

Mountpoint When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine. The file or directory does not need to exist on the Docker host already. It is created on demand if it does not yet exist.

What is the difference between volume and mount in Docker?

The most notable difference between the two options is that --mount is more verbose and explicit, whereas -v is more of a shorthand for --mount . It combines all the options you pass to --mount into one field. On the surface, both commands create a PostgreSQL container and set a volume to persist data.


1 Answers

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   ... 
like image 51
BMitch Avatar answered Sep 20 '22 06:09

BMitch