Change Docker Root Dir: Methods and Advantages
In this article I will explain the configurations we need to make to store the data of the docker running on a Linux server in a special directory.
Why would we want to make changes to the docker root location ?
- Disk Managment: Security and performance management can be simplified by storing Docker data on a different disk.
- Data Protection and Backup: By using a disk with special protection and backup processes, we can store Docker data in a healthier way.
- Distributed Disk Management: By storing Docker data on different machines, performance, data security and backup advantages can be achieved. For example, let’s assume that docker data is stored in a directory associated with nfs client and nfs server on ubuntu server. Backup and performance processes can be managed more successfully only on nfs server. Installing NFS on Ubuntu 20.04 server
How to find docker root path
The default docker root path is the directory “ /var/lib/docker ”. Find docker root directory with dokcer info command
docker info -f '{{ .DockerRootDir }}'
How to change Docker root path?
On our Linux machine we need to be the “root” user or we need a user with “sudo all” authorization.
First of all, we will stop docker services and we need to move the data in the docker root directory to the new directory. If we do not stop docker services, there may be problems with data migration.
Step 1: Stop Docker services
sudo systemctl stop docker
sudo systemctl stop docker.socket
sudo systemctl stop containerd
Step 2: The data in the Docker root path needs to be moved to the new directory. In the example, commands are created using the directory “/var/lib/docker”.
sudo mv /var/lib/docker /new_docker_path
Step 3: The “data-root” field in /etc/docker/daemon.json will be modified. If the daemon.json file does not exist in the /etc/docker directory, it needs to be created and filled in as follows.
The following command will allow us to see and modify the contents of the file if it exists, if not it will create a new file.
sudo nano /etc/docker/daemon.json
The data-root inside the daemon.json file should look like the example below.
{
"data-root": "/new_docker_path"
}
Step 4: After saving the changes in /etc/docker/daemon.json, docker services should be started.
sudo systemctl start docker
Once docker is running, we can check that the changes have taken place with the following command
docker info -f '{{ .DockerRootDir}}'
Summary
I tried to explain the Docker root directory change method and its advantages. The following sources were used while creating the article.
Resource: https://www.ibm.com/docs/en/z-logdata-analytics/5.1.0?topic=compose-relocating-docker-root-directory