Resolving a docker container logs causing the host to the disk space is full of

Log file / var / lib / docker / containers / <docker_container_id> / directory


View log size

vim /opt/docker_log_size.sh

#!/bin/sh

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 /opt/docker_log_size.sh

/opt/docker_log_size.sh


Delete Logs

Find a large space above the log through the script, copy the log path
cleared logs
cat / dev / null> <docker_container_id > -json.log


Clear all script logs

vim /opt/clean_docker_log.sh

#!/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 /opt/clean_docker_log.sh

/opt/clean_docker_log.sh


The above-described method, the log file will sooner or later come back up.
The following solve the problem fundamentally.


Log size Docker containers provided

Set a maximum size of log container services

For example with nginx

nginx: 
  image: nginx:latest
  restart: always 
  logging: 
    driver: "json-file" 
    options: 
      max-size: "5g"

After the restart nginx container, the size of its log files is restricted to 5GB


Global Settings

New /etc/docker/daemon.json, if it would not have built. Adding log-dirver and log-opts parameters, the following examples:
Vim /etc/docker/daemon.json

{
        "registry-mirrors": ["https://registry.docker-cn.com"],
        "live-restore": true,
        "log-driver":"json-file",
        "log-opts": {"max-size":"500m", "max-file":"1"}
}

max-size = 500m, it means a container log maximum size is 500M, 

max-file = 3, mean a container having three logs are id + .json, id + 1.json, id + 2.json.

// restart docker daemon
systemctl-daemon reload
systemctl restart docker

 

 

Reference: https: //my.oschina.net/u/2289161/blog/1931390

Guess you like

Origin www.cnblogs.com/wintersoft/p/11941213.html