1. Background

Using docker logs to view too many container logs is very inconvenient. It is troublesome to clean up manually every time. I wrote a shell script for batch cleaning.

2. Treatment method

The logs of the docker container are all stored in the /data/docker/lib/docker/containers/ directory, we can delete the corresponding log, here you can create a new shell script

vim cleanDockerLog.sh

Put the following content in

#!/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 ========"

Three, perform cleanup

The command to perform the cleanup is as follows

sh ./cleanDockerLog.sh

Check the docker logs -f container ID again, you will find that the log has been completed by the situation


Author: Tang Qingsong

Date: November 12, 2021

WeChat: songboy8888


汤青松
5.2k 声望8.3k 粉丝

《PHP Web安全开发实战》 作者