项目场景:
docker的日志无限增长,/var/lib/docker/containers占用太多空间,通过配置/etc/docker/daemon.json可设置docker日志文件的大小
问题描述
修改/etc/docker/daemon.json文件(如果没有这个文件,直接创建一个就好了,里面就是个json对象),增加如下配置
"log-driver": "json-file",
"log-opts": {"max-size": "100m", "max-file": "5"}
除了 json-file 还支持很多 logging driver
驱动程序 | 描述 |
---|---|
none | 容器没有日志可用,docker logs 什么都不返回 |
local | 日志以自定义格式存储,设计这种格式的目的是将开销降到最低。 |
syslog | 将日志消息写入 syslog 工具,syslog 守护程序必须在主机上运行。 |
journald | 将日志消息写入 journald,journald 守护程序必须在主机上运行。 |
gelf | 将日志消息写入 Graylog Extended Log Format (GELF) 终端,例如 Graylog 或 Logstash。 |
fluentd | 将日志消息写入 fluentd(forward input),fluentd 守护程序必须在主机上运行。 |
awslogs | 将日志消息写入 Amazon CloudWatch Logs。 |
splunk | 使用HTTP事件收集器将日志消息写入splunk。 |
etwlogs | 将日志消息写为 Windows 的 Event Tracing 事件,仅在Windows平台上可用。 |
gcplogs | 将日志消息写入 Google Cloud Platform (GCP) Logging。 |
logentries | 将日志消息写入 Rapid7 Logentries。 |
json-file | 日志格式化为 JSON,这是 Docker 默认的日志驱动程序。 |
log-opts
max-size为文件最大大小
max-file为最多保持几个文件
效果是这样的
配置完之后,重启docker
systemctl daemon-reload
systemctl restart docker
发现启动失败,根据提示打出日志,发现错误为:
unable to configure the Docker daemon with file /etc/docker/daemon.json: the following directives are specified both as a flag and in t…: json-file)

原因分析:
网上找的资料都说logging driver默认为json-file,但是我的docker是journald
dockers启动失败的原因就在此,/etc/docker/daemon.json里配置的logging driver与/etc/sysconfig/docker里的默认配置冲突了。
我的/etc/sysconfig/docker文件如下
# /etc/sysconfig/docker
# Modify these options if you want to change the way the docker daemon runs
OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false'
if [ -z "${DOCKER_CERT_PATH}" ]; then
DOCKER_CERT_PATH=/etc/docker
fi
# Do not add registries in this file anymore. Use /etc/containers/registries.conf
# instead. For more information reference the registries.conf(5) man page.
# Location used for temporary files, such as those created by
# docker load and build operations. Default is /var/lib/docker/tmp
# Can be overriden by setting the following environment variable.
# DOCKER_TMPDIR=/var/tmp
# Controls the /etc/cron.daily/docker-logrotate cron job status.
# To disable, uncomment the line below.
# LOGROTATE=false
# docker-latest daemon can be used by starting the docker-latest unitfile.
# To use docker-latest client, uncomment below lines
#DOCKERBINARY=/usr/bin/docker-latest
#DOCKERDBINARY=/usr/bin/dockerd-latest
#DOCKER_CONTAINERD_BINARY=/usr/bin/docker-containerd-latest
#DOCKER_CONTAINERD_SHIM_BINARY=/usr/bin/docker-containerd-shim-latest
可以看到OPTIONS=‘–selinux-enabled --log-driver=journald --signature-verification=false’
里面配置的参数指定了log-driver=journald
解决方案:
将/etc/sysconfig/docker文件中的
–log-driver=journald
删除即可。
或者把journald换成json-file。
理论上这两个地方保持一致就行,所以修改/etc/docker/daemon.json为journald也行。(我没有实验,json用的多,journald也不知道是啥,没时间研究,就使用json-file吧O(∩_∩)O)
我是直接删除了,启动成功!再看看此时的log-driver是什么,json-file,问题解决!
注意事项
这里的日志文件,其实是docker logs打出来的东西,如果你程序没有记录日志,那么这里的日志文件还是挺重要的,慎用这个配置吧
docker logs命令仅支持json-file和journald两种类型,其他driver无法使用该命令。
另外这个配置需要新创建容器才生效,对之前的容器不会起作用。
部分内容参考:
Docker 日志 logging-driver