非常教程

Docker 17参考手册

引擎: 管理员指南 | Engine: Admin Guide

配置日志记录驱动程序 | Configuring logging drivers (Engine)

Docker 包含多种日志记录机制来帮助您从运行容器和服务中获取信息。这些机制被称为日志驱动程序。

每个 Docker 守护进程(daemon)都有一个默认日志记录驱动程序,每个容器使用该驱动程序,除非您将其配置为使用其他日志记录驱动程序

除了使用 Docker 附带的日志驱动程序之外,您还可以实现和使用日志驱动程序插件。记录驱动程序插件在 Docker 17.05和更高版本中可用。

配置默认记录驱动程序

要将 Docker 守护程序(daemon)配置为默认为特定的日志记录驱动程序,请将log-driver值设置为位于 Linux 主机/etc/docker/或 Windows 服务器主机上C:\ProgramData\docker\config\daemon.json文件中的日志记录驱动程序的名称。默认日志记录驱动程序是json-file。以下示例显式地将默认日志记录驱动程序设置为syslog

{
  "log-driver": "syslog"
}

如果日志记录驱动程序具有可配置选项,则可以使用log-opts键将它们作为 JSON 数组设置在daemon.json文件中。以下示例在json-file日志记录驱动程序上设置两个可配置选项:

{
  "log-driver": "json-file",
  "log-opts": {
    "labels": "production_status",
    "env": "os,customer"
  }
}

如果您不指定日志记录驱动程序,则默认为json-file。因此,JSON之类的命令的默认输出,诸如docker inspect <CONTAINER>

要查找 Docker 守护程序(daemon)的当前默认日志记录驱动程序,请运行docker info并且搜索Logging Driver。您可以在 Linux,macOS 或在 Windows 上的 PowerShell 上使用以下命令:

$ docker info |grep 'Logging Driver'

Logging Driver: json-file

配置容器的日志记录驱动程序

在启动容器时,可以使用--log-driver标志将其配置为使用与 Docker 守护程序(daemon)默认值不同的日志记录驱动程序。如果日志记录驱动程序具有可配置选项,则可以使用--log-opt <NAME>=<VALUE>标志的一个或多个实例来设置它们。即使容器使用默认的日志记录驱动程序,它也可以使用不同的可配置选项。

以下示例带有none日志驱动程序启动 Alpine 容器。

$ docker run -it --log-driver none alpine ash

要查找正在运行的容器的当前日志记录驱动程序,如果守护程序正在使用json-file日志记录驱动程序,请运行以下docker inspect命令,将容器名称或 ID 替换为<CONTAINER>

$ docker inspect -f '{{.HostConfig.LogConfig.Type}}' <CONTAINER>

json-file

使用环境变量或标签与日志驱动程序

一些日志记录驱动程序将一个容器的--env|-e--label多个标志的值添加到容器的日志中。本示例使用 Docker 守护程序(daemon)的默认日志记录驱动程序启动容器(让我们假设json-file),但设置环境变量os=ubuntu

$ docker run -dit --label production_status=testing -e os=ubuntu alpine sh

如果日志记录驱动程序支持它,则会向日志输出添加其他字段。以下输出由json-file日志驱动程序生成:

"attrs":{"production_status":"testing","os":"ubuntu"}

支持的日志驱动

支持以下日志记录驱动程序。如果适用,请参阅每个驱动程序文档的链接以了解其可配置选项。如果您使用的是日志驱动程序插件,则可能会看到更多选项。

驱动

描述

none

没有日志将可用于容器和码头日志不会返回任何输出。

json-file

日志格式为JSON。Docker的默认日志记录驱动程序。

syslog

将日志消息写入syslog工具。系统日志守护程序必须在主机上运行。

journald

将日志消息写入日记帐。journald守护程序必须在主机上运行。

gelf

将日志消息写入Graylog扩展日志格式(GELF)端点,如Graylog或Logstash。

fluentd

将日志消息写入fluentd(正向输入)。fluentd守护进程必须在主机上运行。

awslogs

将日志消息写入Amazon CloudWatch Logs。

splunk

使用HTTP事件收集器将日志消息写入splunk。

etwlogs

将日志消息写为Windows事件跟踪(ETW)事件。仅在Windows平台上可用。

gcplogs

将日志消息写入Google Cloud Platform(GCP)日志记录。

记录驱动程序的限制

docker logs命令不适用于除json-filejournald之外的其他驱动程序。

docker, logging, driver

引擎: 管理员指南 | Engine: Admin Guide相关

1.Amazon CloudWatch记录日志记录驱动程序(引擎) | Amazon CloudWatch logs logging driver (Engine)
2. 绑定挂载 | Bind mounts (Engine)
3.使用Prometheus(引擎)收集Docker指标 | Collect Docker metrics with Prometheus (Engine)
4.配置和运行Docker(引擎) | Configuring and running Docker (Engine)
5.使用systemd控制和配置Docker(引擎) | Control and configure Docker with systemd (Engine)
6.ETW日志记录驱动程序(引擎) | ETW logging driver (Engine)
7.流利的日志驱动程序(引擎) | Fluentd logging driver (Engine)
8.格式化命令和日志输出(引擎) | Format command and log output (Engine)
9.Google Cloud日志记录驱动程序(引擎) | Google Cloud logging driver (Engine)
10.Graylog扩展格式(GELF)日志记录驱动程序(引擎) | Graylog Extended Format (GELF) logging driver (Engine)
11.Journald日志记录驱动程序(引擎) | Journald logging driver (Engine)
12.JSON文件日志记录驱动程序(引擎) | JSON File logging driver (Engine)
13.在守护进程停机期间保持容器处于活动状态(引擎) | Keep containers alive during daemon downtime (Engine)
14.限制容器的资源(引擎) | Limit a container's resources (Engine)
15.通过大使容器链接(引擎) | Link via an ambassador container (Engine)
16.记录驱动程序的日志标记(引擎) | Log tags for logging driver (Engine)
17.Logentries日志驱动程序(引擎) | Logentries logging driver (Engine)
18.PowerShell DSC用法(引擎) | PowerShell DSC usage (Engine)
19.修剪未使用的Docker对象(引擎) | Prune unused Docker objects (Engine)
20.在容器中运行多个服务(引擎) | Run multiple services in a container (Engine)
21.运行时指标(引擎) | Runtime metrics (Engine)
22.Splunk日志记录驱动程序(引擎) | Splunk logging driver (Engine)
23.自动启动容器(引擎) | Start containers automatically (Engine)
24.存储概述(引擎) | Storage overview (Engine)
25.Syslog日志记录驱动程序(引擎) | Syslog logging driver (Engine)
26.tmpfs mounts
27.解决卷问题(引擎) | Troubleshoot volume problems (Engine)
28.使用日志驱动程序插件(引擎) | Use a logging driver plugin (Engine)
29.使用Ansible(引擎) | Using Ansible (Engine)
30.使用Chef(引擎) | Using Chef (Engine)
31.使用Puppet(引擎) | Using Puppet (Engine)
32.查看容器的日志(引擎) | View a container's logs (Engine)
33.Volumes (Engine)
Docker 17

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。

主页 https://docker.com/
源码 https://github.com/docker/docker
版本 17
发布版本 17.06