Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the default location for "docker create volume" command?

When creating volumes through the volume API, that is, as the container volume pattern is now not necessarily the best practice anymore:

# docker volume inspect test-data [     {         "Name": "test-data",         "Driver": "local",         "Mountpoint": "/var/lib/docker/volumes/test-data/_data"     } ] 

I would like to, for example, have docker volumes exist in /data (which is mounted in a different physical volume).

This is not possible to do with symbolic links, it is possible to do with bind mounts, but would I'm wondering if there is some configuration in Docker to change the default location for each separate volume.

like image 445
dukeofgaming Avatar asked Mar 15 '16 14:03

dukeofgaming


People also ask

How do I change my default docker volume location?

Here's how. 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.

Where do docker volumes get created?

Docker automatically creates a directory for the volume on the host under the /var/lib/docker/volume/ path. You can now mount this volume on a container, ensuring data persistence and data sharing among multiple containers.


1 Answers

You can change where Docker stores its files including volumes by changing one of its startup parameters called --data-root.

If you're using systemd for service management, the file is usually located at /lib/systemd/system/docker.service. Edit the file as such:

# Old - taken from the generated docker.service file in Ubuntu 16.04's docker.io package ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_OPTS  # New ExecStart=/usr/bin/dockerd --data-root /new_location/ -H fd:// $DOCKER_OPTS 

Alternatively, you can edit the Docker daemon configuration file which defaults to /etc/docker/daemon.json.

Restart the Docker daemon and your volumes will be under /new_location/volumes/{volume_name}/_data

Note: be careful in production and also locally! You also have to move the existing data from /var/lib/docker/ to the new location for your docker install to work as expected.

You can use symlinks from the new location if you want specific folders to be in specific place.

like image 66
Hugo R Avatar answered Sep 19 '22 09:09

Hugo R