Relocating /var/lib/docker directory
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 |
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
- Edit docker service configuration with:
sudo vi /lib/systemd/system/docker.service - Locate the Exec line:
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock - 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 - Reload configuration and start docker with:
sudo systemctl daemon-reload
sudo systemctl start docker - Test if it is up and running with:
sudo systemctl status docker - 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.
- Once /var/lib/docker is copied to a new place, move it to backup place:
mv /var/lib/docker /var/lib/docker.bkp - Create a symbolic link to a new directory:
sudo ln -s /second_drive/docker /var/lib/docker - Start docker with:
sudo systemctl start docker - Test if it is up and running with:
sudo systemctl status docker - 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.