Permanently Change the Root Data Directory in Docker
This article explains how to permanently switch the default root data directory /var/lib/docker/ to a different directory.
Background information
The /var/lib/docker directory stores all images, volumes from Docker and can grow significantly depending on how many containers are used. The directory is structured as follows:
root@js-checkmk-02:/var/lib/docker# ls buildkit containers engine-id image network overlay2 plugins runtimes swarm tmp volumes
You can see that it is used if you, for example, verify the status with df -h:
root@js-checkmk-02:/var/lib/docker# df -h Filesystem Size Used Avail Use% Mounted on udev 3.9G 0 3.9G 0% /dev tmpfs 794M 684K 794M 1% /run /dev/sda1 47G 3.8G 41G 9% / tmpfs 3.9G 0 3.9G 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock /dev/sda15 124M 12M 113M 10% /boot/efi tmpfs 794M 0 794M 0% /run/user/0 tmpfs 794M 0 794M 0% /run/user/1000 overlay 47G 3.8G 41G 9% /var/lib/docker/overlay2/a6a4203cef42815c7187f584436e5eb9dff385e3ca9c92d597a38806b9dc255d/merged
Configuration
First, docker must be stopped, which has immediately influence on all active containers. In addition, we install the tool rsync.
root@js-checkmk-02:/# systemctl stop docker && apt install rsync
After this, it must be communicated to Docker where to find the new data root:
# Create directory if it has not been created yet
root@js-checkmk-02:/# mkdir -p /root/docker/root
# Daemon-Datei anlegen in /etc/docker
root@js-checkmk-02:/# nano /etc/docker/daemon.json
# Please add the following into the file, then adjust your path and store it. In this example, we change it to /root/docker/root
{
"data-root": "/root/docker/root"
}
Next, the whole content of the old docker-root-directory is copied into the new data-root-directory and then the old /var/lib/docker directory is renamed to /var/lib/docker.old.
rsync -aP /var/lib/docker/ "/root/docker/root" cp -rp /var/lib/docker/* "/root/docker/root" mv /var/lib/docker /var/lib/docker.old
After that, Docker can be restarted and it should be verified if all containers still function:
systemctl start docker
If everything functions, the old data-root-directory can be deleted:
rm -rf /var/lib/docker.old
|
Author: Jonas Sterr Jonas Sterr has been working for Thomas-Krenn for several years. Originally employed as a trainee in technical support and then in hosting (formerly Filoo), Mr. Sterr now mainly deals with the topics of storage (SDS / Huawei / Netapp), virtualization (VMware, Proxmox, HyperV) and network (switches, firewalls) in product management at Thomas-Krenn.AG in Freyung.
|
|
Translator: Alina Ranzinger Alina has been working at Thomas-Krenn.AG since 2024. After her training as multilingual business assistant, she got her job as assistant of the Product Management and is responsible for the translation of texts and for the organisation of the department.
|


