Moving /var/lib/docker to Another Partition with a Bind Mount

Moving /var/lib/docker and using a bind mount on Debian11

* This page contains promotional content

The root partition (/) of the machine I run Docker on was getting tight, so I moved /var/lib/docker

These days, when you install Linux without thinking about it, the automatic configuration often gives the root partition (/) something small such as 30GB.
When you run Docker, /var/lib/docker grows and grows, so / ended up in a state close to full.
So I moved it to /home, which is on a separate partition and has room to spare

Note that this time I use a bind mount on Debian11

Source: /var/lib/docker
Destination: /home/docker

Stopping Docker

# systemctl stop docker
# systemctl stop docker.socket

Moving /var/lib/docker

Create the /home/docker directory and copy the contents of /var/lib/docker with rsync

# mkdir /home/docker
# rsync -aXS /var/lib/docker/. /home/docker/

mount bind

Move /var/lib/docker out of the way and create a new, empty /var/lib/docker directory

# mv /var/lib/docker /bar/lib/docker.bak
# mkdir /var/lib/docker

Mount /home/docker as /var/lib/docker

# mount -o bind /home/docker /var/lib/docker

Unlike a symbolic link, this is handled the same way as mounting a CD-ROM or an external device, so it is not mounted when you reboot. That means fstab has to be edited

fstab

Whenever you edit fstab, be sure to take a backup

Add the following

/home/docker /var/lib/docker	none	bind	0	0

Checking the mount

Since I mounted it by hand, I unmount it once and check that it can be mounted from the fstab entry

# umount /var/lib/docker
# mount -a

# df -h
-
/dev/sda6        396G   17G  360G    5% /home
/dev/sda6        396G   17G  360G    5% /var/lib/docker

Adding it to DOCKER_OPTS

Since we are changing where containers are stored, it has to be specified explicitly

# pwd
/etc/default
# cat docker |grep DOCKER_OPTS
DOCKER_OPTS="-g /home/docker"

Starting Docker

# systemctl start docker

Once docker has started up without trouble, watch it for a while and delete /var/lib/docker.bak if there are no problems

Reference articles

See also