How to clean up /var/lib/docker/overlay2 space under Docker
1. View disk usage
df -h
2. Docker's built-in CLI command docker system df
It can be used to query the space usage of large space users such as images, containers, and local volumes.
~]# docker system df
view details
~]# docker system df -v
3. Space Cleanup
[1] Use Docker's built-in CLI command docker system prune to perform automatic space cleanup.
~]# docker system prune --help
By default, this directive clears all the following resources:
Stopped container (container)
volume not used by any container
A network not associated with any container
All dangling images.
By default, this command only clears dangling images, and unused images will not be deleted. After adding the -a or --all parameter, all unused mirrors and dangling mirrors can be cleared together.
You can add the -f or --force parameter to ignore related alarm confirmation information.
[2] In addition to the system level, there are also delete commands for the container or image level:
docker image prune: Delete dangling images.
docker container prune: delete useless containers.
--By default, the docker container prune command will clean up all containers in the stopped state
-- If you don't want to delete all of them so cruelly, you can also use the --filter flag to filter out the containers that you don't want to be cleaned up. Example: Clear all stopped containers, except those created within 24:
--$ docker container prune --filter "until=24h"
docker volume prune: Delete useless volumes.
docker network prune: delete useless network
【3】Manual clear
For dangling mirrors and unused mirrors, you can manually delete them individually
1. Delete all dangling images, do not delete unused images
docker rmi $(docker images -f "dangling=true" -q)
2. Delete all unused mirrors and dangling mirrors
docker rmi $(docker images -q)
3. Clean up volume
If the space occupied by the volume is too high, you can clear some unused volumes, including some volumes that are not called by any container (if LINKS = 0 is displayed in the -v details, it is not called):
Delete all volumes not referenced by containers:
docker volume rm $(docker volume ls -qf dangling=true)
4. Container cleaning
If you find that the container occupies too much space, you can manually delete some:
Delete all exited containers:
docker rm -v $(docker ps -aq -f status=exited)
Delete all containers in the dead state
docker rm -v $(docker ps -aq -f status=dead)
4. Find large files in the system [execute when the above three steps still fail]
find /var/lib/docker/overlay2/ -type f -size +100M -print0 | xargs -0 du -h | sort -nr
# Find all files larger than 100M in the specified directory
5. Limit the size and number of standard input logs
Create or modify /etc/docker/daemon.json, add log-dirver and log-opts parameters
vi /etc/docker/daemon.json
{
"log-driver":"json-file",
"log-opts": {"max-size":"3m", "max-file":"1"}
}
Restart the daemon thread of docker
systemctl daemon-reload
systemctl restart docker
6. docker clear space command
method one:
delete dangling mirror
docker rmi $(docker images -f "dangling=true" -q)
docker image prune -a -f
Display all containers, filter out the containers in the Exited state, and take out the IDs of these containers
sudo docker ps -a|grep Exited|awk '{print $1}'
#Query all containers, filter out the containers in Exited state, list the container IDs, and delete these containers
sudo docker rm `docker ps -a|grep Exited|awk '{print $1}'`
Method Two:
#Delete all non-running containers (the ones that are already running cannot be deleted, and the ones that are not running will be deleted together)
sudo docker rm $(sudo docker ps -a -q)
Method three:
#According to the status of the container, delete the container in the Exited state
sudo docker rm $(sudo docker ps -qf status=exited)
Method four:
After #Docker version 1.13, you can use the docker containers prune command to delete isolated containers.
sudo docker container prune
# delete all images
sudo docker rmi $(docker images -q)
7. There is really no way, only to clear all log files in the /var directory
~]# for i in `find /var -name *.log*`;do >$i;done
Then restart the node node, because some log files are occupied, and the space still cannot be released after clearing