栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

docker配置log-driver后启动失败

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

docker配置log-driver后启动失败

项目场景:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/844826.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号