64

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.

3
  • 8
    I did it using a symbolic link: # cd /var/lib/docker/volumes # ln -s /mnt/myvolume myvolume and then create the volume and start your docker image: # docker volume create --name myvolume # docker run -v myvolume:/myvolumemountpoint ... Jun 17, 2016 at 14:53
  • 1
    using a symlink seems to work fine, but then you cannot delete the volume as docker will not allow deleting a volume if it is not a subdirectory of the data_root. (apparently docker resolves the symlink for volumes.)
    – go2null
    Nov 26, 2019 at 3:50
  • related to the comment above (on inability to remove volumes residing by a symlink): github.com/moby/moby/issues/39446 Apr 21, 2020 at 7:11

4 Answers 4

71

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.

3
  • 5
    The -g flag is deprecated. I suggest using the new --data-root flag. [source: docs.docker.com/engine/deprecated/… Sep 13, 2018 at 17:39
  • 14
    The --data-root parameter moves all persistent data to the specified directory, including container data, image data etc. It would be nice to be able to specify a directory where volumes go, in order to separate user data from docker internal state data.
    – Sebastian
    Dec 19, 2020 at 11:43
  • 1
    I actually also wish to only keep the volumes on my RAID instead of the state data and containers, because I can always pull the images anew - so no redundancy is necessary here, yet it is for volumes... Aww :/
    – Igor
    May 23, 2022 at 19:55
27
+50

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.

10

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

2
  • Yes working as expected. Thank you Jun 15, 2022 at 5:11
  • This method works, however I had some problems with permission. Just have to make sure when you create a copy or move, the user and permissions are preserved. Instead of mv, use cp -Rp.
    – AFwcxx
    Aug 16 at 11:05
1

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.