Docker container log viewing and cleaning method of pro test effective

  • 2021-01-02 22:05:31
  • OfStack

Problem 1.

docker container logs cause host disk space to fill up. There's a lot of noise, it takes up a lot of space, so you can clean up the logs if you don't use them.

2. Solutions

2.1 Find the Docker container log

On linux, 1 container log in/var/lib/docker/containers/container_id/below, with json. log at the end of the file operations (log) is a large, check the log file size script docker_log_size. sh, content is as follows:


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

# ./docker_log_size.sh

2.2 Cleaning of Docker container logs (symptoms)

If the docker container is running, after deleting the log using ES41en-ES42en, you will find that disk space is not freed through ES43en-ES44en. The reason is that in Linux or Unix systems, deleting files via ES47en-ES48en or file manager will unlink from the directory structure of the file system (unlink). If the file is open (there is a process in use), the process will still be able to read the file, and disk space will be occupied by 1. The correct posture is cat /dev/null > * -ES54en. log. Of course, you can also delete rm-ES57en and restart docker. Next, provide a log cleanup script, clean_ES60en_log.sh, which reads as follows:


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

However, after cleaning up like this, over time, container logs will come back like weeds.

2.3 Set the Docker container log size (root cure)

Sets the maximum log size for 1 container service

With the above methods, log files will come back sooner or later. To get to the root of the problem, you need to limit the maximum log size of container services. This is achieved by configuring the ES76en-ES77en option for the container ES74en-ES75en


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

After restarting the nginx container, its log file size is limited to 5GB and you don't have to worry about it anymore.

Global Settings

New/etc/docker/daemon json, if there is need not new. Add es89EN-ES90en and ES91en-opts parameters, as shown below:


# vim /etc/docker/daemon.json

{
 "registry-mirrors": ["http://f613ce8f.m.daocloud.io"],
 "log-driver":"json-file",
 "log-opts": {"max-size":"500m", "max-file":"3"}
}

max-size =500m, which means that the maximum size of a container log is 500M,

max-file =3, which means that each container has three logs, namely id+.json, id+ 1.ES108en and id+ 2.json.


//  restart docker daemon 

# systemctl daemon-reload

# systemctl restart docker

Note: The log size set is only valid for the newly created container.


Related articles: