DEV Community

Cédric Teyton for Promyze

Posted on

Update the Docker installation directory (Ubuntu/Debian)

Argh, my "/" partition is almost full

You may already have been in this situation where you setup Docker using the classic tutorial and thus use the directory /var/lib/docker as Docker root directory, where all containers, images and so on are stored. This directory gets quickly filled, and depending on your server structure, you may have a secondary partition (like /data) with plenty of disk space available.
Let's how to migrate this directory properly.

How to move Docker data ?

The process can last few minutes depending on the size of the Docker directory, but it won't last too much time. Just remember you need to shutdown Docker during this process.

Note that the following commands may be require sudo privileges.

First, we need to stop all your containers and the Docker service.

$> systemctl stop docker
Enter fullscreen mode Exit fullscreen mode

To make sure the Docker daemon is stopped, the following command should display an empty result :

$> ps aux | grep -i docker | grep -v grep
Enter fullscreen mode Exit fullscreen mode

Then, modify with your favorite text editor the systemd /lib/systemd/system/docker.service script that is launched during the sytem startup :

$> systemctl edit --full docker
Enter fullscreen mode Exit fullscreen mode

Locate the following line:

ExecStart=/usr/bin/docker daemon -H fd://
Enter fullscreen mode Exit fullscreen mode

Edit that line to add the -g option with the path of the new directory.

ExecStart=/usr/bin/docker daemon -g /new/path/docker -H fd://
Enter fullscreen mode Exit fullscreen mode

Replace /new/path/docker with the absolute path of the destination file that you wish. Save the file, then restart now the Docker demon:

$> systemctl daemon-reload
Enter fullscreen mode Exit fullscreen mode

Then, you have to create the new Docker directory and copy the current Docker directory to the new one with the rsync command:

$> mkdir /new/path/docker
$> rsync -aqxP /var/lib/docker/ /new/path/docker
Enter fullscreen mode Exit fullscreen mode

You may now start the Docker demon:

$> systemctl start docker
Enter fullscreen mode Exit fullscreen mode

Now you can check that the Docker demon is launched with the new -g option:

$> ps aux | grep -i docker | grep -v grep
root      2095  0.2  0.4 664472 36176 ?        Ssl  18:14   0:00 /usr/bin/docker daemon -g  /new/path/docker -H fd://
root      2100  0.0  0.1 360300 10444 ?        Ssl  18:14   0:00 docker-containerd -l /var/run/docker/libcontainerd/docker-containerd.soc
Enter fullscreen mode Exit fullscreen mode

You can also execute the docker images ands docker ps -a commands to check that all your Docker data is still there.

Once everything is ok, you can clean the old directory.

$> rm -rf /var/lib/docker
Enter fullscreen mode Exit fullscreen mode

Thats it ! 🚀

Discussion (0)