Frequently Asked Question
To move the Docker storage directory (data root) from its default location (/var/lib/docker) to a new location, such as /home/var/lib/docker, follow these steps:
This process ensures data integrity by stopping Docker, copying all existing data, configuring Docker to use the new location, and then restarting the service.
Step 1: Stop the Docker Daemon
It is crucial to stop the Docker daemon to prevent any data corruption during the move.
sudo systemctl stop docker
Step 2: Create the New Docker Data Directory
Create the new directory where Docker's data will be stored. Replace /home/var/lib/docker with your desired path if it differs. Ensure the parent directories exist or create them.
sudo mkdir -p /home/var/lib/docker
Step 3: Copy Existing Docker Data to the New Location
Use rsync to copy all existing data from the old Docker directory to the new one. The -a flag preserves permissions, ownership, and timestamps, and -P shows progress.
sudo rsync -aP /var/lib/docker/ /home/var/lib/docker/
Verify that the copy was successful by listing the contents of the new directory.
ls /home/var/lib/docker
Step 4: Configure Docker to Use the New Data Directory
Edit or create the Docker daemon configuration file (daemon.json) to point Docker to the new data root.
First, check if the daemon.json file already exists:
ls /etc/docker/daemon.json
If the file does not exist, create it. If it exists, open it for editing.
sudo nano /etc/docker/daemon.json
Add or modify the data-root entry within the JSON file. If other configurations already exist, add data-root as a new entry, ensuring correct JSON syntax (commas between entries).
{
"data-root": "/home/var/lib/docker"
}
Save and close the file. In nano, this is typically Ctrl+O then Enter, followed by Ctrl+X.
Step 5: Reload Systemd Daemon and Start Docker
After modifying the Docker configuration, reload the systemd manager configuration to ensure it picks up the changes, then start the Docker daemon.
sudo systemctl daemon-reload
sudo systemctl start docker
To verify Docker is running and using the new data-root, check the Docker info:
docker info | grep "Docker Root Dir"
The output should show /home/var/lib/docker (or your chosen path).
Step 6: Verify Functionality
Check if your Docker containers, images, and volumes are still accessible and functional.
docker ps -a
docker images
docker volume ls
If everything appears correctly, attempt to start one of your existing containers to ensure it functions as expected.
Step 7: Remove the Old Docker Data Directory (Optional but Recommended)
Once you have confirmed that Docker is running correctly and all data is accessible from the new location, you can delete the original /var/lib/docker directory to free up space.
WARNING: Only perform this step after thoroughly verifying that Docker is working perfectly with the new data-root. Deleting the old directory prematurely could lead to data loss.
sudo rm -rf /var/lib/docker
