Migrate docker data to different directory

This post guides us how to migrate the docker data from the existing directory to a target directory in the case that the old directory runs out of space.

Stop the docker daemon

$ cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
    
$ systemctl stop docker.service
$ ps aux | grep -i docker | grep -v grep

Add a configuration file to tell docker where is the new location of the data

$ mkdir /data/var_lib_docker/
$ vim /etc/docker/daemon.json
{
    "data-root": "/data/var_lib_docker"
}

Copy the docker data to new directory(It takes time)

sudo rsync -aP /var/lib/docker/ /data/var_lib_docker

Verify if the migration works

$ mv /var/lib/docker/ /var/lib/docker.old

$ sudo systemctl start docker

$ ps aux | grep -i docker | grep -v grep
root     29227  0.2  0.1 1244076 28448 ?       Ssl  10:43   0:01 /usr/bin/dockerd
root     29243  0.0  0.0 984268  7696 ?        Ssl  10:43   0:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc

$ docker info  | grep Root
    Docker Root Dir: /data/var_lib_docker

$ docker images
$ docker inspect 1cd20ecd897d | grep RootDir
"RootDir": "/data/var_lib_docker/overlay/90021ce8266c3f717e2d30e258311e850b50e946b7f68d505f504b008378414c/root"