在开发基于Docker的应用程序时,才能在日志中查找特定信息并将此数据保存到文件中可以推动故障排除和调试过程。以下是使用日志选项,tail和grep在docker容器的日志数据中查找所需内容的一些提示。
关于开始使用Docker的贴子
菜鸟dockercli指令和dockerrun十个选项和其他docker贴子
显示所有日志
在启动Docker容器(比如with)时docker-composeup,它将手动显示日志。假如你在后台运行它们,比如使用docker-composeup-d或从不同的终端运行它们,则可以使用以下方法显示日志:
然而,这将为你提供大量信息。
跟踪容器日志
使用docker-compose,你可以指定要使用的容器日志(在坐落docker-compose文件的当前目录执行):
调试特定应用程序时,一个有用的选项是持续实时查看日志输出。这意味着你可以启动容器,测试功能并查看在使用时发送到日志的内容。
另一种方式是测试你的应用程序,之后在日志中搜索特定信息,以向你显示它的工作情况(或不是!!!)。有两个基于Unix命令的命令可用于此目的。
使用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选项。
[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,--taildocker官网也有说明
另外,可以与日志一起使用的另一个Bash命令是grep返回包含指定字符串的行。诸如:
这将显示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 log,比如你晓得存在问题的时间docker loglinux虚拟主机,则可以告诉docker使用时间戳显示时间戳
[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选项(仅适用于dockerlogs,不是docker-composelogs):
比如,假如我想在上面的示例中见到日志接近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
组合命令
你可以将这种选项和命令组合在一起,以使用你须要的信息来定位日志的特定区域。在下边的示例中,我们将-ttimestamps选项与--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
将日志写入文件
如今你已把握了dockerlogs命令以及怎样确切找到所需内容,请使用此命令将数据发送到日志文件。使用Bash或取代shell(如Zsh),>>命令后跟文件名输出并将数据保存到该文件。
docker-compose logs --tail all nginx_repo|grep INFO >> ./nginx_repo.log
你可能希望使用它来为特定日志数据创建日志文件。比如linux多线程编程,在调试时,你可以创建警告或错误。
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
这意味着你可以使用与文本文件一起使用的所有其他应用程序和命令,并将它们应用于此日志数据。
为何不尝试一些自己的自定义日志命令并将输出保存到自己的日志文件中?