【磁盘空间不足问题解决】Docker 日志清理
问题描述:
1、系统无法访问,提示“无法访问此网站”
2、启动Docker 镜像提示错误信息,如下:
“Error response from daemon: Cannot restart container 7f812bfba45f: write /var/lib/docker/containers/7f812bfba45fe73859bd40216a94800b6448346e0b5e645a9de63db2ed33435e/hash769099957: no space left on device”
问题解决:
1、查看磁盘空间
[root@pltpbqapp5 containers]# sudo du -sh * [root@pltpbqapp5 containers]# df -TH [root@pltpbqapp5 containers]# sudo find / -type f -size +100M
2、找到Docker日志
Docker 的日志文件存在 /var/lib/docker/containers 目录中,通过下面的命令可以将日志文件夹根据升序的方式罗列出来。
[root@pltpbqapp5 containers]# du -d1 -h /var/lib/docker/containers | sort -h
输出如下:
[root@pltpbqapp5 containers]# du -d1 -h /var/lib/docker/containers | sort -h 0 /var/lib/docker/containers/73a76ad6fc83d4a9034a7563cb5bd29c0d469453ae41dd119b8b63b09ce51cf3 20K /var/lib/docker/containers/32f33d28227bb882a696d8d284de3b75d7a0bc940df8e2e67b5e9e02ce704aff 56K /var/lib/docker/containers/379ccf129c7d03447f63f8599d0ab97d95a5c315f19018f1bf76ab18166090ef 188K /var/lib/docker/containers/464ac79af2a4ecb201930a662f89a2c6f4ddaf54754a9da9cf341087c74fe7a3 300K /var/lib/docker/containers/60decf8cd9ac943a08aa505a05a8e918c504d36a368fb7c50f101d3cfc39b266 3.2M /var/lib/docker/containers/2aa7823d93041339e3ed52dbf94a5bad3dbb2b15512315725b6b04bfc1d965ef 4.5M /var/lib/docker/containers/a5fdcf8b7c1446a20426de2fdc1884c3f9e62ab3ba4815f9818858d9795522d9 6.9M /var/lib/docker/containers/f0e340b0ccf8c8e91bd242eeb444e7269986ad07c200cd4a7d012320eb20af00 11M /var/lib/docker/containers/d2c6155db43a13f8cce6777ac576c76d2cabf93d44ca8a48c9db478eca0ecdab 16M /var/lib/docker/containers/ffe70a34f252e4f23c2ac91a22694b5568fc77d553d59f96137563d13700f4b2 20M /var/lib/docker/containers/89673d151c7fe59686719a3a9baf6e3e0f514b4b41e9d8acb5f38d127f5bc392 461M /var/lib/docker/containers/f0cb3a1ce0881540d998f4e3ab471bf141edf25f4ae0b157edc90a37415bb906 3.1G /var/lib/docker/containers/361274753a0a967818667c278c447c2091788acc3d03864c20c5920ecb696e3f 8.2G /var/lib/docker/containers/7f812bfba45fe73859bd40216a94800b6448346e0b5e645a9de63db2ed33435e
3、清理日志
由于日志文件比较多,一个个清理比较麻烦,这里直接使用脚本清理
创建文件:
[root@pltpbqapp5 /]# cd /home/testops/
[root@pltpbqapp5 /]# touch clean_docker_logs.sh [root@pltpbqapp5 /]# vim clean_docker_logs.sh
内容如下:
#!/bin/bash echo "==================== start clean docker containers logs ==========================" logs=$(find /var/lib/docker/containers/ -name *-json.log) for log in $logs do echo "clean logs : $log" cat /dev/null > $log done echo "==================== end clean docker containers logs =========================="
脚本赋予执行权限:
[root@pltpbqapp5 testops]# chmod 700 clean_docker_logs.sh
执行脚本:
/home/testops/clean_docker_logs.sh
执行完成:
[root@pltpbqapp5 testops]# /home/testops/clean_docker_logs.sh ==================== start clean docker containers logs ========================== clean logs : /var/lib/docker/containers/f0e340b0ccf8c8e91bd242eeb444e7269986ad07c200cd4a7d012320eb20af00/f0e340b0ccf8c8e91bd242eeb444e7269986ad07c200cd4a7d012320eb20af00-json.log clean logs : /var/lib/docker/containers/f0cb3a1ce0881540d998f4e3ab471bf141edf25f4ae0b157edc90a37415bb906/f0cb3a1ce0881540d998f4e3ab471bf141edf25f4ae0b157edc90a37415bb906-json.log ... ==================== end clean docker containers logs ==========================
4、查看清理结果
[root@pltpbqapp5 testops]# du -sh /var/lib/docker/containers
396K /var/lib/docker/containers
5、 重启docker 服务,问题解决
[root@pltpbqapp5 /]# docker restart 7f812bfba45f
7f812bfba45f
posted on 2023-03-13 10:09 SH-xuliang 阅读(285) 评论(0) 编辑 收藏 举报