How to move docker's default /var/lib/docker to another directory on Ubuntu/Debian Linux - LinuxConfig.org

The following config will guide you through a process of changing the docker's default /var/lib/docker storage disk space to another directory. There are various reasons why you may want to change docker's default directory from which the most obvious could be that ran out of disk space. The following guide should work for both Ubuntu and Debian Linux or any other systemd system. Make sure to follow this guide in the exact order of execution.
This is a companion discussion topic for the original entry at https://linuxconfig.org/how-to-move-docker-s-default-var-lib-docker-to-another-directory-on-ubuntu-debian-linux

mihai

If you start docker and the images are gone and docker tries to fetch others, it’s because of rsync -aqxP /var/lib/docker/ /new/path/docker, normally it creates another docker container in the destination. The command should probably be rsync -aqxP /var/lib/docker/* /new/path/docker. Anyway, I fixed it without re-copying it using : cd /new/path/docker/docker;rm -rf ../{builder,containerd,containers,image,network,overlay2,plugins,runtimes,swarm,tmp,trust,volumes}; mv * ..

Ralms

Didn’t work for me on OpenMediaVault3 running Debian 8
Docker seems to be ignoring all the changes I do :S

Soma Consulting

After I restart server, I don’t see any containers.
so I tried to run rsync -aqxP /var/lib/docker/ /new/path/docker
Then I got the container with previous data and lost all the new update data.
What should I do to get the update data?

Tommy Powell

use daemon.json instead as suggested by kn. I followed the OP directions and the docker.service file got reset after and update.

François-Xavier Michaud

Great, thank you !

You should write :

ps aux | grep -i [d]ocker

to avoid call twice grep

regis25

Altering system scripts is a bad practice when:

  • they’re under a package manager control (s.a. apt): problems on package upgrade
  • the same could be achieved in a less intrusive way, i.e. through a configuration file

In this case, the recommended solution is to create a custom override as per documentation:

https://docs.docker. com/v1.11/engine/reference/commandline/daemon/#daemon-configuration-file

tjenarre

Great, thanks!!!

Arnaud Marchand

Works perfectly. Docker version 17.03.0-ce, build 60ccb22
On ubuntu 16
Thanks for sharing.

kn

Instead of modifying the systemd startup script, one can modify the “-g / --graph” option by adding a /etc/docker/daemon.json with the contents of:

{
“graph”: “/new/path/docker”
}

Other options can also be inserted into that file, e.g. TLS usage

1 Like

trevi79

Thanks, this worked for me on:

Kernel Version: 4.4.0-62-generic
Operating System: Ubuntu 16.04.1 LTS
OSType: linux
Architecture: x86_64

I had some issues setting the docker root to a non-static mount drive. I used this to set a static mount which solved my problem: http://askubuntu .com/questions/299131/hard-drive-location-keeps-changing-after-upgrade-to-13-04

Martin Goldhahn

Thanks for sharing this one.

I would suggest using this rsync command:

 rsync -aqxHPX --delete /var/lib/docker/ /new/path/docker

-H to ensure hard links are not expanded to duplicate files.
-X for anyone using SELinux to preserve labels

1 Like

Be careful if you’re using devicemapper as a storage! rsync does not account sparse files with these keys, so after such dumb copying onto another partition you will get suddenly large files for data/metadata on the target partition, if they will even fit it! Moreover, for me even the -S, or --sparse keys, which are dedicated for this purpose, didn’t work. So I copied those files with cp: cp --sparse=always /var/lib/docker/devicemapper/devicemapper/data targetdir/data, and the same for metadata; then deleted original files and replaced them with symbolic links to the new destination. Now it works great, and no need to move the whole directory, as the remaining files are tiny.

1 Like

That article isn’t good at all. First: the command to start docker daemon is NOT “docker” but “dockerd”. Then, NEVER CHANGE “/lib” directory files, because systemd is able to use overrides, you MUST change files in “/etc” directory. And, then, instead of searching which file to change, you can use sudo systemctl edit --full docker that will make the file copy if needed in /etc/systemd/system and open editor that let you change configuration.

Don’t forget to type sudo systemctl daemon-reload after having changed something and before to restart service.

2 Likes

See my answer, it’s probably because your distribution uses the legacy systemd directory. Always use “systemctl edit --full docker” instead of editing /lib directory. And don’t forget to make “systemctl daemon-reload” afterward

Works like a charm in Kubuntu 18.04

Linux juky-ThinkPad-E470 5.0.0-32-generic #34~18.04.2-Ubuntu SMP Thu Oct 10 10:36:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

The -g / --graph option is deprecated in newer versions of docker. Use ’ --data-root /new/path/docker’ instead.

lungaro@pisense:/var/lib $ /usr/bin/dockerd -g /mnt/omv/docker/var/lib/docker -H fd:// --containerd=/run/containerd/containerd.sock
Flag --graph has been deprecated, Use --data-root instead
the “graph” config file option is deprecated; use “data-root” instead

2 Likes

Thank you for this article works perfectly. I also had to replace -g with --data-root. Today I had to do it again since the docker service file had been overwritten by a ubuntu update or upgrade.

2 Likes

The -g flag has been deprecated now. Use --data-root instead. It’ll work like a charm.

1 Like