- Published on
Moving Docker on an Extra Disk
- Authors
- Name
- Marcio Moreira Junior

As your Docker workloads grow, storage management becomes a crucial factor in maintaining system performance and reliability. By default, Docker stores its data on the system disk, which can lead to space limitations, performance bottlenecks, and potential system crashes. In this guide, we'll walk you through the process of moving Docker's storage to an external or dedicated disk. This simple adjustment can help improve performance, increase storage capacity, and enhance scalability. Whether you're managing a production environment or optimizing your development setup, this step-by-step tutorial will ensure a smooth transition.
Why Move Docker Data to an External Disk?
By default, Docker stores its data in /var/lib/docker, which is usually located on the system's main disk. As containerized applications grow, Docker's storage requirements can increase significantly, leading to potential performance issues, limited space, and even system crashes if the main disk becomes full. Moving Docker's storage to an external or dedicated disk offers several benefits:
- Improved Performance: Separating Docker's data from the main system disk can reduce I/O bottlenecks, especially in high-traffic environments.
- Increased Storage Capacity: A dedicated disk allows for more space, preventing Docker from consuming the root partition.
- Better Reliability and Scalability: Using an external or dedicated disk makes it easier to scale storage without affecting the system's core operations.
- Easier Backup and Recovery: Storing Docker data separately simplifies backup and disaster recovery processes.
For these reasons, moving Docker's storage to an external disk is a recommended best practice for maintaining a stable and efficient containerized environment.
Step-by-Step Guide to Using an Extra Disk for Docker
For this tutorial, I'll assume you have an extra disk mounted on your system and want to move Docker's data to this new location. Follow these steps to safely transfer Docker's storage to an external disk:
1 - Stop Docker:
First, stop the Docker service to prevent any write operations while moving the data.
sudo systemctl stop docker
sudo systemctl stop docker.socket
2 - Move the Data to the New Location:
Copy the current Docker directory (/var/lib/docker) to the new location at /mnt/docker-swarm.
sudo rsync -aH /var/lib/docker/ /mnt/docker-swarm/docker
3 - Rename the Old Directory (Optional)
For safety, you can rename the original directory before creating a symbolic link.
sudo mv /var/lib/docker /var/lib/docker.bak
5 - Verify Permissions:
Ensure the permissions are correct for Docker to access the new location.
sudo chown -R root:root /mnt/docker-swarm/docker
sudo chmod -R 755 /mnt/docker-swarm/docker
6 - Restart Docker:
Now, restart the Docker service.
sudo systemctl start docker
sudo systemctl start docker.socket
7 - Verify Everything is Working:
Check if Docker is using the new directory correctly.
docker info | grep "Docker Root Dir"
Cleanup (Optional)
After verifying that everything is working correctly, you can remove the old backup:
sudo rm -rf /var/lib/docker.bak
Troubleshooting
If any overlay is mounted, as shown in the example below, it will be necessary to unmount it:
root@server:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 16G 0 16G 0% /dev
tmpfs 3.2G 688K 3.2G 1% /run
/dev/sdb1 19G 11G 9.0G 54% /
tmpfs 16G 0 16G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda1 100G 6.3G 94G 7% /mnt/docker-swarm
tmpfs 3.2G 0 3.2G 0% /run/user/1000
overlay 19G 11G 9.0G 54% /var/lib/docker.bak/overlay2 19G 11G 9.0G 54% /var/lib/docker.bak/overlay2
Steps to Fix the Issue
1 - Stop the Docker Service (Again):
Before unmounting the file systems, stop Docker to ensure it does not keep active mounts.
sudo systemctl stop docker
sudo systemctl stop docker.socket
2 - Unmount the Overlay File Systems:
Use the umount command to manually unmount each overlay file system. For example:
sudo umount /var/lib/docker.bak/overlay2/31fd0871764cc9bcc280854fbef90b83dfcc7d739c94edf37419c65e2b9d11b6/merged
sudo umount /var/lib/docker.bak/overlay2/a1a1281a34cd33b01f940d94012f66caab599640e24ac3d86d36f7b1bd8a26c4/merged
sudo umount /var/lib/docker.bak/overlay2/67cbc6754500503cbc507f645f59a66e288a580377d281d45f38df81a7146e68/merged
3 - Check if All File Systems Have Been Unmounted:
Use the df -h or mount command to check if there are no more overlay mounts in the /var/lib/docker.bak directory.
4 - Confirm the Data Move:
Now that the file systems have been unmounted, you can proceed to move the data to the new location without risk of errors.
5 - Delete the Original Backup (Optional):
If you are confident that the new setup is working, you can remove the backup folder:
sudo rm -rf /var/lib/docker.bak
6 - Restart Docker:
Start Docker again to check if it is working correctly with the new directory:
sudo systemctl start docker
sudo systemctl start docker.socket
Conclusion
Moving Docker's storage to an external disk is a simple yet effective way to optimize performance, prevent system storage issues, and improve scalability. By following the steps outlined in this guide, you can ensure that your Docker environment runs efficiently without overwhelming your main system disk.
Whether you're managing a small development setup or a large-scale production environment, proper storage management is key to maintaining stability and reliability. If you found this guide helpful, feel free to share your experience or ask any questions in the contact form!