0%

docker实践11:管理Log文件

Docker Logs

一旦启动容器,Docker会记录进程的标准输出和标准错误,并且使用Docker客户端可以访问。

假设后台有一个Redis实例容器在运行,容器名是redis-server。使用Docker客户端,可以使用如下语句来访问标准输出和标准错误。

1
docker logs redis-server

SysLog

Docker默认使用json格式的log文件存储在主机上。这会在硬盘上存储大量的文件,可以把Log文件存储到其他的目标位置。

The Syslog log driver will write all the container logs to the central syslog on the host. “syslog is a widely used standard for message logging. It permits separation of the software that generates messages, the system that stores them, and the software that reports and analyses them.” Wikipedia

This log-driver is designed to be used when syslog is being collected and aggregated by an external system.

使用下面的语句可以把Redis的Log记录进syslog

1
docker run -d --name redis-syslog --log-driver=syslog redis

如果尝试用客户端访问这些Log,会出现错误:FATA[0000] “logs” command is supported only for “json-file” logging driver

应该使用syslog 流来访问。

停用记录日志

当容器日志很冗长的时候可以停用日志。

将log-driver设置成none即可以停用日志。

1
docker run -d --name redis-none --log-driver=none redis

inspect命令可以查看某个容器的Log配置。下面的命令会输出每个容器Log配置的部分。

1
2
3
4
5
docker inspect --format '{{ .HostConfig.LogConfig }}' redis-server

docker inspect --format '{{ .HostConfig.LogConfig }}' redis-syslog

docker inspect --format '{{ .HostConfig.LogConfig }}' redis-none

欢迎关注我的其它发布渠道