个人博客原文:
https://blog.ywandy.top/2019/05/25/docker%E6%97%A5%E5%BF%97%E6%B8%85%E7%90%86%E6%96%B9%E6%B3%95/
方法1: 使用脚本定期清理(手动删除docker 容器的log文件)
- 找出docker容器的日志大小:
#!/bin/sh
echo "======== docker containers logs file size ========"
logs=$(find /var/lib/docker/containers/ -name *-json.log)
for log in $logs
do
ls -lh $log
done
- 删除docker容器日志:
#!/bin/sh
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 ========"
方法2: 通过设定docker容器的日志大小(达到容量会自动清理)
- 使用docker-compose 设定:
nginx:
image: nginx:1.12.1
restart: always
logging:
driver: “json-file”
options:
max-size: “5g”
- 使用docker全局配置:
# vim /etc/docker/daemon.json
{
"log-driver":"json-file",
"log-opts": {"max-size":"500m", "max-file":"3"}
}