Today I encountered an online machine alarm. After checking, I found that the Overlay directory of docker takes up too much disk space. I will record the solution.
What is the Overlay directory for?
Docker's Overlay directory is used to store Docker containers.
Overlay is a joint file system that can overlay multiple file systems together to form a read-only base file system and a writable upper file system.
In Docker, each container has its own read-only base file system and a writable upper file system.
When Docker starts a container, a new directory is created in the Overlay directory, which contains the container's read-only base file system and a writable upper file system.
During container runs, all modifications to the file system are recorded in the upper file system. When the container is stopped, the directory and its contents are also deleted.
Using the Overlay directory can effectively save disk space because multiple containers can share the same underlying file system.
In addition, the Overlay directory can also increase the startup speed of the container, because the underlying file system only needs to be loaded once, instead of reloading each time the container is started.
What should be noted is:
- The Overlay directory is only suitable for Linux operating systems, because it relies on the Overlay file system of the Linux kernel.
- On Windows and macOS, Docker uses different storage drivers to manage the file system of the container.
Solution
1. Delete the Overlay directory
To delete Docker's Overlay directory, you can follow these steps:
To stop all running Docker containers, you can stop all containers using the following command:
docker stop $(docker ps -a -q)
To view the Docker storage driver you are currently using, you can use the following command to view:
docker info | grep “Storage Driver”
If the output contains "overlay" or "overlay2", it means that the Overlay storage driver is currently being used.
Execute the following command to delete the Overlay directory:
sudo rm -rf /var/lib/docker/overlay2
Here "/var/lib/docker/overlay2" is the default location of the Overlay directory. If the Overlay directory is stored in another location on your system, you need to modify the path accordingly.
Restart Docker service:
sudo systemctl restart docker
This way you can delete Docker's Overlay directory.
It should be noted thatDeleting the Overlay directory will delete the file system data of all containers, so make sure you have backed up the necessary data and that all containers have stopped before performing the deletion operation.。
2. Try to delete the excess questionnaire in this directory
If Docker's Overlay directory takes up too much data, you can follow these steps to try to free up some disk space:
To clean Docker containers and images, you can use the following command to clean up all stopped containers and unused images:
docker system prune -a
This command cleans up all unused images, stopped containers, networks, and data volumes. If you want to clean up unused images, you can use the following command:
docker image prune -a
Check if the Overlay directory contains deleted files that may be caused by the container not being cleaned correctly.
If such files exist, you can manually delete them to free up disk space.
You can use the following command to find these files:
sudo find /var/lib/docker/overlay2 -type f -name “deleted”
This command lists all files containing the "deleted" character.
To check whether Docker's log files occupy too much disk space, you can use the following command to clean Docker's log files:
sudo truncate -s 0 /var/lib/docker/containers//-
This command will clear the log files of all containers.
If the above method does not free up enough disk space, you may want to consider increasing disk space or moving the Overlay directory to another disk partition.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.