1.新建/etc/docker/daemon.json,若有就不用新建了。添加log-dirver和log-opts参数,样例如下:
# vim /etc/docker/daemon.json
{
"registry-mirrors": [
"<https://sucejt9z.mirror.aliyuncs.com>"
],
"log-driver":"json-file",
"log-opts": {"max-size":"200m", "max-file":"3"}
}
{
"log-driver":"json-file",
"log-opts": {"max-size":"200m", "max-file":"3"}
}
max-size=200m,意味着一个容器日志大小上限是200M,
max-file=3,意味着一个容器有三个日志,分别是id+.json、id+1.json、id+2.json。
2.然后重启docker的守护线程
命令如下:
sudo systemctl daemon-reload
sudo systemctl restart docker
【需要注意的是:设置的日志大小规则,只对新建的容器有效】
cat /dev/null > <container-id>-json.log
{
"log-driver": "json-file",
"log-opts": {
"max-size": "50m",
"max-file": "3"
}
}
systemctl daemon-reload
systemctl restart docker
path=/var/lib/docker/containers/
echo ""
echo "========== Clean Docker Containers Log =========="
echo "Path: "$path
cd $path
for file in $(ls)
do
if [ -d $file ];then
echo $file"-json.log"
cat /dev/null > $file/$file-json.log
else
echo 0
fi
done
echo "========== Clean Docker Containers Log =========="
echo ""