How to clear docker logs

王林
Release: 2020-05-21 10:39:30
Original
5023 people have browsed it

How to clear docker logs

1. Find the Docker container log

On Linux, container logs are generally stored under /var/lib/docker/containers/container_id/. The file ending with json.log (business log) is very large. Check the script docker_log_size.sh for the size of each log file. The 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
Copy after login

Execute the following command:

# chmod +x docker_log_size.sh
# ./docker_log_size.sh
Copy after login

2 , Clean up the Docker container log

If the docker container is running, after using the rm -rf method to delete the log, you will find that the disk space has not been released through the df -h command.

The reason is that in Linux or Unix systems, deleting files through rm -rf or the file manager will unlink (unlink) from the directory structure of the file system. If the file is open (being used by a process), the process will still be able to read the file, and disk space will remain occupied.

The correct method iscat /dev/null > *-json.log, of course you can also delete it through rm -rf and restart docker. Next, provide a log cleaning script clean_docker_log.sh with the following content:

#!/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 ========
Copy after login

Then execute the following command:

# chmod +x clean_docker_log.sh
# ./clean_docker_log.sh
Copy after login

Recommended tutorial: docker tutorial

The above is the detailed content of How to clear docker logs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template