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

查看:1339
本文介绍了如何正确清除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?

推荐答案

来自这个问题您可以运行:

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

或者有类似的truncate命令:

or there's the similar truncate command:

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


我都不喜欢这两个,因为它们直接修改Docker的文件.当docker将json格式的数据写入文件时,可能会发生外部日志删除,导致部分行并破坏了从docker logs cli读取任何日志的能力.


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.

相反,您可以让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而是使用更快和更小的二进制语法来存储在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.

我在 查看全文

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