概述
因环境需要对docker日志文件大小进行限制,对/etc/docker/daemon.json
文件进行编辑后遇到无法启动docker问题。
配置内容
[root@localhost ~]# cat /etc/docker/daemon.json
{
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
}
}
报错内容
这个含义应该是Docker启动的时候传入了命令行参数,同时也指定了配置文件,两个配置发生了冲突。那么就查看一下Docker服务启动文件。
[root@localhost ~]# systemctl status docker -l
11月 05 11:00:02 localhost.localdomain dockerd-current[9833]: unable to configure the Docker daemon with file /etc/docker/daemon.json: the following directives are specified both as a flag and in the configuration file: log-driver: (from flag: journald, from file: json-file)
排查步骤
查看Docker服务配置文件
可以看到启动的时候会从/etc/sysconfig/docker
中获取环境变量。
[root@localhost ~]# cat /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.com
After=network.target
Wants=docker-storage-setup.service
Requires=docker-cleanup.timer
\[Service\]
Type=notify
NotifyAccess=main
EnvironmentFile=-/run/containers/registries.conf
EnvironmentFile=-/etc/sysconfig/docker
EnvironmentFile=-/etc/sysconfig/docker-storage
EnvironmentFile=-/etc/sysconfig/docker-network
Environment=GOTRACEBACK=crash
Environment=DOCKER_HTTP_HOST_COMPAT=1
Environment=PATH=/usr/libexec/docker:/usr/bin:/usr/sbin
ExecStart=/usr/bin/dockerd-current
--add-runtime docker-runc=/usr/libexec/docker/docker-runc-current
--default-runtime=docker-runc
--exec-opt native.cgroupdriver=systemd
--userland-proxy-path=/usr/libexec/docker/docker-proxy-current
--init-path=/usr/libexec/docker/docker-init-current
--seccomp-profile=/etc/docker/seccomp.json
$OPTIONS
$DOCKER_STORAGE_OPTIONS
$DOCKER_NETWORK_OPTIONS
$ADD_REGISTRY
$BLOCK_REGISTRY
$INSECURE_REGISTRY
$REGISTRIES
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
TimeoutStartSec=0
Restart=on-abnormal
KillMode=process
`[Install]
WantedBy=multi-user.target`
可以看到参数中默认了--log-driver=journald
,把这一段删掉就可以了。
[root@localhost ~]# cat /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
[root@localhost ~]# sed -i 's/(^OPTIONS.`)--log-driver=journald (.`)/\1\2/g' /etc/sysconfig/docker
[root@localhost ~]# systemctl restart docker`
重启后服务正常无报错。