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

DockerDocker Volume

Docker Problem Overview


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.

Docker Solutions


Solution 1 - Docker

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.

Solution 2 - Docker

2017: with 17.05.0-ce (2017-05-04), the PR 28696 deprecates --graph flag in favor or --data-root: commit 1ecaed0

> The name "graph" is a legacy term from long ago when there used to be a directory at the default location /var/lib/docker/graph. > > However, the flag would indicate the path of the parent directory of the "graph" directory which contains not only image data but also data for volumes, containers, and networks.
In the most recent version of docker, this directory also contains swarm cluster state and node certificates.

With issue 5922 and PR 5978, the documentation has been updated.

Example:

ExecStart=/usr/bin/dockerd -H fd:// --data-root=/mnt/ssd/lib/docker

2016 (now deprecated)

I only know of a docker option to change /var/lib/docker itself, not its subfolders (part of its "graph" used by a docker daemon storage driver)

See docker daemon "Miscellaneous options":

> Docker supports softlinks for the Docker data directory (/var/lib/docker) and for /var/lib/docker/tmp.
The DOCKER_TMPDIR and the data directory can be set like this:

DOCKER_TMPDIR=/mnt/disk2/tmp /usr/local/bin/docker daemon -D -g /var/lib/docker -H unix:// > /var/lib/docker-machine/docker.log 2>&1
# or
export DOCKER_TMPDIR=/mnt/disk2/tmp
/usr/local/bin/docker daemon -D -g /var/lib/docker -H unix:// > /var/lib/docker-machine/docker.log

As mentioned in "Where are docker images stored on the host machine?" (and that would apply also for containers/volumes):

> The contents of the /var/lib/docker directory vary depending on the driver Docker is using for storage.

Solution 3 - Docker

I successfully moved the storage location of docker by moving the content of /var/lib/docker to a new location and then place a symlink pointing to the new location (I took this solution from here https://askubuntu.com/questions/631450/change-data-directory-of-docker):

> Caution - These steps depend on your current /var/lib/docker being an > actual directory (not a symlink to another location). > > 1) Stop docker: service docker stop. Verify no docker process is running: > ps aux | grep -i [d]ocker > > 2) Double check docker really isn't running. Take a look at the current docker directory: > ls /var/lib/docker/ > > 2b) Make a backup - tar -zcC /var/lib docker > > /mnt/pd0/var_lib_docker-backup-$(date +%s).tar.gz > > 3) Move the /var/lib/docker directory to your new partition: > mv /var/lib/docker /mnt/pd0/docker > > 4) Make a symlink: ln -s /mnt/pd0/docker /var/lib/docker > > 5) Take a peek at the directory structure to make sure it looks like > it did before the mv: ls /var/lib/docker/ (note the trailing slash) > > 6) Start docker back up service docker start > > 7) restart your containers (resolve the symlink)

Worked for me on Ubuntu 18.04.1 LTS on an Azure VM with Docker 18.09.2

Solution 4 - Docker

If you're on Fedora (tested on 32) just change or add the --data-root flag with your desired path to the OPTIONS variable on /etc/sysconfig/docker, this is the environment file used by systemd to start the dockerd service.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondukeofgamingView Question on Stackoverflow
Solution 1 - DockerHugo RView Answer on Stackoverflow
Solution 2 - DockerVonCView Answer on Stackoverflow
Solution 3 - DockerCodevView Answer on Stackoverflow
Solution 4 - DockerMarcelo R.View Answer on Stackoverflow