How to Change Docker Root Directory (Data-Root) Safely

By default, Docker stores all its data—images, containers, and volumes—in /var/lib/docker. This can quickly fill up your system partition. If you need to free up space or move to a larger disk, you must change the Docker root directory to a new location. In this guide, I’ll show you how to do it safely without losing your existing containers.

If you need to move this directory to another location (like a larger disk or a dedicated partition), here is a safe, step-by-step guide to do it.

Step-by-Step Guide

1. Stop the Docker Service
First, ensure no containers are running and the service is stopped to prevent data corruption.

$ sudo systemctl stop docker

2. Create the New Directory
Create the target directory where you want Docker to store its data from now on. Replace the path with your desired location.

$ sudo mkdir -p /path/to/new/docker-data

3. Move the Existing Data
Copy (or move) the current contents of the Docker directory to the new location. This preserves your existing images and containers.

$ sudo mv /var/lib/docker/* /path/to/new/docker-data/

4. Configure the Docker Daemon
Now, tell the Docker daemon about the new location. You need to edit (or create) the daemon configuration file.

$ sudo vim /etc/docker/daemon.json

Add the following content. Ensure the JSON syntax is correct. The key is data-root.

{
    "data-root": "/path/to/new/docker-data"
}

Note: If the file already has other configurations, just add this line, separated by a comma.

5. Restart Docker
Start the Docker service to apply the new configuration.

$ sudo systemctl restart docker

6. Verify the Change
Finally, check that Docker is now using the new directory.

$ docker info | grep "Docker Root Dir"

If successful, you should see your new path:

Docker Root Dir: /path/to/new/docker-data

That’s it. You can now safely remove the old directory (sudo rm -rf /var/lib/docker) once you have confirmed everything is working correctly.

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *