如何清除Docker容器日志? [英] How to clean Docker container logs?

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

问题描述

我使用

docker logs -f <container_name>

我通过调用<$ c $来将大量数据记录到node.js应用程序的日志中c> console.log()。我需要清理日志,因为它太长了, docker logs 命令首先运行日志的现有行,然后结束。我该如何清洁使其再次变短?我想看到这样的命令:

I log lots of data to the log in my node.js app via calls to console.log(). I need to clean the log, because it's gotten too long and the docker logs command first runs through the existing lines of the log before getting to the end. How do I clean it to make it short again? I'd like to see a command like:

docker logs clean <container_name>

但它似乎不存在。

推荐答案

首先,如果只需要查看较少的输出,则可以让docker只向您显示最近的行:

First, if you just need to see less output, you can have docker only show you the more recent lines:

docker logs --since 30s -f <container_name_or_id>

或者您可以设置多行限制:

Or you can put a number of lines to limit:

docker logs --tail 20 -f <container_name_or_id>






要删除Linux版Docker上的日志,您可以对单个容器运行以下命令:


To delete the logs on a Docker for Linux install, you can run the following for a single container:

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

请注意,这需要root用户,我不建议这样做。如果在Docker中间将文件写入相同文件的日志中使该文件为空,则可能会损坏该日志文件。相反,您应该配置docker旋转日志。

Note that this requires root, and I do not recommend this. You could potentially corrupt the logfile if you null the file in the middle of docker writing a log to the same file. Instead you should configure docker to rotate the logs.

最后,您可以配置docker 以自动轮换 / etc / docker / daemon中的日志。 json 文件:

Lastly, you can configure docker to automatically rotate logs with the following in an /etc/docker/daemon.json file:

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

这允许Docker每个容器最多保留3个日志文件,每个文件限制为10兆(因此,每个容器限制为20到30兆) 。您将需要运行 systemctl reload docker 来应用这些更改。这些更改是任何新创建的容器的默认设置,它们不适用于已创建的容器。您需要删除并重新创建任何现有容器以应用这些设置。

That allows docker to keep up to 3 log files per container, with each file limited to 10 megs (so a limit between 20 and 30 megs of logs per container). You will need to run a systemctl reload docker to apply those changes. And these changes are the defaults for any newly created container, they do not apply to already created containers. You will need to remove and recreate any existing containers to have these settings apply.

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

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