cancel
Showing results for 
Search instead for 
Did you mean: 
vsolodkyi
Sisense Team Member
Sisense Team Member

Relocating /var/lib/docker directory

Introduction

Docker uses /var/lib/docker to store images, containers, and locally named volumes. Deleting this can result in data loss and possibly stop the engine from running. However, in case it is needed to free some space on the root volume, it is possible to relocate this directory to another partition or drive. 
This tutorial describes two ways how to move the /var/lib/docker directory in Linux.

Prerequisites

As the prerequisite, a new directory for docker should be prepared on a second partition (or a separate drive):
1. Make sure that a separate partition is mounted. In this example, a partition called “/dev/mapper/sisense_vg-sisense” mounted at “/second_drive”: 

 

df -h

Filesystem                      Size  Used Avail Use% Mounted on
udev                             16G     0   16G   0% /dev
tmpfs                           3.2G  956K  3.2G   1% /run
/dev/mapper/sisense_vg-root     140G  7.7G  125G   6% /
tmpfs                            16G     0   16G   0% /dev/shm
tmpfs                           5.0M     0  5.0M   0% /run/lock
tmpfs                            16G     0   16G   0% /sys/fs/cgroup
/dev/sda2                       1.9G  212M  1.6G  12% /boot
/dev/sda1                       511M  4.0K  511M   1% /boot/efi
/dev/mapper/sisense_vg-sisense   86G  262M   82G   1% /second_drive
tmpfs                           3.2G     0  3.2G   0% /run/user/1000

 

2. Create a new docker directory:
sudo mkdir /second_drive/docker

3. Stop the docker service before making any changes in the configuration: 
sudo systemctl stop docker

4. Confirm that the docker engine is stopped: 
sudo systemctl docker status

5. If the docker engine is stopped, the old directory could be copied into a new place with:
sudo rsync -avxP /var/lib/docker/ /second_drive/docker/

Once copied, it is possible to make changes in the Docker configuration for the working directory. This tutorial will describe two ways to do this.

 Method 1. Modifying Docker engine service configuration

  1. Edit docker service configuration with: 
    sudo vi /lib/systemd/system/docker.service 
  2. Locate the Exec line:
    ExecStart=/usr/bin/dockerd  -H fd:// --containerd=/run/containerd/containerd.sock
  3. Add a parameter that points docker to the new directory:
     ExecStart=/usr/bin/dockerd --data-root /second_drive/docker -H fd:// --containerd=/run/containerd/containerd.sock
    In older versions of docker, -g key is used:
    ExecStart=/usr/bin/dockerd -g /second_drive/docker -H fd:// --containerd=/run/containerd/containerd.sock
  4. Reload configuration and start docker with:
    sudo systemctl daemon-reload
    sudo systemctl start docker
  5. Test if it is up and running with: 
    sudo systemctl status docker
  6. If the service is up and running, it is good to remove the old directory:
    rm -r /var/lib/docker

 Method 2. Create a symbolic link to a new directory
This method is simpler and does not require configuration editing.

  1. Once /var/lib/docker is copied to a new place, move it to backup place:
    mv /var/lib/docker /var/lib/docker.bkp
  2. Create a symbolic link to a new directory:
    sudo ln -s /second_drive/docker /var/lib/docker
  3. Start docker with: 
    sudo systemctl start docker
  4. Test if it is up and running with: 
    sudo systemctl status docker
  5. If service is up and running, it is good to remove the old directory:
    rm -r /var/lib/docker.bkp

Conclusion
This tutorial has provided two methods for relocating the /var/lib/docker directory in Linux, which is essential for storing Docker images, containers, and locally named volumes. Both methods offer a way to move this directory to another partition or drive which helps to free space in the root partition for further usage by other services.

Rate this article:
Version history
Last update:
‎10-30-2023 07:54 AM
Updated by:
Contributors