How to view and clean up Docker container logs (tested and effective)

How to view and clean up Docker container logs (tested and effective)

1. Problem

The docker container logs caused the host disk space to be full. docker logs -f container_name has a lot of crackling logs, which takes up a lot of space. Unused logs can be cleaned up.

2. Solution

2.1 Find Docker container logs

On Linux, container logs are generally stored in /var/lib/docker/containers/container_id/. Files ending with json.log (business logs) are very large. The script docker_log_size.sh to check the size of each log file 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 up Docker container logs (treating the symptoms)

If the Docker container is running, after deleting the log using rm -rf, you will find that the disk space is not released through df -h. The reason is that in Linux or Unix systems, deleting files through rm -rf or the file manager will unlink them from the directory structure of the file system. If the file is open (in use by a process), the process will still be able to read the file and the disk space will continue to be occupied. The correct approach is cat /dev/null > *-json.log. Of course, you can also delete it with rm -rf and restart docker. Next, provide a log cleaning script clean_docker_log.sh, the content is 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 such cleanup, container logs will come back like weeds over time.

2.3 Setting the Docker container log size (root cause)

  • Set the upper limit of the log size for a container service

With the above method, the log file will increase in size sooner or later. To fundamentally solve the problem, you need to limit the upper limit of the 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: "5g" 

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

  • Global Settings

Create a new /etc/docker/daemon.json. If it already exists, you don’t need to create a new one. Add the log-dirver and log-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 means that the upper limit of a container log size is 500M.

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

// Restart the Docker daemon# systemctl daemon-reload

# systemctl restart docker

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

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Docker cleaning killer/Docker overlay file takes up too much disk space
  • How to clean up junk files generated by Docker
  • How to quickly clean up docker resources
  • How to analyze and clean up Docker disk space usage
  • Docker cleanup command collection
  • Common methods and problems of Docker cleaning

<<:  Example of using store in vue3 to record scroll position

>>:  MySql uses skip-name-resolve to solve the problem of slow external network connection client

Recommend

Vue implements the shake function (compatible with ios13.3 and above)

Recently, I made a function similar to shake, usi...

JavaScript implements three common web effects (offset, client, scroll series)

Table of contents 1. Element offset series 2. Ele...

Three steps to solve the IE address bar ICON display problem

<br />This web page production skills tutori...

How to communicate between WIN10 system and Docker internal container IP

1. After installing the Windows version of Docker...

JavaScript ES new feature block scope

Table of contents 1. What is block scope? 2. Why ...

Detailed explanation of importing/exporting MySQL data in Docker container

Preface We all know that the import and export of...

WeChat Mini Programs Implement Star Rating

This article shares the specific code for WeChat ...

Mysql database design three paradigm examples analysis

Three Paradigms 1NF: Fields are inseparable; 2NF:...

3 common errors in reading MySQL Binlog logs

1. mysqlbinlog: [ERROR] unknown variable 'def...

HTML table cross-row and cross-column operations (rowspan, colspan)

Generally, the colspan attribute of the <td>...

How to enable remote access in Docker

Docker daemon socket The Docker daemon can listen...

CSS Summary Notes: Examples of Transformations, Transitions, and Animations

1. Transition Transition property usage: transiti...

vue-pdf realizes online file preview

This article example shares the specific code of ...

Detailed explanation of Linux environment variable configuration strategy

When customizing the installation of software, yo...