Docker 教程

Docker 查看容器日志

在 Docker 中,使用 docker container logs 命令查看 Docker 容器的输出日志。

语法

语法如下:

用法:docker container logs [OPTIONS] CONTAINER

获取容器的日志

别名:
  docker container logs, docker logs

选项
  --details   显示日志中提供的额外细节
  -f, -follow   跟随日志输出
  --since string   显示自时间戳(如 “2013-01-02T13:23:37Z”)
              或相对时间戳(如 “42m” 表示 42 分钟)起的日志输出
  -n,--tail string   从日志末尾开始显示的行数(默认为 “所有”)
  -t, --timestamps   显示时间戳
  --until string   显示时间戳(如 “2013-01-02T13:23:37Z”)之前的日志或相对日志(如 “42m” 表示 42 分钟)。

基本用法

docker container logs [OPTIONS] CONTAINER

参数说明:

  • CONTAINER   要查看日志的容器的名称或 ID。

例如:

root@hxvm2:~# docker container logs redis
1:C 23 Aug 2024 02:49:48.071 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:C 23 Aug 2024 02:49:48.071 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 23 Aug 2024 02:49:48.071 * Redis version=7.4.0, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 23 Aug 2024 02:49:48.071 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 23 Aug 2024 02:49:48.071 * monotonic clock: POSIX clock_gettime
1:M 23 Aug 2024 02:49:48.072 * Running mode=standalone, port=6379.
1:M 23 Aug 2024 02:49:48.073 * Server initialized
1:M 23 Aug 2024 02:49:48.074 * Ready to accept connections tcp

上面命令将显示名为 redis 的容器的日志输出。

其他用法

试试跟踪输出

使用 -f 或 --follow 选项,实时跟踪容器的日志输出,类似于tail -f命令的功能。例如:

root@hxvm2:~# docker container logs -f redis
1:C 23 Aug 2024 02:49:48.071 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:C 23 Aug 2024 02:49:48.071 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 23 Aug 2024 02:49:48.071 * Redis version=7.4.0, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 23 Aug 2024 02:49:48.071 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 23 Aug 2024 02:49:48.071 * monotonic clock: POSIX clock_gettime
1:M 23 Aug 2024 02:49:48.072 * Running mode=standalone, port=6379.
1:M 23 Aug 2024 02:49:48.073 * Server initialized
1:M 23 Aug 2024 02:49:48.074 * Ready to accept connections tcp

将持续显示容器的最新日志内容。

显示时间戳

使用 -t 或 --timestamps 选项,在日志输出中显示时间戳。例如:

root@hxvm2:~# docker container logs -t redis
2024-08-23T02:49:48.071762450Z 1:C 23 Aug 2024 02:49:48.071 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2024-08-23T02:49:48.071855342Z 1:C 23 Aug 2024 02:49:48.071 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2024-08-23T02:49:48.071867041Z 1:C 23 Aug 2024 02:49:48.071 * Redis version=7.4.0, bits=64, commit=00000000, modified=0, pid=1, just started
2024-08-23T02:49:48.071868640Z 1:C 23 Aug 2024 02:49:48.071 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
2024-08-23T02:49:48.071870040Z 1:M 23 Aug 2024 02:49:48.071 * monotonic clock: POSIX clock_gettime
2024-08-23T02:49:48.073054035Z 1:M 23 Aug 2024 02:49:48.072 * Running mode=standalone, port=6379.
2024-08-23T02:49:48.073284615Z 1:M 23 Aug 2024 02:49:48.073 * Server initialized
2024-08-23T02:49:48.074407315Z 1:M 23 Aug 2024 02:49:48.074 * Ready to accept connections tcp

日志将显示每条记录的时间信息。

显示末尾 n 行

使用 --tail 选项,只显示最后指定行数的日志行。例如:

root@hxvm2:~# docker container logs --tail 5 redis
1:C 23 Aug 2024 02:49:48.071 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 23 Aug 2024 02:49:48.071 * monotonic clock: POSIX clock_gettime
1:M 23 Aug 2024 02:49:48.072 * Running mode=standalone, port=6379.
1:M 23 Aug 2024 02:49:48.073 * Server initialized
1:M 23 Aug 2024 02:49:48.074 * Ready to accept connections tcp

将显示容器日志的最后 5 行。

常见用途

(1)故障排除

当容器中的应用出现问题时,查看日志是一种常见的故障排除方法。日志中可能包含错误消息、警告信息或其他有助于确定问题根源的线索。

例如,如果容器中的 Web 应用无法启动,可以查看日志以确定是否有数据库连接错误、配置问题或其他异常情况。

(2)监控应用状态

通过定期查看容器的日志,可以了解应用的运行状态和活动情况。日志可以提供关于请求处理、资源使用、错误率等方面的信息。

例如,对于一个在线服务,可以通过查看日志来监控请求的响应时间、错误率和吞吐量,以便及时发现性能问题。

(3)调试应用程序

在开发过程中,可以使用日志来调试容器中的应用程序。通过在应用代码中插入适当的日志语句,可以跟踪程序的执行流程、变量值和错误情况。

例如,在开发一个新的微服务时,可以在关键位置添加日志语句,以便在测试和调试过程中了解程序的行为。

总结

docker container logs 命令是一个非常有用的工具,用于查看 Docker 容器的日志输出,帮助用户进行故障排除、监控应用状态和调试应用程序。

说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号