20190208

在开发基于Docker的应用程序时,能够在日志中查找特定信息并将此数据保存到文件中可以加快故障排除和调试过程。以下是使用日志选项,tail和grep在docker容器的日志数据中查找所需内容的一些提示。

关于开始使用Docker的帖子

新手docker cli指令docker run十个选项其他docker帖子

显示所有日志#

在启动Docker容器(例如with)时docker-compose up,它将自动显示日志。如果你在后台运行它们,例如使用docker-compose up -d或从不同的终端运行它们,则可以使用以下方式显示日志:

但是,这将为你提供大量信息。

跟踪容器日志#

使用docker-compose,你可以指定要使用的容器日志(在位于docker-compose文件的当前目录执行):

调试特定应用程序时,一个有用的选项是持续实时查看日志输出。这意味着你可以启动容器,测试功能并查看在使用时发送到日志的内容。

另一种方法是测试你的应用程序,然后在日志中搜索特定信息,以向你显示它的工作情况(或不是!!!)。有两个基于Unix命令的命令可用于此目的。

使用tail和grep切片和搜索日志#

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

[marksugar@www.linuxea.com /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选项。

[marksugar@www.linuxea.com /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容器记录的所有想要的信息。非常有用,可以看到你需要关注开发的重点。

[marksugar@www.linuxea.com /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使用时间戳显示时间戳

[marksugar@www.linuxea.com /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):

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

[marksugar@www.linuxea.com /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级别记录的行的行。

[marksugar@www.linuxea.com /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

[marksugar@www.linuxea.com /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

这意味着你可以使用与文本文件一起使用的所有其他应用程序和命令,并将它们应用于此日志数据。

为什么不尝试一些自己的自定义日志命令并将输出保存到自己的日志文件中?

了解更多#

使用有针对性的日志数据可以更轻松地开发和调试docker应用程序。例如,你可以在一段时间内搜索特定类型的错误,然后可以将其保存到错误日志文件中。你或许还需要了解:

学习更多#

学习如何使用Docker CLI命令,Dockerfile命令,使用Bash命令可以帮助你更有效地使用Docker应用程序。查看Docker文档和我的其他帖子以了解更多信息。