Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Docker container log cleanup

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

preface

Recently, I found that the Gitlab server disk is full. After investigation, I found that docker container logs occupy dozens of G capacity. How can I view and clean these logs?

The main knowledge points mentioned in this section are as follows:

(1) Docker container log path

(2) How to clean Docker container logs

(3) How to fundamentally solve the Docker container log footprint problem Docker container log path

On Linux, container logs are typically stored in files ending in json.log (business logs) under/var/lib/docker/containers/container_id/. As follows:

How to clean Docker container logs

Use the command:

cat /dev/null > *-json.log

Of course you can also delete logs using rm -rf. But for a running docker container, if you execute rm -rf, df -h will show that disk space is not freed.

The reason is that on Linux or Unix systems, deleting files via rm-rf or file managers unlinks from the directory structure of the file system. If the file is open (and a process is in use), the process will still be able to read the file and disk space will always be occupied.

Of course, you can also restart docker after deleting it via rm -rf.

The above two ways to clear docker logs only temporarily free up disk space, but after this cleaning, with the passage of time, container logs will accumulate a lot one day. Below we will fundamentally solve this problem ~

How to fundamentally solve the Docker container log footprint problem (1) Method 1: Set the log size limit of a Kubernetes Engine

One way to get to the root of the problem is to cap the log size of the Kubernetes Engine. This is achieved by configuring the container docker-compose max-size option as follows:

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, so there is no need to worry anymore.

(2) Method 2: Global setting

Create/etc/docker/daemon.json. If there is one, don't create it. Add log-dirver and log-opts parameters, such as:

# vim /etc/docker/daemon.json{ "log-driver":"json-file", "log-opts": {"max-size":"500m", "max-file":"3"}}

Description $>:

Set log size, valid only for newly created containers.

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

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

//restart docker daemon # systemctl daemon-reload# systemctl restart docker reference Docker container log view and cleanup

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report