How to view and clean up Docker container logs

The third president of the United States (Thomas Jefferson) once said: "Don’t believe or deny something because other people believe or deny it. God has given you a mind for judging truth and error. . Then you can use it!".

On days without running with you, work hard!

Find

    Normally use docker to deploy the test environment program, but it has not been deployed. Finally, it was found that the docker container was running too many logs, which caused the system storage to be full and the service could not be accessed normally. (The highest log level debugger is enabled in the service).

    If you encounter a problem, then find a way to solve it, Baidu has it, and finally found a solution. Make a note here.

Log classification

Logs are divided into two categories, one is Docker engine logs; the other is container logs.

  • Docker engine log
    Docker engine log is generally handed over to Upstart (Ubuntu 14.04) or systemd (CentOS 7, Ubuntu 16.04). The former is generally located under /var/log/upstart/docker.log, and the latter is generally read by jounarlctl -u docker. The location of different systems is different, refer to the following:
system Log location
Ubuntu(14.04) /var/log/upstart/docker.log
Ubuntu(16.04) journalctl -u docker.service
CentOS 7/RHEL 7/Fedora journalctl -u docker.service
CoreOS journalctl -u docker.service
OpenSuSE journalctl -u docker.service
OSX ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/log/d‌​ocker.log
Debian GNU/Linux 7 /var/log/daemon.log
Debian GNU/Linux 8 journalctl -u docker.service
Boot2Docker /var/log/docker.log
  • Docker container log The log of
    each container will be stored in json-file format under /var/lib/docker/containers/<container id>/<container id>-json.log by default.
  1. Find log files for all containers
find /var/lib/docker/containers -name *.log
  1. View log location
docker inspect --format='{
    
    {.LogPath}}' <container_name>
  1. Real-time query content
tail -f `docker inspect --format='{
    
    {.LogPath}}' <container_name>`
  • Write scripts to view and clean up logs
         Now that we know how to view the storage locations of logs and files generated by docker, let's start writing scripts. The reference is as follows:
  1. View container log size — logs_file_size.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
  1. Clean up logs — clean_containers_logs.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 ========"

cat /dev/null> log means log means willL O G table shows the log file contents emptied

Two shell scripts can be run through sh logs_file_size.sh and sh clean_containers_logs.sh.

Reference article:
https://wxnacy.com/2018/06/14/docker-log/
https://www.cnblogs.com/YatHo/p/7866029.html
https://www.choupangxia.com/2019/ 09/15/linux-centos-var-lib-docker-container

Guess you like

Origin blog.csdn.net/qq_37640410/article/details/108167287