Programster's Blog

Tutorials focusing on Linux, programming, and open-source

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 the /etc/docker/daemon.json if it doesn't already exist (it didn't for me on Debian 12), and specify the new path location in it like so:

{
    "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

Why not use trash-cli instead, just to be safe?

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.

References

Last updated: 27th May 2024
First published: 27th May 2024