简体   繁体   English

如何清理Docker容器日志?

[英]How to clean Docker container logs?

I read my Docker container log output using我阅读了我的 Docker 容器日志 output 使用

docker logs -f <container_name>

I log lots of data to the log in my node.js app via calls to console.log() .我通过调用console.log()将大量数据记录到我的 node.js 应用程序的日志中。 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.我需要清理日志,因为它变得太长了, docker logs命令在结束之前首先运行日志的现有行。 How do I clean it to make it short again?我如何清洁它以使其再次变短? I'd like to see a command like:我希望看到这样的命令:

docker logs clean <container_name>

But it doesn't seem to exist.但它似乎不存在。

First, if you just need to see less output, you can have docker only show you the more recent lines:首先,如果你只需要看到更少的输出,你可以让 docker 只显示最近的几行:

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>

To delete the logs on a Docker for Linux install, you can run the following for a single container:要删除 Docker for Linux 安装上的日志,您可以为单个容器运行以下命令:

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

Note that this requires root, and I do not recommend this.请注意,这需要root,我不推荐这样做。 You could potentially corrupt the logfile if you null the file in the middle of docker writing a log to the same file.如果在 docker 将日志写入同一文件的过程中将文件置空,则可能会损坏日志文件。 Instead you should configure docker to rotate the logs.相反,您应该配置 docker 来轮换日志。


Lastly, you can configure docker to automatically rotate logs with the following in an /etc/docker/daemon.json file:最后,您可以/etc/docker/daemon.json配置为使用/etc/docker/daemon.json文件中的以下内容自动轮换日志:

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

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).这允许 docker 每个容器最多保留 3 个日志文件,每个文件限制为 10 兆(因此每个容器的日志限制在 20 到 30 兆之间)。 You will need to run a systemctl reload docker to apply those changes.您需要运行systemctl reload docker来应用这些更改。 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.您需要删除并重新创建任何现有容器才能应用这些设置。

The best script I found is我发现的最好的脚本是

sudo sh -c 'truncate -s 0 /var/lib/docker/containers/*/*-json.log'

It cleans all logs and you don't need to stop the containers.它会清除所有日志,您无需停止容器。

Credit goes to https://bytefreaks.net/applications/docker/horrible-solution-how-to-delete-all-docker-logs归功于https://bytefreaks.net/applications/docker/horrible-solution-how-to-delete-all-docker-logs

This is not the ideal solution, but until Docker builds in a command to do it, this is a good workaround.这不是理想的解决方案,但在 Docker 构建命令来执行此操作之前,这是一个很好的解决方法。

Create a script file docker-clean-logs.sh with this content:使用以下内容创建脚本文件docker-clean-logs.sh

#!/bin/bash

rm $(docker inspect $1 | grep -G '"LogPath": "*"' | sed -e 's/.*"LogPath": "//g' | sed -e 's/",//g');

Grant the execute permission to it:授予它执行权限:

chmod +x ./docker-clean-logs.sh

Stop the Docker container that you want to clean:停止要清理的 Docker 容器:

docker stop <container_name>

Then run the above script:然后运行上面的脚本:

./docker-clean-logs.sh <container_name>

And finally run your container again:最后再次运行你的容器:

docker start ...

Credit goes to the user sgarbesi on this page: https://github.com/docker/compose/issues/1083归功于此页面上的用户 sgarbesi: https : //github.com/docker/compose/issues/1083

If you want to remove all log files, not only for a specific container's log, you can use:如果要删除所有日志文件,不仅是针对特定容器的日志,还可以使用:

docker system prune

But, note that this does not clear logs for running containers.但是,请注意,这不会清除运行容器的日志。

You can use logrotate as explained in this article您可以按照本文中的说明使用 logrotate

https://sandro-keil.de/blog/2015/03/11/logrotate-for-docker-container/ https://sandro-keil.de/blog/2015/03/11/logrotate-for-docker-container/

This needs to be done before launching the container.这需要在启动容器之前完成。

Solution for a docker swarm service: docker swarm 服务的解决方案:

   logging:
     options:
       max-size: "10m"
       max-file: "10"

Run:跑:

 docker inspect {containerId}

Copy LogPath value复制日志路径值

truncate -s 0 {LogaPath}

In order to do this on OSX, you need to get to the virtual machine the Docker containers are running in.为了在 OSX 上执行此操作,您需要访问运行 Docker 容器的虚拟机。

You can use the walkerlee/nsenter image to run commands inside the VM like so:您可以使用walkerlee/nsenter映像在 VM 内运行命令,如下所示:

docker run --rm -it --privileged --pid=host walkerlee/nsenter -t 1 -m -u -i -n sh

Combining that with a simplified version of the accepted answer you get:将其与已接受答案的简化版本相结合,您将得到:

#!/bin/sh
docker run --rm -it --privileged --pid=host walkerlee/nsenter -t 1 -m -u -i -n \
    cp /dev/null $(docker inspect -f '{{.LogPath}}' $1)

Save it, chmod +x it, run it.保存它, chmod +x它,运行它。

As far as I can tell this doesn't require the container to be stopped.据我所知,这不需要停止容器。 Also, it clears out the log file (instead of deleting it) avoiding errors when doing docker logs right after cleanup.此外,它会清除日志文件(而不是删除它),避免在清理后立即执行docker logs时出错。

On Windows 10 none of the solutions worked for me, I kept getting 'No such file or directory'在 Windows 10 上,没有一个解决方案对我有用,我一直收到“没有这样的文件或目录”

This worked这有效

  • Get container ID (inspect the container)获取容器 ID(检查容器)
  • In file explorer open docker-desktop-data (in WSL)在文件资源管理器中打开docker-desktop-data (在 WSL 中)
  • Navigate to version-pack-data\community\docker\containers\CONTAINER_ID导航到version-pack-data\community\docker\containers\CONTAINER_ID
  • Stop the container停止容器
  • Open the file CONTAINER_ID-json.log file and trim it or just create a blank file with same name打开文件CONTAINER_ID-json.log文件并修剪它或只创建一个具有相同名称的空白文件

在此处输入图像描述

source来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM