How to Change Docker Root Data Directory
Luke Hebert
- 2 minutes read - 313 wordsDocker stores all your images, volumes, and containers under /var/lib/docker by default. That directory can balloon quickly, and if your root filesystem is small, you will run out of space before you know it. Moving the data directory to a larger volume is a straightforward fix, and this guide walks you through the stop, configure, migrate, and verify steps for both legacy and modern Docker versions.
1. Stop the Docker daemon
sudo service docker stop2. Add a configuration file to tell the Docker daemon the new data directory location
Create or edit /etc/docker/daemon.json with the appropriate content for your Docker version.
Version before v17.05.0
{
"graph": "/path/to/your/new/docker/root"
}Replace /path/to/your/new/docker/root with the actual path where you want the new Docker data directory to live.
v17.05.0 and newer
Starting with v17.05.0, the --graph flag was replaced by the more descriptive --data-root flag. Use the following configuration:
{
"data-root": "/path/to/your/new/docker/root"
}The graph key was added before Docker 1.0, so it will not be removed, only hidden. You can still use it, but the Docker team discourages its use in favor of data-root.
3. Copy the current data directory to the new location
You can use either rsync or cp:
sudo rsync -aP /var/lib/docker/ /path/to/your/new/docker/root/or
sudo cp -rp /var/lib/docker/* /path/to/your/new/docker/root/4. Rename the old Docker directory
Renaming (rather than deleting) the old directory gives you a safety net in case something goes wrong.
sudo mv /var/lib/docker /var/lib/docker.old5. Restart the Docker daemon
sudo service docker start6. Test
Run a quick sanity check. Your existing containers and images should behave exactly as before:
docker ps -a
docker imagesIf everything looks correct, you are good to proceed.
7. Clean up old data
Once you are confident the new location is working, remove the old directory to reclaim disk space:
sudo rm -rf /var/lib/docker.old