Solve the problem of insufficient disk space in docker

  • 2021-10-11 20:03:42
  • OfStack

The server where docker is located, after running for 1 period of time, found that the server disk directory was running out. Through du-h-max-depth=1/step-by-step directory investigation, it was found that the/var/lib/docker directory file was too large. Solve this problem by the following methods.

Transfer data to modify docker default storage location

There are many ways to modify the default storage location of docker.
It is best to modify the default storage location of docker to other large directories or disks in the first time after docker is installed. Avoid the risks caused by migrating data.

-Stop docker service


systemctl stop docker

-Create a new docker directory, execute the commands df-h, and find a large disk

I built the/data/docker/lib directory under the/data directory


 mkdir -p /data/docker/lib

-Migrate files under the/var/lib/docker directories to/data/docker/lib

Complete docker path after migration:/data/docker/lib/docker


rsync -avz /var/lib/docker/ /data/docker/lib/

-Configuration/usr/lib/systemd/system/docker. service


vi /usr/lib/systemd/system/docker.service

[Service]
ExecStart=/usr/bin/dockerd --graph=/data/docker/lib/docker

-Restart docker


systemctl daemon-reload
systemctl restart docker
systemctl enable docker

-Confirm that the Docker Root Dir modification has taken effect


[root@iZbp1jcwx7sfb1yrnvpg84Z docker]# docker info
...
Docker Root Dir: /data/docker/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
...

-After booting successfully, confirm whether the previous image is still there


[root@iZbp1jcwx7sfb1yrnvpg84Z docker]# docker images
REPOSITORY             TAG         IMAGE ID      CREATED       SIZE
10.80.177.233/policy        2.1.2        64ac4e178cd2    2 hours ago     818 MB
10.80.177.233/crm          2.1.3        d7636fbb7a29    2 hours ago     762 MB

-Delete files in the/var/lib/docker/ directory after making sure the container is ok


Related articles: