move docker default directory

Docker's default directory is /var/lib/docker

Some linux admin likes to assign only a few GBs to the var directory, so running a few docker images can fill up the space very easily.

Here is how to move the default directory to a new directory, usually with a bigger space.

Go to /etc/docker/daemon.json (if it doesn’t exist, create it)

Add the following 

   {

     "data-root": "/new directory/docker"

   }

   

Stop the docker systemd service

$sudo systemctl stop docker

Copy existing docker data from default directory to the new directory

$sudo rsync -axPS /var/lib/docker /new directory/docker

The rsync command might not be available then just yum install it. The used parameters are:

-a, --archive             archive mode;

-x, --one-file-system     don't cross filesystem boundaries

-P                        show progress

-S, --sparse              handle sparse files efficiently

Note, the rsync doesn't seem to work??? Docker doesn't seem to pick up the old images and containers. 

So, make a backup of images to run them using the new directory

Now start docker again

$sudo systemctl start docker

Check if the docker root directory has been changed.

$docker info

Should have:

Docker Root Dir: /new directory/docker

Then load the backup images and run them, if all good, delete old directory

$sudo rm -r /var/lib/docker