本地文件日志驱动程序

local 日志驱动程序捕获容器的 stdout/stderr 输出,并将其写入一个针对性能和磁盘使用优化的内部存储。

默认情况下,local 驱动程序为每个容器保留100MB的日志消息,并使用自动压缩来减少磁盘上的大小。100MB的默认值基于每个文件的默认大小为20M,以及此类文件的默认数量为5(以考虑日志轮换)。

警告

local 日志驱动程序使用基于文件的存储。这些文件设计为由 Docker 守护进程独占访问。使用外部工具与这些文件交互可能会干扰 Docker 的日志系统并导致意外行为,应避免这样做。

用法

要将local驱动程序设置为默认的日志记录驱动程序,请在daemon.json文件中将log-driverlog-opt键设置为适当的值,该文件位于Linux主机上的/etc/docker/或Windows Server上的C:\ProgramData\docker\config\daemon.json。有关使用daemon.json配置Docker的更多信息,请参阅daemon.json

以下示例将日志驱动程序设置为 local 并设置 max-size 选项。

{
  "log-driver": "local",
  "log-opts": {
    "max-size": "10m"
  }
}

重新启动Docker以使更改对新创建的容器生效。 现有容器不会自动使用新的日志配置。

您可以通过使用--log-driver标志为特定容器设置日志驱动程序,该标志用于docker container createdocker run

$ docker run \
      --log-driver local --log-opt max-size=10m \
      alpine echo hello world

请注意,local 是 bash 的保留关键字,因此在脚本中可能需要引用它。

选项

local 日志驱动程序支持以下日志选项:

OptionDescriptionExample value
max-sizeThe maximum size of the log before it's rolled. A positive integer plus a modifier representing the unit of measure (k, m, or g). Defaults to 20m.--log-opt max-size=10m
max-fileThe maximum number of log files that can be present. If rolling the logs creates excess files, the oldest file is removed. A positive integer. Defaults to 5.--log-opt max-file=3
compressToggle compression of rotated log files. Enabled by default.--log-opt compress=false

示例

这个示例启动了一个alpine容器,该容器最多可以有3个日志文件,每个文件的大小不超过10兆字节。

$ docker run -it --log-driver local --log-opt max-size=10m --log-opt max-file=3 alpine ash