互联网技术 / 互联网资讯 · 2024年1月16日

Docker教程:配置Docker日志驱动程序

docker 包含多种日志记录机制,可帮助您 从正在运行的容器和服务中获取信息。这些机制称为日志驱动程序。每个 docker 守护进程都有一个默认的日志驱动程序,每个容器都会使用它,除非您将其配置为使用不同的日志驱动程序,或简称为”log-driver”。

默认情况下,docker 使用json-filelogging driver,它在内部将容器日志缓存为 JSON。除了使用 docker 附带的日志驱动程序,您还可以实现和使用日志驱动程序插件。

配置默认日志驱动

要将 docker 守护程序配置为默认使用特定的日志驱动程序,请将 的值设置log-driver为daemon.json 配置文件中日志驱动程序的名称。有关详细信息,请参阅docker参考手册中的”守护程序配置文件”部分。

默认的日志驱动程序是json-file. 以下示例将默认日志记录驱动程序设置为local日志驱动程序:

{

“log-driver”: “local”

}

如果日志驱动程序具有可配置的选项,您可以在daemon.json文件中将它们设置 为带有键的 JSON 对象log-opts。以下示例在json-file日志记录驱动程序上设置了两个可配置选项:

{

“log-driver”: “json-file”,

“log-opts”: {

“Max-size”: “10M”,

“Max-file”: “3”,

“labels”: “Production_status”,

“env”: “os,customer”

}

}

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

如果未指定日志记录驱动程序,则默认为json-file. 要查找 docker 守护程序的当前默认日志记录驱动程序,请运行 docker info并搜索Logging Driver. 您可以在 linux、MacOS 或 Windows 上的 PowerShell 上使用以下命令:

$ docker info –format ‘{{.LoggingDriver}}’

json-file

为容器配置日志驱动程序

当您启动一个容器时,您可以使用–log-driver标志将其配置为使用不同于 docker 守护程序默认值的日志记录驱动程序。如果日志驱动程序具有可配置选项,您可以使用一个或多个–log-opt=标志实例来设置它们。即使容器使用默认日志驱动程序,它也可以使用不同的可配置选项。

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

$ docker Run -IT –log-driver none alpine ash

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

$ docker inspect -f ‘{{.hostConfig.LogConfig.Type}}’

json-file

OpenMagic API

Need more than content? Move into the product flow.

If you are here for model access, pricing, developer docs, or the future API console, the dedicated product path now lives on api.openmagic.ai.

登录免费注册