Move Docker Data Directory
Introduction
After completing numerous manual steps to deploy and configure my new mail server via Docker, I realized the server needed more storage space to prevent running out of disk space in the coming months. Unfortunately, I discovered that this particular virtual machine had an EFI partition at the end of the disk partition table, rather than at the beginning. This meant my usual method for expanding a QCOW2-based VM wouldn’t work. The simplest solution was to add another disk and configure Docker to use the additional space. This tutorial will guide you through that process.
Steps
Add the additional disk to your server. If using KVM, you can use this guide.
Stop all containers and then stop the Docker service:
docker stop `docker ps -aq`
sudo service docker stop
Create/edit the /etc/docker/daemon.json
file:
sudo vim /etc/docker/daemon.json
Add the following to it (changing the path to whatever you want th new path to be).
{
"data-root": "/mnt/my-new-disk/docker"
}
Sync the existing docker data over to the new location and be sure to do this with sudo or as root to preserve permissions/ownership without any issues..
sudo rsync -aP \
/var/lib/docker/ \
/mnt/my-new-disk/docker/
Rename the old location so that we can test it works and revert if we need to:
sudo mv -i \
/var/lib/docker \
/var/lib/docker.old
Now start the docker service:
sudo service docker start
... and check it runs this hello world application just fine:
docker run --rm hello-world
If you got a "Hello from Docker!" message then you know you are fine and you can remove the original docker data folder location:
sudo rm -rf /var/lib/docker.old
Conclusion
That's it! Docker is now using the new storage location for all of it's data, and all the existing data should have been migrated across so it was able to start right where it left off.
Appdendix
Expanded Rsync Command
For reference, the fully expanded form of sudo rsync -aP /var/lib/docker/ /mnt/my-new-disk/docker/
from earlier is:
sudo rsync \
--recursive \
--links \
--perms \
--times \
--group \
--owner \
--devices \
--specials \
--partial \
--progress \
/var/lib/docker/ \
/mnt/my-new-disk/docker/
References
First published: 27th May 2024