Manually clear docker container log information under Linux

Operation of the server with caution, the following content is reproduced from "Docker checks the container size and clears the logs"

View container size

# 简略
docker system df
# 详细信息
docker system df -v

Insert image description here
Insert image description here

Clear log files (treating the symptoms)

Find the log file: On Linux, container logs are generally stored under /var/lib/docker/containers/container_id/

# 命令行清除
cd /var/lib/docker/containers/
du -sh *
cd 94de1859951a96aee05bfdf8f6eb3a721546bf13aaf5f0785ba537c0eccb702b
cat /dev/null > 94de1859951a96aee05bfdf8f6eb3a721546bf13aaf5f0785ba537c0eccb702b-json.log

Clean using script

cat clean-docker-log.sh
#!/bin/bash
echo "======== docker containers logs file size ========"  

logs=$(find /var/lib/docker/containers/ -name *-json.log)  

for log in $logs  
        do  
             ls -lh $log   
        done  

# 授权并运行脚本
chmod +x docker_log_size.sh

./clean-docker-log.sh

Set the docker container log size (the root cause)

Limit the maximum log size of the container service. This is achieved by configuring the max-size option of the container docker-compose

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

Global settings: Create or modify /etc/docker/daemon.json and add log-dirver and log-opts parameters

{
    
    
        "registry-mirrors": [
                "https://docker.mirrors.ustc.edu.cn","http://hub-mirror.c.163.com"
                ],
        "insecure-registries":["docker.mirrors.ustc.edu.cn"],
        # 设置日志大小
        "log-driver":"json-file",
        "log-opts":{
    
    "max-size":"500m","max-file":"3"}
}
# 加载配置并重启
systemctl daemon-reload 
systemctl restart docker

max-size=500m, which means that the upper limit of the log size of a container is 500
max-file=3, which means that a container has three logs, namely id+.json, id +1.json、id+2.json

Transfer:https://www.cnblogs.com/Clera-tea/p/16425712.html

Guess you like

Origin blog.csdn.net/weixin_46099269/article/details/134452440