Skip to content
Alexander Holbreich
Go back

Moving docker images location to different partition

By default docker will put all the data including images under /var/lib/docker (At least on Debian Linux). This could lead to problems with space your machine. Its was case now on my home server, so I had to move the docker location. I had to mount /var/lib/docker to new place. Because my /var and /usr are mounted to different partitions and discs, I try to solve the space problem by mounting docker default location to the new mount under /usr/local/docker transparently.

At first, do backup of the /etc/fstab

sudo cp /etc/fstab /etc/fstab.$(date +%Y-%m-%d)

Then stop docker daemon and copy all files per rsync preserving all file attributes.

sudo service docker stop
sudo mkdir /usr/local/docker
sudo rsync -aXS /var/lib/docker/. /usr/local/docker/ 

Now it was important to check that everything was copied right. I’ve done an eye check, but diff -r command is useful too. Ok, it’s important to make a new mount and make it durable in fstab. That is what was useful in my case inside of fstab.

# <file> <system> <mount point>   <type>  <options>     <dump>  <pass>
# ...
/usr/local/docker /var/lib/docker none bind 0 0

Now mount the new configuration without a reboot.

mount -a

And your docker has enough space again!


Archived comments (12)

These comments were migrated from Disqus and are no longer accepting replies.

  • Steven Merrill

    Instead of a bind mount, you could start the docker daemon with the '--graph="/usr/local/docker"' option.

  • AlexH

    Thank you to pointing me to that Steven. Indeed it would have same effect very quick! Mounting solution doesn't change the default dir. You will find docker stuff where you excpect to. But this is of course a question of taste.

  • portenez

    Works really well! Thank you!

  • AlexH

    You're welcome...

    With Docker 1.7 which shortly released they have rewritten a lot on storage system. Drivers are plugable and support of ZFS is included. Now things happen very fast...

  • Failed at Human

    This is not a good solution as the space saving layering is not preserved. You may well find that after this operation, your destination folder occupies several times as much as the source. The only elegant solution that I've been able to find is to docker export and import each image, or to actually image the disk and modify the partition.

  • AlexH
    You may well find that after this operation, your destination folder occupies several times as much as the source.

    Can't remember it was the case... And why should layering be affected?

  • Failed at Human

    I've got 400GB of images that takes up 30GB of space, without any special filesystem features. It's been surprisingly difficult to move without the copy taking up 400GB or the export process requiring a tmp directory with as much space. I'll blog about it soon... as it seems nobody else has! The layering seems to be preserved with just a neat copy, but docker does a bit more than just layering.

  • AlexH

    Which version of docker, kernel and what file-system do you use?

  • Failed at Human

    I've been working with 1.6.1 and 1.7.1 and ext4 with overlayfs on one, zfs on another, and btrfs on another. Haven't and enough time to go into the details yet.

  • AlexH

    zfs and btrfs are tricky... I rememeber people reportet some issues with them and docker in early versions... Maybe things changed. But i can't help here :(

  • Dan Beaulieu

    I think I am hitting this as well with docker 1.10.2 and overlay. mv, cp, rsync etc end up exploding the destination. Save and then load of images seems to be the only way to preserve images I've found. I'm interested to know if you've come up with a solution that is simpler?

  • Aleksey Pastuhov

    Thanks a lot, it helped! Also a good point to add - that you need to cleanup /var/lib/docker so it will not take the space ;)