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/edit the /etc/docker/daemon.json file:

sudo vim /etc/docker/daemon.json

This didn't already exist for me on Debian 12.

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/

The expanded form of the rsync command can be found in the appendix

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.

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

Last updated: 23rd January 2025
First published: 27th May 2024

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch