linux:有效使用docker logs查看日志


在開發基於Docker的應用程序時,能夠在日志中查找特定信息並將此數據保存到文件中可以加快故障排除和調試過程。以下是使用日志選項,tail和grep在docker容器的日志數據中查找所需內容的一些提示。

關於開始使用Docker的帖子

新手docker cli指令docker run十個選項其他docker帖子

I. 顯示所有日志

在啟動Docker容器(例如with)時docker-compose up,它將自動顯示日志。如果你在后台運行它們,例如使用docker-compose up -d或從不同的終端運行它們,則可以使用以下方式顯示日志:

但是,這將為你提供大量信息。

II. 跟蹤容器日志

使用docker-compose,你可以指定要使用的容器日志(在位於docker-compose文件的當前目錄執行):

  • docker-compose logs [options] [SERVICE...]

調試特定應用程序時,一個有用的選項是持續實時查看日志輸出。這意味着你可以啟動容器,測試功能並查看在使用時發送到日志的內容。

  • --follow , -f

另一種方法是測試你的應用程序,然后在日志中搜索特定信息,以向你顯示它的工作情況(或不是!!!)。有兩個基於Unix命令的命令可用於此目的。

III. 使用tail和grep切片和搜索日志

tail命令輸出n文件末尾的最后一行數。例如:

[root@LinuxEA-172_25_50_250 /data/mirrors]# tail -n5 docker-compose.yaml 
      - FTPPASSWD=123
      - FTPDATA=/data/wwwroot
      - SERVER_NAME=meftp.ds.com
      - NGINX_PORT=80
      - WELCOME="welome to www.linuxea.com"

要查看docker日志中的最新輸出,你可以直接在日志文件中使用它,也可以使用docker --tail選項。

  • --tail 從日志末尾顯示的行數
[root@LinuxEA-172_25_50_250 /data/mirrors]# docker-compose logs --tail 5 nginx_repo
Attaching to nginx_repo
nginx_repo    | 2019-01-29 17:45:58,689 INFO spawned: 'vsftpd' with pid 26
nginx_repo    | 2019-01-29 17:45:58,738 INFO exited: vsftpd (exit status 0; not expected)
nginx_repo    | 2019-01-29 17:45:59,739 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
nginx_repo    | 2019-01-29 17:45:59,740 INFO success: createrepo entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
nginx_repo    | 2019-01-29 17:45:59,740 INFO gave up: vsftpd entered FATAL state, too many start retries too quickly

這僅僅只是一個示例,其他選項如,-f, -t ,--tail docker官網也有說明

另外,可以與日志一起使用的另一個Bash命令是grep返回包含指定字符串的行。例如:

  • docker-compose logs | grep success

這將顯示docker容器記錄的所有想要的信息。非常有用,可以看到你需要關注開發的重點。

[root@LinuxEA-172_25_50_250 /data/mirrors]# docker-compose logs --tail 5 nginx_repo|grep success
nginx_repo    | 2019-01-29 17:45:59,739 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
nginx_repo    | 2019-01-29 17:45:59,740 INFO success: createrepo entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

按時間記錄

如果你知道要關注的時間段,例如你知道存在問題的時間,則可以告訴docker使用時間戳顯示時間戳

  • --timestamps , -t
[root@LinuxEA-172_25_50_250 /data/mirrors]# docker-compose logs -t nginx_repo
Attaching to nginx_repo
nginx_repo    | 2019-01-29T09:45:57.110408403Z useradd: warning: the home directory already exists.
nginx_repo    | 2019-01-29T09:45:57.110441950Z Not copying any file from skel directory into it.
nginx_repo    | 2019-01-29T09:45:57.136689405Z Changing password for user marksugar.
nginx_repo    | 2019-01-29T09:45:57.136748778Z passwd: all authentication tokens updated successfully.
nginx_repo    | 2019-01-29T09:45:57.593741281Z Saving Primary metadata
nginx_repo    | 2019-01-29T09:45:57.593832853Z Saving file lists metadata
nginx_repo    | 2019-01-29T09:45:57.593854286Z Saving other metadata
nginx_repo    | 2019-01-29T09:45:57.593862151Z Generating sqlite DBs
nginx_repo    | 2019-01-29T09:45:57.593869092Z Sqlite DBs complete
nginx_repo    | 2019-01-29T09:45:57.672214250Z 2019-01-29 17:45:57,671 CRIT Supervisor is running as root.  Privileges were not dropped because no user is specified in the config file.  If you intend to run as root, you can set user=root in the config file to avoid this message.
nginx_repo    | 2019-01-29T09:45:57.679619865Z 2019-01-29 17:45:57,679 INFO RPC interface 'supervisor' initialized
nginx_repo    | 2019-01-29T09:45:57.679661466Z 2019-01-29 17:45:57,679 CRIT Server 'unix_http_server' running without any HTTP authentication checking
nginx_repo    | 2019-01-29T09:45:57.679740900Z 2019-01-29 17:45:57,679 INFO supervisord started with pid 1
nginx_repo    | 2019-01-29T09:45:58.683866045Z 2019-01-29 17:45:58,683 INFO spawned: 'nginx' with pid 24
nginx_repo    | 2019-01-29T09:45:58.687228502Z 2019-01-29 17:45:58,686 INFO spawned: 'createrepo' with pid 25
nginx_repo    | 2019-01-29T09:45:58.690025433Z 2019-01-29 17:45:58,689 INFO spawned: 'vsftpd' with pid 26
nginx_repo    | 2019-01-29T09:45:58.738620050Z 2019-01-29 17:45:58,738 INFO exited: vsftpd (exit status 0; not expected)
nginx_repo    | 2019-01-29T09:45:59.740406128Z 2019-01-29 17:45:59,739 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
nginx_repo    | 2019-01-29T09:45:59.740444435Z 2019-01-29 17:45:59,740 INFO success: createrepo entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
nginx_repo    | 2019-01-29T09:45:59.740540049Z 2019-01-29 17:45:59,740 INFO gave up: vsftpd entered FATAL state, too many start retries too quickly

選擇一個特定的時間段--since--until選項(僅適用於docker logs,不是docker-compose logs):

  • --since從時間戳(例如2013-01-02T13:23:37)或相對(例如42分鍾42米)顯示日志
  • --until在時間戳(例如2013-01-02T13:23:37)或相對之前顯示日志(例如42分鍾42米)

例如,如果我想在前面的示例中看到日志接近info的消息,我將執行:

[root@LinuxEA-172_25_50_250 /data/mirrors]# docker logs -t --since 2019-01-29T09:45:57.679661466Z --until 2019-01-29T09:45:59.740540049Z nginx_repo
2019-01-29T09:45:57.679661466Z 2019-01-29 17:45:57,679 CRIT Server 'unix_http_server' running without any HTTP authentication checking
2019-01-29T09:45:57.679740900Z 2019-01-29 17:45:57,679 INFO supervisord started with pid 1
2019-01-29T09:45:58.683866045Z 2019-01-29 17:45:58,683 INFO spawned: 'nginx' with pid 24
2019-01-29T09:45:58.687228502Z 2019-01-29 17:45:58,686 INFO spawned: 'createrepo' with pid 25
2019-01-29T09:45:58.690025433Z 2019-01-29 17:45:58,689 INFO spawned: 'vsftpd' with pid 26
2019-01-29T09:45:58.738620050Z 2019-01-29 17:45:58,738 INFO exited: vsftpd (exit status 0; not expected)
2019-01-29T09:45:59.740406128Z 2019-01-29 17:45:59,739 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2019-01-29T09:45:59.740444435Z 2019-01-29 17:45:59,740 INFO success: createrepo entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2019-01-29T09:45:59.740540049Z 2019-01-29 17:45:59,740 INFO gave up: vsftpd entered FATAL state, too many start retries too quickly

組合命令

你可以將這些選項和命令組合在一起,以使用你需要的信息來定位日志的特定區域。在下面的示例中,我們將-t timestamps選項與--tail容器日志的最后5行組合nginx_repo,然后在這些行中搜索包含INFO僅查看INFO級別記錄的行的行。

[root@LinuxEA-172_25_50_250 /data/mirrors]# docker-compose logs --tail 5 nginx_repo|grep INFO
nginx_repo    | 2019-01-29 17:45:58,689 INFO spawned: 'vsftpd' with pid 26
nginx_repo    | 2019-01-29 17:45:58,738 INFO exited: vsftpd (exit status 0; not expected)
nginx_repo    | 2019-01-29 17:45:59,739 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
nginx_repo    | 2019-01-29 17:45:59,740 INFO success: createrepo entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
nginx_repo    | 2019-01-29 17:45:59,740 INFO gave up: vsftpd entered FATAL state, too many start retries too quickly

如果要在所有內容中查找,這里可以替換成all

[root@LinuxEA-172_25_50_250 /data/mirrors]# docker-compose logs --tail all nginx_repo|grep INFO
nginx_repo    | 2019-01-29 17:45:57,679 INFO RPC interface 'supervisor' initialized
nginx_repo    | 2019-01-29 17:45:57,679 INFO supervisord started with pid 1
nginx_repo    | 2019-01-29 17:45:58,683 INFO spawned: 'nginx' with pid 24
nginx_repo    | 2019-01-29 17:45:58,686 INFO spawned: 'createrepo' with pid 25
nginx_repo    | 2019-01-29 17:45:58,689 INFO spawned: 'vsftpd' with pid 26
nginx_repo    | 2019-01-29 17:45:58,738 INFO exited: vsftpd (exit status 0; not expected)
nginx_repo    | 2019-01-29 17:45:59,739 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
nginx_repo    | 2019-01-29 17:45:59,740 INFO success: createrepo entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
nginx_repo    | 2019-01-29 17:45:59,740 INFO gave up: vsftpd entered FATAL state, too many start retries too quickly

將日志寫入文件

現在你已掌握了docker logs命令以及如何准確找到所需內容,請使用此命令將數據發送到日志文件。使用Bash或替代shell(如Zsh)>>命令后跟文件名輸出並將數據保存到該文件。

docker-compose logs --tail all nginx_repo|grep INFO >> ./nginx_repo.log

你可能希望使用它來為特定日志數據創建日志文件。例如,在調試時,你可以創建警告錯誤

docker-compose logs --tail all nginx_repo| grep warning >> logs_warnings.log

現在我的nginx_repo.log文件內容包含:

nginx_repo    | 2019-01-29 17:45:57,679 INFO RPC interface 'supervisor' initialized
nginx_repo    | 2019-01-29 17:45:57,679 INFO supervisord started with pid 1
nginx_repo    | 2019-01-29 17:45:58,683 INFO spawned: 'nginx' with pid 24
nginx_repo    | 2019-01-29 17:45:58,686 INFO spawned: 'createrepo' with pid 25
nginx_repo    | 2019-01-29 17:45:58,689 INFO spawned: 'vsftpd' with pid 26
nginx_repo    | 2019-01-29 17:45:58,738 INFO exited: vsftpd (exit status 0; not expected)
nginx_repo    | 2019-01-29 17:45:59,739 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
nginx_repo    | 2019-01-29 17:45:59,740 INFO success: createrepo entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
nginx_repo    | 2019-01-29 17:45:59,740 INFO gave up: vsftpd entered FATAL state, too many start retries too quickly

這意味着你可以使用與文本文件一起使用的所有其他應用程序和命令,並將它們應用於此日志數據。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM