How to Clear Logs of Running Docker Containers

发布时间 2023-06-12 10:36:01作者: lightsong

How to Clear Logs of Running Docker Containers

 https://www.howtogeek.com/devops/how-to-clear-logs-of-running-docker-containers/

Understanding the Problem

Docker collects logs from the standard output and error streams of container foreground processes. The docker logs command is used to retrieve these logs but it doesn’t let you delete old lines.

Docker supports many different logging drivers so it’s not possible to offer a standardized clean-up mechanism. This article focuses on the standard json-file logging driver, where log lines are stored in a JSON file on your container host’s filesystem. Refer to your storage driver’s documentation if you need to clean up logs streamed to a remote host.

 

Log files created by the json-file driver are stored under the /var/lib/docker/containers directory. Each container gets its own log file which is used throughout its lifetime. There’s no log rotation configured by default.

 

Simply deleting the log file isn’t an effective solution. Docker expects the file to be available continually and won’t recreate it automatically if it’s removed. It’s safer to clear the existing log’s contents to avoid affecting any current writes.

docker inspect --format='{{.LogPath}}' my-app

sudo sh -c 'echo "" > $(docker inspect --format="{{.LogPath}}" my-app)'

 

对于所有容器进行清理

https://blog.csdn.net/yjk13703623757/article/details/80283729

写脚本批量处理

#!/bin/sh 
  
echo "======== start clean docker containers logs ========"  
  
logs=$(find /var/lib/docker/containers/ -name *-json.log)  
  
for log in $logs  
        do  
                echo "clean logs : $log"  
                cat /dev/null > $log  
        done  

echo "======== end clean docker containers logs ========"  

 

# chmod +x clean_docker_log.sh

# ./clean_docker_log.sh

 

每天定时清理

使用cron工具

https://zhuanlan.zhihu.com/p/58719487

$ crontab -e      // 打开crontab任务编辑
* * * * * date >> /tmp/time.txt

 

配置log rotate

https://www.howtogeek.com/devops/how-to-clear-logs-of-running-docker-containers/

Setting Up Log Rotation

Regularly having to clean up log files in this manner usually signals that your application’s logs are either excessively verbose or there’s simply too much activity to store in one file.

Many Docker logging drivers, including json-file, have optional log rotation support that you can enable globally for the Docker daemon or on a per-container basis.

 

Daemon settings are configured in /etc/docker/daemon.json. Here’s an example that rotates container logs once they reach 8MB. Up to five files will be retained at any time, with old ones automatically deleted as new rotations occur.

 
{
    "log-opts": {
        "max-size": "8m",
        "max-file": "5"
    }
}

Restart the Docker daemon to apply the change:

$ sudo service docker restart

Daemon-level rotation applies to all newly created containers. Changes won’t affect any containers that already exist on your host.

 

Rotation can be configured for individual containers using --log-opts flags. These will override your default Docker daemon settings.

 
docker run --name app \
    --log-driver json-file \
    --log-opts max-size=8M \
    --log-opts max-file=5 \
    app-image:latest

 

https://docs.docker.com/config/containers/logging/json-file/

重启只对新建的容器有限制作用, 对已经建立的容器无影响。

Restart Docker for the changes to take effect for newly created containers. Existing containers do not use the new logging configuration.

 

https://blog.csdn.net/yjk13703623757/article/details/80283729

在docker-compose文件中声明log-opts属性

nginx:
  image: nginx:1.12.1
  restart: always
  logging:
    driver: “json-file”
    options:
      max-size: “5g”