Podman 教程

Podman 查看容器日志

在 Podman 中,可以通过 podman logs 命令查看容器日志,它支持实时跟踪日志、基于时间过滤日志、日志限制等实用功能,能满足不同场景下的日志排查需求。

基础语法

podman logs [选项] 容器名称或ID

常用选项说明:

  • -f, --follow    实时跟踪日志输出(类似 tail -f),容器产生新日志时会持续显示。

  • --tail=N    只显示最后 N 行日志(默认显示所有日志)。

  • -t, --timestamps   显示每条日志的时间戳(精确到毫秒),便于追溯时间点。

  • --since=时间    仅显示指定时间之后的日志,支持两种时间格式:

    • 绝对时间(如 2024-05-01T08:30:00)

    • 相对时间(如 2h 表示 2 小时前,10m 表示 10 分钟前)

  • --until=时间    仅显示指定时间之前的日志,格式同 --since。

点击查看 podman logs 命令手册

  

常见使用场景示例

查看指定容器日志(所有日志)

执行 “podman logs [容器名/容器ID]” 命令查看日志,默认输出容器启动后的所有日志。如果你的服务稳定运行了很久(如一个月),建议不要轻易运行该命令,将输出太多的日志。

C:\Users\Administrator> podman logs my-nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2025/11/11 05:16:11 [notice] 1#1: using the "epoll" event method
2025/11/11 05:16:11 [notice] 1#1: nginx/1.29.3
...

实时跟踪日志

执行 “podman logs -f [容器名/容器ID]” 命令(类似 tail -f)可以实时打印新增日志。这对服务进行调试很方便,可以实时观察服务的日志输出,类在 IDEA 中运行服务后,可以实时查看日志一样。

C:\Users\Administrator> podman logs -f my-nginx
...
2025/11/13 01:27:35 [notice] 1#1: start worker process 21
2025/11/13 01:27:35 [notice] 1#1: start worker process 22
2025/11/13 01:27:35 [notice] 1#1: start worker process 23
2025/11/13 01:27:35 [notice] 1#1: start worker process 24

查看最后 N 行日志

执行 “podman logs --tail=N [容器名/容器ID]”命令查看最后 N 行日志,其中,N 为具体数字(如 --tail=100 查看最后 100 行)。

当容器运行中出现的错误或异常往往记录在最新的日志中,使用该命令查看最后 N 行可以快速定位最近发生的问题(如崩溃、连接失败等),避免翻阅大量历史日志。

C:\Users\Administrator> podman logs --tail=5 my-nginx
2025/11/13 01:27:35 [notice] 1#1: start worker process 20
2025/11/13 01:27:35 [notice] 1#1: start worker process 21
2025/11/13 01:27:35 [notice] 1#1: start worker process 22
2025/11/13 01:27:35 [notice] 1#1: start worker process 23
2025/11/13 01:27:35 [notice] 1#1: start worker process 24

查看指定时间后的日志

执行“podman logs --since "2024-05-01T10:00:00" [容器名/容器 ID]”命令查看指定时间后记录的日志,支持格式包括 YYYY-MM-DDTHH:MM:SS、相对时间(如 1h 表示 1 小时前、30m 表示 30 分钟前)。

C:\Users\Administrator> podman logs --since "2025-11-13T01:27:35" my-nginx
...
2025/11/13 01:27:35 [notice] 1#1: start worker process 21
2025/11/13 01:27:35 [notice] 1#1: start worker process 22
2025/11/13 01:27:35 [notice] 1#1: start worker process 23
2025/11/13 01:27:35 [notice] 1#1: start worker process 24

或者

# 查看最近30分钟的日志
C:\Users\Administrator> podman logs --since 30m my-nginx
...
2025/11/13 01:27:35 [notice] 1#1: start worker process 20
2025/11/13 01:27:35 [notice] 1#1: start worker process 21
2025/11/13 01:27:35 [notice] 1#1: start worker process 22
2025/11/13 01:27:35 [notice] 1#1: start worker process 23
2025/11/13 01:27:35 [notice] 1#1: start worker process 24

查看指定时间范围内的日志

执行 “podman logs --since "1h" --until "30m" [容器名/容器 ID]”命令,可以查看 1 小时前到 30 分钟前的日志。

# 查看 my-nginx 容器中20小时前到30分钟前的日志
C:\Users\Administrator> podman logs --since 20h --until 30m my-nginx
2025/11/12 09:07:05 [notice] 1#1: using the "epoll" event method
2025/11/12 09:07:05 [notice] 1#1: nginx/1.29.3
...
2025/11/13 01:27:35 [notice] 1#1: start worker process 22
2025/11/13 01:27:35 [notice] 1#1: start worker process 23
2025/11/13 01:27:35 [notice] 1#1: start worker process 24

显示日志时间戳

执行 “podman logs --timestamps [容器名/容器 ID]”命令(缩写 -t),将会在输出的每条日志前添加时间戳。

C:\Users\Administrator> podman logs --timestamps my-nginx
2025-11-11T13:16:11+08:00 /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
2025-11-11T13:16:11+08:00 /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
2025-11-11T13:16:11+08:00 /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
2025-11-11T13:16:11+08:00 10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
...

或者

C:\Users\Administrator> podman logs -t my-nginx
2025-11-11T13:16:11+08:00 /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
2025-11-11T13:16:11+08:00 /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
2025-11-11T13:16:11+08:00 /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
2025-11-11T13:16:11+08:00 10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
...

只显示错误日志(部分容器支持)

将 podman logs 命令结合 grep 过滤实现日志过滤功能,例如 podman logs -f [容器名] | grep "ERROR"。

[user@localhost ~]$ podman logs -f my-nginx | grep -i "error"
2025/11/11 05:16:36 [error] 19#19: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 10.88.0.2, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:9000", referrer: "http://localhost:9000/"
2025/11/13 02:03:00 [error] 18#18: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 10.88.0.2, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:9000", referrer: "http://localhost:9000/"
2025/11/13 02:03:47 [error] 18#18: *1 open() "/usr/share/nginx/html/test/index.html" failed (2: No such file or directory), client: 10.88.0.2, server: localhost, request: "GET /test/index.html HTTP/1.1", host: "localhost:9000"
...

注意,上面命令仅适合 linux。Windows 可以执行如下命令:

C:\Users\Administrator> podman logs -f my-nginx | findstr /I "error"
2025/11/11 05:16:36 [error] 19#19: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 10.88.0.2, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:9000", referrer: "http://localhost:9000/"
2025/11/13 02:03:00 [error] 18#18: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 10.88.0.2, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:9000", referrer: "http://localhost:9000/"
...

更多信息请执行 podman logs --help 命令查看命令手册。

  

说说我的看法
全部评论(
没有评论
关于
本网站专注于 Java、数据库(MySQL、Oracle)、Linux、软件架构及大数据等多领域技术知识分享。涵盖丰富的原创与精选技术文章,助力技术传播与交流。无论是技术新手渴望入门,还是资深开发者寻求进阶,这里都能为您提供深度见解与实用经验,让复杂编码变得轻松易懂,携手共赴技术提升新高度。如有侵权,请来信告知:hxstrive@outlook.com
其他应用
公众号