docker 容器 日志_如何为Docker容器设置日志轮换

docker 容器 日志

by Ying Kit Yuen

英杰苑

如何为Docker容器设置日志轮换 (How to setup log rotation for a Docker container)

我们都需要日志! (We all need logs!)

Sometimes working with Docker makes me feel like I’m working with a black box. Especially when I’m playing with Docker images from the community, and it doesn’t go the way I expected. In many cases, reading logs takes up a large portion of time when debugging.

有时候,与Docker一起工作会让我感觉就像在使用黑匣子。 尤其是当我使用社区中的Docker映像时,它并没有达到我的预期。 在许多情况下,调试时读取日志会占用大量时间。

This article is about setting up log rotation for Docker containers.

本文是关于为Docker容器设置日志轮换。

默认日志记录驱动程序 (The default logging driver)

We can configure different logging drivers for containers. By default, the stdout and stderr of the container are written in a JSON file located in /var/lib/docker/containers/[container-id]/[container-id]-json.log. If you leave it unattended, it can take up a large amount of disk space, as shown below.

我们可以为容器配置不同的日志记录驱动程序。 默认情况下,容器的stdoutstderr被写入位于/var/lib/docker/containers/[container-id]/[container-id]-json.log中的JSON文件中。 如果不加注意,它将占用大量磁盘空间,如下所示。

手动清除日志 (Purge the log manually)

If this JSON log file takes up a significant amount of the disk, we can purge it using the following command.

如果此JSON日志文件占用了大量磁盘,则可以使用以下命令清除它。

We could setup a cronjob to purge these JSON log files regularly. But for the long term, it would be better to setup log rotation.

我们可以设置一个cronjob来定期清除这些JSON日志文件。 但是从长远来看,最好设置日志轮转。

设置日志轮换 (Setup the log rotation)

配置默认的日志记录驱动程序 (Configure the default logging driver)

This can be done by adding the following values in /etc/docker/daemon.json. Create this file if it doesn’t exist.

这可以通过在/etc/docker/daemon.json中添加以下值来完成 。 如果该文件不存在,则创建它。

The json-file logging driver has a few more options, and we can even change to other logging drivers such as syslog. For more information, please refer to the Docker Docs — Configure logging drivers.

json文件日志记录驱动程序还有更多选项,我们甚至可以更改为其他日志记录驱动程序,例如syslog 。 有关更多信息,请参阅Docker Docs-配置日志记录驱动程序

Execute the following commands to reload the updated daemon.json. The new configuration will apply to all newly created containers after restart.

执行以下命令以重新加载更新的daemon.json 。 重新启动后,新配置将应用于所有新创建的容器。

为容器配置日志记录驱动程序 (Configure the logging driver for a container)

The configuration can also be done on the container level if you do not want to apply it globally.

如果您不想全局应用配置,也可以在容器级别进行配置。

The docker run command

docker run命令

We can specify the logging driver and options in the docker run command. For example:

我们可以在docker run命令中指定日志记录驱动程序和选项。 例如:

Using docker-compose

使用docker-compose

The logging driver and options can also be configured using docker-compose. For example:

还可以使用docker-compose配置日志记录驱动程序和选项。 例如:

Verify if the setup is working.

验证安装程序是否正常运行。

摘要 (Summary)

Although the default settings work fine, you never know when the container logs take up all the disk space. This can be avoided by the few steps discussed above. Other than that, logs are an important asset. They are not only useful when something goes wrong, but they also contain a lot of hidden value. So never let the logs go.

尽管默认设置可以正常工作,但您永远不知道何时容器日志会占用所有磁盘空间。 这可以通过上面讨论的几个步骤来避免。 除此之外,日志是重要的资产。 它们不仅在出现问题时有用,而且还包含许多隐藏的价值。 因此,永远不要放弃日志。

If you are looking for a log management SAAS solution, consider using Boatswain. We will help you manage all the logs and monitor your Docker servers. ?

如果您正在寻找日志管理SAAS解决方案 ,请考虑使用Boatswain 。 我们将帮助您管理所有日志并监视Docker服务器。 ?

— Originally posted on Boatswain Blog.

—最初发布在Boatswain博客上

翻译自: https://www.freecodecamp.org/news/how-to-setup-log-rotation-for-a-docker-container-a508093912b2/

docker 容器 日志

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值