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


#1
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

#2

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 * ..


#3

Ralms

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


#4

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?


#5

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.


#6

François-Xavier Michaud

Great, thank you !

You should write :

ps aux | grep -i [d]ocker

to avoid call twice grep


#7

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


#8

tjenarre

Great, thanks!!!


#9

Arnaud Marchand

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


#10

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


#11

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


#12

Martin Goldhahn

Thanks for sharing this one.


#13

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


#14

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.


#15

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.


#16

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