容器日志全解:Docker 日志输出原理与 logs 命令实战 第8章 容器日志摘要本章介绍了容器日志管理在微服务架构中的重要性。容器因其短暂的生命周期使得集中式日志管理成为生产环境不可或缺的部分。文章重点讲解了 Docker 日志机制容器日志默认输出到 STDOUT/STDERR。通过 httpd 容器实例演示了前台运行直接查看日志、后台运行使用docker attach和docker logs命令查看日志的两种方式并对比了它们的优缺点。最终推荐使用docker logs命令支持-f参数实时跟踪作为查看容器历史与实时日志的标准方法。续上篇博客 Docker 容器监控全攻略原生命令与 cAdvisor 实操详解高效的监控和日志管理对保持生产系统持续稳定地运行以及排查问题至关重要。在微服务架构中由于容器的数量众多以及快速变化的特性使得记录日志和监控变得越来越重要。考虑 到容器短暂和不固定的生命周期当我们需要 debug 问题时有些容器可能已经不存在了。因此一套集 中式的日志管理系统是生产环境中不可或缺的组成部分。Docker logs对于一个运行的容器Docker 会将日志发送到 容器标准输出设备STDOUT和标准错误设备 STDERRSTDOUT 和 STDERR 实际上就是容器的控制台终端。举个例子用下面的命令运行 httpd 容器[rootdocker ~]# docker run -p 80:80 httpdAH00558: httpd: Could not reliably determine the servers fully qualified domain name, using 172.17.0.3. Set the ServerName directive globally to suppress this message AH00558: httpd: Could not reliably determine the servers fully qualified domain name, using172.17.0.3. Set theServerNamedirective globally to suppress this message[Tue Oct 0814:28:10.3128762024][mpm_event:notice][pid1:tid1]AH00489: Apache/2.4.62(Unix)configured -- resuming normal operations[Tue Oct 0814:28:10.3160582024][core:notice][pid1:tid1]AH00094: Command line:httpd -D FOREGROUND我们在启动日志的时候没有用-d参数httpd 容器以前台方式启动日志会直接打印在当前的终端窗 口。如果加上-d参数以后台方式运行容器我们就看不到输出的日志了。[rootdocker ~]# docker run -d -p 80:80 httpda8286845e6f8afc09fdcdf0b94248239963e3ad04495e927a68a2148814c65fd这种情况下如果要查看容器的日志有两种方法attach 到该容器。用docker logs命令查看日志。先来看 attach 的方法。运行docker attach命令。[rootdocker ~]# docker attach a8286845e6f8attach 到了 httpd 容器但并没有任何输出这是因为当前没有新的日志信息。为了产生一条新的日志可以在 host 的另一个命令行终端执行curl localhost。终端B[rootdocker ~]# curl localhosthtmlbodyh1It works!/h1/body/html[rootdocker ~]# curl localhosthtmlbodyh1It works!/h1/body/html这时attach 的终端就会打印出新的日志。终端A:[rootdocker ~]# docker attach a8286845e6f8172.17.0.1 - -[08/Oct/2024:14:30:24 0000]GET / HTTP/1.120045172.17.0.1 - -[08/Oct/2024:14:30:25 0000]GET / HTTP/1.120045attach 的方法在实际使用中不太方便因为只能看到 attach 之后的日志以前的日志不可见。退出 attach 状态比较麻烦Ctrlp 然后 Ctrlq 组合键一不小心很容器将容器杀掉比如按下 CtrlC。查看容器日志推荐的方法是用docker logs命令。[rootdocker ~]# docker logs a8286845e6f8AH00558: httpd: Could not reliably determine the servers fully qualified domain name, using 172.17.0.3. Set the ServerName directive globally to suppress this message AH00558: httpd: Could not reliably determine the servers fully qualified domain name, using172.17.0.3. Set theServerNamedirective globally to suppress this message[Tue Oct 0814:28:33.1687122024][mpm_event:notice][pid1:tid1]AH00489: Apache/2.4.62(Unix)configured -- resuming normal operations[Tue Oct 0814:28:33.1688012024][core:notice][pid1:tid1]AH00094: Command line:httpd -D FOREGROUND[Tue Oct 0814:29:37.1607292024][mpm_event:notice][pid1:tid1]AH00491: caught SIGTERM, shutting down AH00558: httpd: Could not reliably determine the servers fully qualified domain name, using 172.17.0.3. Set the ServerName directive globally to suppress this message AH00558: httpd: Could not reliably determine the servers fully qualified domain name, using172.17.0.3. Set theServerNamedirective globally to suppress this message[Tue Oct 0814:30:08.3792262024][mpm_event:notice][pid1:tid1]AH00489: Apache/2.4.62(Unix)configured -- resuming normal operations[Tue Oct 0814:30:08.3793752024][core:notice][pid1:tid1]AH00094: Command line:httpd -D FOREGROUND172.17.0.1 - -[08/Oct/2024:14:30:24 0000]GET / HTTP/1.120045172.17.0.1 - -[08/Oct/2024:14:30:25 0000]GET / HTTP/1.120045[Tue Oct 0814:31:35.6643862024][mpm_event:notice][pid1:tid1]AH00491: caught SIGTERM, shutting down AH00558: httpd: Could not reliably determine the servers fully qualified domain name, using 172.17.0.3. Set the ServerName directive globally to suppress this message AH00558: httpd: Could not reliably determine the servers fully qualified domain name, using172.17.0.3. Set theServerNamedirective globally to suppress this message[Tue Oct 0814:31:41.9951592024][mpm_event:notice][pid1:tid1]AH00489: Apache/2.4.62(Unix)configured -- resuming normal operations[Tue Oct 0814:31:41.9953392024][core:notice][pid1:tid1]AH00094: Command line:httpd -D FOREGROUND172.17.0.1 - -[08/Oct/2024:14:30:24 0000]GET / HTTP/1.120045172.17.0.1 - -[08/Oct/2024:14:30:25 0000]GET / HTTP/1.120045docker logs能够打印出自容器启动以来完整的日志并且-f参数可以继续打印出新产生的日志效果上与 Linux 命令tail -f一样。