By default, Docker organizes its data in the directory “/var/lib/docker” on the root partition. As your applications and containers expand, this directory can quickly occupy significant space. Symptoms such as degraded server performance, an inability to save data, or random crashes often indicate that the root partition is nearing full capacity. This guide will explore effective strategies for relocating your Docker volumes or data to an alternative partition, thus alleviating pressure on the root partition.
The Importance of Moving Docker Volumes
Docker containers frequently generate substantial amounts of persistent data, which can quickly exhaust your root partition’s storage capacity. Moving Docker volumes to a different partition can offer several benefits:
- Free up space on the root filesystem
- Avoid system crashes caused by disk space shortages
- Allocate larger application data to exclusive storage solutions
- Enhance overall management of storage resources
For instance, database containers like MySQL or PostgreSQL can store numerous gigabytes of data. Transferring these volumes to a more spacious partition can significantly bolster system stability.
How to Find the Docker Volume Directory
Locating your Docker volume directory is a crucial first step before initiating the move. Use the command below to list all of your available Docker volumes:
docker volume ls

To retrieve detailed information about a specific volume, execute the following command:
docker volume inspect volume_name
Within the output, locate the Mountpoint field, which indicates the exact directory where Docker retains the volume data.

How to Move the Entire Docker Data Root
To transfer all Docker data—including volumes, images, and logs—to a different partition, you can move the entire Docker storage directory. This comprehensive approach ensures that “/var/lib/docker” does not fill up.
Begin by stopping the Docker service to prevent file modifications during the migration:
sudo systemctl stop docker
Next, utilize the rsync command to copy the Docker data:
sudo rsync -avxP /var/lib/docker/ /path/to/new/partition/docker-data/
This command will replicate the Docker directory while retaining permissions and displaying transfer progress.

If you need to preserve additional Docker attributes, you may use this enhanced command:
sudo rsync -aHAX --info=progress2 /var/lib/docker/ /mnt/docker-data/
This command is preferable due to its retention of more filesystem metadata and detailed progress indication.
After the transfer, rename the previous data directory as a backup:
sudo mv /var/lib/docker /var/lib/docker.old
Update the Docker configuration by editing the daemon file using:
sudo nano /etc/docker/daemon.json
Add the following configuration:
{"data-root": "/path/to/new/partition/docker-data"}

Afterward, restart Docker by executing:
sudo systemctl start docker
Confirm that the Docker data directory has updated successfully with:
docker info | grep "Docker Root Dir"

If everything appears correct, you may proceed to delete the old backup:
sudo rm -rf /var/lib/docker.old
Moving All Docker Data via Docker Desktop (GUI Method)
If you prefer a graphical interface, Docker Desktop enables you to transfer all Docker data by modifying the Disk Image Location. This method effectively shifts all data stored by Docker—including images, containers, and volumes—to a new location.
Open Docker Desktop, navigate to Settings, head to Resources → Advanced, and locate the Disk Image Location option.

Select Browse to designate a directory on the preferred partition for Docker’s storage, such as “/mnt/newdisk/docker-data”.Click Apply & Restart to initiate the data relocation.

Moving Specific Volume Data
For greater flexibility, you can move a specific Docker volume to a different partition while utilizing it with your container via a host bind mount. This approach is secure, straightforward, and simplifies backup processes. Begin by stopping the container that uses the volume:
docker stop container_name
Subsequently, utilize rsync to transfer the volume data safely:
sudo rsync -a /var/lib/docker/volumes/my_volume/_data/ /data/docker/my_volume/
This command will transfer all contents from the previous volume to “/data/docker/my_volume”.
You must adjust your “docker-compose.yml” file to reflect a bind mount instead of a Docker-managed volume:
services: my_service: volumes: - /data/docker/my_volume:/container/path
Make sure to replace /container/path with the original mount path inside the container. Redeploy the container using the following command:
docker-compose up -d
Conclusion
Transferring Docker volumes or the entire Docker data directory to a more spacious partition can significantly free up space on your root filesystem, enhancing server stability. This proactive approach minimizes the risk of data loss and simplifies future storage management.
If shifting the complete Docker directory proves unfeasible, other options exist to reclaim space, such as using the docker system prune command to eliminate unused containers, images, networks, and caches, or employing Linux bind mounts to move substantial directories without disrupting Docker’s operation.
Leave a Reply