Moving docker root directory

I have a Mini-PC where I usually do my docker builds from, which works great. The only problem was storage:

$ df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        20G  8.3G  1.6G  84% /
/dev/sda3        97G   11G   82G  12% /home

Docker stores its storage by default under /var/lib/docker which we can retrieve using:

docker info | grep "Docker Root Dir"

Up until now, I have regularly cleaned it up using:

docker system prune -f --all

But since I have a partition with more than enough storage at /home I will use that partition for my docker data storage.

Preparing the new path

First we need to stop docker:

sudo systemctl stop docker.socket
sudo systemctl stop docker

Now we need to create the new storage directory location path:

mkdir -p /home/ruan/docker

Then we need to define the new data path in /etc/docker/daemon.json:

{
  "data-root": "/home/ruan/docker"
}

Migrate the data

Now we need to migrate the docker data from its old location to the new location:

sudo rsync -aP /var/lib/docker/ /home/ruan/docker/

Once that has completed, we can start up the service again.

Start Docker

Start docker:

sudo systemctl start docker

We can then verify if the new storage location is recognized by docker:

docker info | grep "Docker Root Dir"

If the output is true to the location that you configured it too, you can then clean out the old directory path:

sudo rm -rf /var/lib/docker

And then we will see our storage issue has been sorted:

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        20G  6.3G   13G  34% /
/dev/sda3        97G   11G   82G  12% /home

Thank You

Thank you for reading. If you like my content, please feel free to visit me on my website:

Or follow me on Twitter: