How to Migrate Docker Data Directory to Another Partition

docker directory migrationmove docker datadocker storage partitionlinux docker setup
Published·Modified·

After Docker installation, the default directory is /var/lib/docker. If this directory is not partitioned separately, it will consume space on the / partition. If the root partition fills up, it will affect the normal use of the server, so it is necessary to migrate the Docker directory to a non-root partition.

Plan Partitioning in Advance

If you are preparing to run Docker, you can plan the partitioning in advance and mount /var/lib/docker separately to a partition. This avoids occupying the root partition.

Migrate Docker Directory to Another Partition

If your Docker is already running and you forgot to plan partitioning before, you need to migrate the data. First, stop the Docker service:

systemctl stop docker

Backup the files:

mv /var/lib/docker /var/lib/docker_bak

Migrate the files to another partition. A new partition /home/disk2 has been created in advance. We will migrate the data to this partition:

cd /home/disk2
cp -a /var/lib/docker_bak docker

Create a symbolic link:

ln -s /home/disk2/docker /var/lib/docker

Finally, restart the Docker service:

systemctl start docker

After testing to ensure there are no issues, you can delete the backup directory /var/lib/docker_bak.

This article references part of the content from: Docker Container Directory Migration