docker 日志清理,限制日志数量大小

限制大小,在得到服务器时,容器创建前,设置全局限制。

# vim /etc/docker/daemon.json
{
"registry-mirrors": ["http://f613ce8f.m.daocloud.io"],
"log-driver":"json-file",
"log-opts": {"max-size":"500m", "max-file":"3"}
}

 设置完成,执行

systemctl daemon-reload
systemctl restart docker

 

日志清理处理:

新建文件 clean_docker_log.sh

[root@izm5ej4bsah4npfgi5wl3vz soft]# vi clean_docker_log.sh 
#!/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 ========"

 

执行效果:

[root@izm5ej4bsah4npfgi5wl3vz soft]# ./clean_docker_log.sh 
======== start clean docker containers logs ========
clean logs : /var/lib/docker/containers/5dd30b084c0e947e6a91b22d59d5a02e2020e4189590581cae92b0ecf7a259cb/5dd30b084c0e947e6a91b22d59d5a02e2020e4189590581cae92b0ecf7a259cb-json.log
clean logs : /var/lib/docker/containers/37b47ba3cf99b78e76cdc0e789dd09897a3540e2ea8ba91cbefb0111df6991cf/37b47ba3cf99b78e76cdc0e789dd09897a3540e2ea8ba91cbefb0111df6991cf-json.log
clean logs : /var/lib/docker/containers/4985eea14f54754cb1a48dfa1ada014d97728a6121957e0df0fd004f048c6e0f/4985eea14f54754cb1a48dfa1ada014d97728a6121957e0df0fd004f048c6e0f-json.log
clean logs : /var/lib/docker/containers/4eb490fb0784d89fc465cbd0f509c82e885721fde39571ca0dde1bf78204379b/4eb490fb0784d89fc465cbd0f509c82e885721fde39571ca0dde1bf78204379b-json.log
======== end clean docker containers logs ========

日志大小查看:

新建脚本文件:docker_log_size.sh

[root@izm5ej4bsah4npfgi5wl3vz soft]# vi docker_log_size.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

 

 

 

执行效果:

[root@izm5ej4bsah4npfgi5wl3vz soft]# ./docker_log_size.sh 
======== docker containers logs file size ========
-rw-r----- 1 root root 0 Jun 28 19:17 /var/lib/docker/containers/5dd30b084c0e947e6a91b22d59d5a02e2020e4189590581cae92b0ecf7a259cb/5dd30b084c0e947e6a91b22d59d5a02e2020e4189590581cae92b0ecf7a259cb-json.log
-rw-r----- 1 root root 0 Jun 28 19:17 /var/lib/docker/containers/37b47ba3cf99b78e76cdc0e789dd09897a3540e2ea8ba91cbefb0111df6991cf/37b47ba3cf99b78e76cdc0e789dd09897a3540e2ea8ba91cbefb0111df6991cf-json.log
-rw-r----- 1 root root 940 Jun 28 19:18 /var/lib/docker/containers/4985eea14f54754cb1a48dfa1ada014d97728a6121957e0df0fd004f048c6e0f/4985eea14f54754cb1a48dfa1ada014d97728a6121957e0df0fd004f048c6e0f-json.log
-rw-r----- 1 root root 0 Jun 28 19:17 /var/lib/docker/containers/4eb490fb0784d89fc465cbd0f509c82e885721fde39571ca0dde1bf78204379b/4eb490fb0784d89fc465cbd0f509c82e885721fde39571ca0dde1bf78204379b-json.log

 

你可能感兴趣的:(docker 日志清理,限制日志数量大小)