Docker:如何正确清除 Docker 容器的日志? [英] Docker: How to clear the logs properly for a Docker container?

查看:171
本文介绍了Docker:如何正确清除 Docker 容器的日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 docker logs [container-name] 查看特定容器的日志.

I use docker logs [container-name] to see the logs of a specific container.

有没有一种优雅的方法来清除这些日志?

Is there an elegant way to clear these logs?

推荐答案

首先是错误的答案.从这个问题,您可以运行一个单线:

First the bad answer. From this question there's a one-liner that you can run:

echo "" > $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)

不是回声,而是更简单的:

instead of echo, there's the simpler:

: > $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)

或者有截断命令:

truncate -s 0 $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)


我不是其中任何一个的忠实粉丝,因为它们直接修改 Docker 的文件.当 docker 将 json 格式的数据写入文件时,可能会发生外部日志删除,导致部分行,并破坏了从 docker logs cli 读取任何日志的能力.有关发生这种情况的示例,请参阅 对duketwo 的回答的评论:


I'm not a big fan of either of those since they modify Docker's files directly. The external log deletion could happen while docker is writing json formatted data to the file, resulting in a partial line, and breaking the ability to read any logs from the docker logs cli. For an example of that happening, see this comment on duketwo's answer:

清空日志文件后,我收到此错误:来自流中守护进程的错误:错误抓取日志:无效字符 'x00' 寻找值的开头

after emptying the logfile, I get this error: error from daemon in stream: Error grabbing logs: invalid character 'x00' looking for beginning of value

相反,您可以让 Docker 自动为您轮换日志.如果您使用默认的 JSON 日志驱动程序:

Instead, you can have Docker automatically rotate the logs for you. This is done with additional flags to dockerd if you are using the default JSON logging driver:

dockerd ... --log-opt max-size=10m --log-opt max-file=3

您也可以将其设置为 daemon.json 文件而不是修改您的启动脚本:

You can also set this as part of your daemon.json file instead of modifying your startup scripts:

{
  "log-driver": "json-file",
  "log-opts": {"max-size": "10m", "max-file": "3"}
}

这些选项需要配置为具有 root 访问权限.确保在更改此文件以应用设置后运行 systemctl reload docker.此设置将成为任何新创建的容器的默认设置.请注意,需要删除并重新创建现有容器才能接收新的日志限制.

These options need to be configured with root access. Make sure to run a systemctl reload docker after changing this file to have the settings applied. This setting will then be the default for any newly created containers. Note, existing containers need to be deleted and recreated to receive the new log limits.

可以将类似的日志选项传递给单个容器以覆盖这些默认值,从而允许您在单个容器上保存更多或更少的日志.从 docker run 这看起来像:

Similar log options can be passed to individual containers to override these defaults, allowing you to save more or fewer logs on individual containers. From docker run this looks like:

docker run --log-driver json-file --log-opt max-size=10m --log-opt max-file=3 ...

或在撰写文件中:

version: '3.7'
services:
  app:
    image: ...
    logging:
      options:
        max-size: "10m"
        max-file: "3"


为了额外节省空间,您可以从 json 日志驱动程序切换到本地"驱动程序.记录驱动程序.它采用相同的 max-size 和 max-file 选项,但不是存储在 json 中,而是使用更快、更小的二进制语法.这允许您在相同大小的文件中存储更多日志.daemon.json 条目如下所示:


For additional space savings, you can switch from the json log driver to the "local" log driver. It takes the same max-size and max-file options, but instead of storing in json it uses a binary syntax that is faster and smaller. This allows you to store more logs in the same sized file. The daemon.json entry for that looks like:

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

本地驱动程序的缺点是依赖于直接访问 json 日志的外部日志解析器/转发器将不再工作.因此,如果您使用 filebeat 之类的工具发送到 Elastic 或 Splunk 的通用转发器,我会避免使用本地"文件.司机.

The downside of the local driver is external log parsers/forwarders that depended on direct access to the json logs will no longer work. So if you use a tool like filebeat to send to Elastic, or Splunk's universal forwarder, I'd avoid the "local" driver.

我在我的 提示和技巧演示.

这篇关于Docker:如何正确清除 Docker 容器的日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆