How To Move Docker's Data Directory To Free Up Disk Space

Photo edited by me from Ian Taylor / Unsplash

How To Move Docker's Data Directory To Free Up Disk Space

Running out of space? Relocate Docker’s storage to keep your system optimized and prevent unexpected downtime.

Paul Knulst  in  Docker Feb 14, 2025 5 min read

Introduction

When working with services like Docker which stores large amounts of data in locations that often do not have enough space can be a very frustrating experience, especially, if we are running out of disk space on our Linux server.

Recently, I had a case where the / partition on a host using docker was running very low on disk space.

💡
First, I checked if the logs were causing this problem again (see this article), but unfortunately, this wasn't the problem this time...

This time, I deployed so many containers on the host that the storage on the root partition (~15GB) was nearly full. Luckily, I had an additional disk mounted at /mnt/data01 with plenty of disk space.

So I was searching for a simple and effective solution to my problem which was moving Docker's default storage location to another directory on the disk mounted at /mnt/data01 to ensure that all docker services can operate without problems and prevent data loss.

In this article, I want to explain all the steps I did to safely relocate Docker's data directory so that everyone can do this without breaking their containerized applications if this problem ever occurs.

Why Move Docker Data Directory?

If Docker is installed its default behavior is that container images, volumes, and other runtime data (logs) will be stored in /var/lib/docker which will grow significantly over time because of:

  • Log files and container data
  • Persistent volumes containing application data (databases, files, etc)
  • Docker images and layers

When hosting Linux servers, the root partition often has limited space because it should only contain the filesystem and configuration files. This results in having Docker using all left space very fast resulting in slowdowns, crashes, or failures if launching new Docker containers. The solution is to move the Docker's data directory to avoid these problems.

Step-by-Step Guide To Move Docker Data Directory

💡
Moving Docker's data directory on Linux is very easy and can be achieved by following the seven steps mentioned here. I personally think that no errors can occur during the move, but to be sure you should back up everything before starting.

Stop the Docker Service

Before we can move the Docker's data directory we should stop the Docker service to prevent any data loss while moving the files.

sudo systemctl stop docker

Running this command will ensure that no active processes are using Docker’s current data directory while we move it. Unfortunately, this will stop all services running on our host. If we do this in production we have to plan it in a maintenance window!

Create or Edit Docker’s Configuration File

After the Docker service is stopped we can add (or set) a setting value in the daemon.json configuration file which normally is located at /etc/docker/daemon.json. If we cannot find it there we should create it and add the following configuration:

{
    "data-root": "/mnt/data01/docker"
}
💡
If your extra storage is located somewhere else replace /mnt/data01/docker with your preferred location.

Copy Existing Data to the New Location

Now, to ensure that every already created container, all images, and all volumes keep working, we have to copy the contents of /var/lib/docker (the old path) to the new location using the following command:

sudo cp -axT /var/lib/docker /mnt/data01/docker

In this command, the -a flag preserves the permissions, symbolic links, and all corresponding metadata. The -x is used to not copy data from other mounted filesystems and the -T property is used to simply copy the directory and not create an extra subdirectory.

💡
Important: If there are any files in the target directory make sure that the /var/lib/docker folder is not copied into a subdirectory by accident. The final path has to match the path that we set in the daemon.json

Restart Docker

The next step is restarting the Docker service:

sudo systemctl start docker

This command will restart the Docker service and should instruct Docker to use the new storage location which we defined in the daemon.json.

Verify Everything is Working

Finally, we should confirm that Docker is using the new directory and is working correctly. To check this, we can use the following command:

sudo docker info | grep "Docker Root Dir"

This command should return the new directory path we set:

Docker Root Dir: /mnt/data01/docker

Then, we should test that our containers, images, and volumes are working by simply listing them:

sudo docker images
sudo docker container ls
sudo docker ps -a

As a last step, we can check the container by accessing them. For example, if we host a website we can simply access it with the browser.

Alternative Methods to Freeing Up Disk Space

In some cases moving the Docker's data directory is not possible and we could use different techniques to free up disk space and move files.

Prune Unused Docker Data

The most important command for freeing up disk space while using Docker is:

Docker provides built-in commands to remove unused data and reclaim space:

sudo docker system prune -a

This built-in command from Docker is used to remove unused data and reclaim space. It will remove every stopped container, each not-used network, the dangling images, and all build caches (it won't delete the volumes!). We should keep in mind that we use it with caution as it will delete every image that is not linked to a running Docker container!

Clean Up Individual Components

If sudo docker system prune -a is too dangerous we can remove the individual parts separately.

Remove unused networks:

sudo docker network prune

Remove unused volumes:

sudo docker volume prune

Remove dangling images:

sudo docker image prune -a

Use Overlay or Bind Mounts

Another approach to moving the entire Docker directory, we could use Linux bind mounts to selectively move certain large parts in the Docker directory like images or volumes.

To do this, we simply create a Linux bind mount using the new path as the source and the docker directory as the destination:

sudo mount --bind /mnt/data01/docker/volumes /var/lib/docker/volumes

This solution is very flexible because we are able to manage the storage without affecting the Docker runtime.

Closing Notes

Having not enough disk space in a Docker environment could lead to heavy problems as I already learned in this "incident". By freeing up disk space regularly and moving Docker's data directory to a separate large partition it is possible to ensure a running system and our containerized applications.

I hope this article gave you an easy step-by-step guide to apply this to your Docker environment if you encounter that your Docker data directory is consuming too much space.

But remember, keeping an eye on your storage needs and proactively managing Docker's data, like enabling logrotate, will help you prevent these unexpected problems.

As a best practice, I would recommend cleaning up unused Docker resources regularly and monitoring disk usage on a daily basis to avoid running into problems! To do this, we could use Grafana, Netdata, Checkmk (Nagios), Zabbix, MinMon, or simply a CronJob.

💡
Important: I am working on a tutorial/article explaining how to monitor your system with a self-hosted Docker service that works in any Docker environment (and Kubernetes!).

However, I would love to hear your feedback about this tutorial. Furthermore, if you have any alternative approaches, please comment here and explain what you have done. Also, if you have any questions, please ask them in the comments. I try to answer them if possible.

Feel free to connect with me on MediumLinkedInTwitter, BlueSky, and GitHub.


🙌 Support this content

If you like this content, please consider supporting me. You can share it on social media, buy me a coffee, or become a paid member. Any support helps.

See the contribute page for all (free or paid) ways to say thank you!

Thanks! 🥰

By Paul Knulst

I'm a husband, dad, lifelong learner, tech lover, and Senior Engineer working as a Tech Lead. I write about projects and challenges in IT.