docker启动容器后,默认Docker的日志会发送到容器的标准输出设备(STDOUT)和标准错误设备(STDERR),其中STDOUT和STDERR实际上就是容器的控制台终端。可以通过docker logs命令来查看某个容器输出的日志,具体如下:本文地址:http://www.04007.cn/article/1009.html,未经许可,不得转载.
#静态查看日志-不更新
docker logs 容器ID
#动态查看静态-实时更新
docker logs -f 容器ID本文地址:http://www.04007.cn/article/1009.html,未经许可,不得转载.
虽然我们能查看日志,但这些日志存储在哪里呢?这里就需要提到Docker logging driver这个概念。Docker logging driver是Docker收集容器日志及内部运行服务的日志的机制,这些机制统称为logging drivers。可以通过docker info命令查看到(也许有的不能,可能只列出当前docker支持的日志机制)。Plugins: Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog本文地址:http://www.04007.cn/article/1009.html,未经许可,不得转载.
上面在docker info命令结果中的Plugins项中Log里列出各项日志机制。docker官网有对应介绍:https://docs.docker.com/config/containers/logging/configure/ 本文地址:http://www.04007.cn/article/1009.html,未经许可,不得转载.
Driver Description
none No logs are available for the container and docker logs does not return any output.
local Logs are stored in a custom format designed for minimal overhead.
json-file The logs are formatted as JSON. The default logging driver for Docker.
syslog Writes logging messages to the syslog facility. The syslog daemon must be running on the host machine.
journald Writes log messages to journald. The journald daemon must be running on the host machine.
gelf Writes log messages to a Graylog Extended Log Format (GELF) endpoint such as Graylog or Logstash.
fluentd Writes log messages to fluentd (forward input). The fluentd daemon must be running on the host machine.
awslogs Writes log messages to Amazon CloudWatch Logs.
splunk Writes log messages to splunk using the HTTP Event Collector.
etwlogs Writes log messages as Event Tracing for Windows (ETW) events. Only available on Windows platforms.
gcplogs Writes log messages to Google Cloud Platform (GCP) Logging.
logentries Writes log messages to Rapid7 Logentries.本文地址:http://www.04007.cn/article/1009.html,未经许可,不得转载.
虽然可用的logging driver有很多,不过常见的主要是json-file和journald。可以通过docker info命令查找Logging Driver关键能看到当前的docker使用的是哪种机制,也可以通过这个值可以通过docker的daemon.json配置文件来进行修改。在docker inspect的结果中它就是包含在HostConfig.LogConfig.Type项中。本文地址:http://www.04007.cn/article/1009.html,未经许可,不得转载.
#通过inspect查看当前docker使用的日志机制 docker inspect -f '{{.HostConfig.LogConfig.Type}}' <CONTAINER> #daemon.json中关于logging driver的配置 #cat /etc/docker/daemon.json { "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file": "3", "labels": "production_status", "env": "os,customer" } }本文地址:http://www.04007.cn/article/1009.html,未经许可,不得转载.
然后根据当前的docker使用的日志机制来查看docker的日志。
如果使用的是json-file机制:则docker启动后日志会在 /var/lib/docker/containers/容器ID/容器ID-json.log 位置,也可以通过 inspect 容器ID找到LogPath项即是日志文件路径。
如果使用的是journalctl机制:则需要了解日志管理工具journalctl的使用,journalctl是centos7上专有的日志管理工具,它从message文件里读取信息。centos7中使用Systemd统一管理所有Unit的启动日志。然后可以只用journalctl一个命令来查看所有日志(可区分内核日志和应用日志)。日志的配置文件是/etc/systemd/journald.conf。在CoreOS中使用本文地址:http://www.04007.cn/article/1009.html,未经许可,不得转载.
#查看docker容器日志。 journalctl -u docker.service #通过容器特征查看具体容器日志 journalctl -u docker CONTAINER_TAG=mytag journalctl -u docker CONTAINER_ID=13211dsdfwwq journalctl -f -u docker CONTAINER_NAME=server9本文地址:http://www.04007.cn/article/1009.html,未经许可,不得转载.
本文地址:http://www.04007.cn/article/1009.html 未经许可,不得转载. 手机访问本页请扫描右下方二维码.
![]() |
![]() |
手机扫码直接打开本页面 |