查看日志与监控
约 803 字大约 3 分钟
布欧-Lewyon
2026-05-15
首页 › Docker › 容器管理(在新窗口打开) › 查看日志与监控
docker logs 查看容器日志
# 查看容器所有日志
docker logs web
# 实时追踪(类似 tail -f)
docker logs -f web
# 查看最近 50 行
docker logs --tail 50 web
# 查看指定时间后的日志
docker logs --since 2026-05-15T10:00:00 web
docker logs --since 10m web # 最近 10 分钟
# 查看指定时间前的日志
docker logs --until 2026-05-15T12:00:00 web
# 添加时间戳
docker logs -t web
# 组合使用
docker logs -f --tail 100 -t web日志驱动
Docker 支持多种日志驱动,决定了日志数据的流向:
# 查看当前日志驱动
docker info --format '{{.LoggingDriver}}'| 驱动 | 说明 | 适用场景 |
|---|---|---|
json-file(默认) | 以 JSON 文件存磁盘,docker logs 读取 | 单机调试和开发 |
syslog | 发送到 Syslog | Linux 传统日志系统 |
journald | 发送到 systemd journal | systemd 系统 |
gelf | 发送到 Graylog | 集中日志管理 |
fluentd | 发送到 Fluentd | 日志收集管道 |
awslogs | 发送到 CloudWatch | AWS 环境 |
none | 不记录日志 | 极简场景 |
# 运行容器时指定日志驱动
docker run -d --log-driver syslog --log-opt syslog-address=udp://192.168.1.1:514 nginx
# 限制日志文件大小(json-file 驱动有效)
docker run -d \
--log-opt max-size=10m \
--log-opt max-file=3 \
nginx生产环境建议将日志集中到外部系统(ELK、Grafana Loki、Fluentd),避免日志撑满磁盘。
docker stats 实时监控
# 查看所有容器的资源使用
docker stats
# 查看特定容器
docker stats web db
# 只输出一次(不持续刷新)
docker stats --no-stream
# 格式化输出
docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}"输出示例:
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O
a1b2c3d4e5f6 web 0.15% 12.34MiB / 1.944GiB 0.62% 1.23kB / 654B 0B / 0Bdocker events 实时事件流
# 查看 Docker 守护进程的实时事件
docker events
# 过滤容器事件
docker events --filter 'container=web'
# 只关注 start/stop/kill
docker events --filter 'event=start' --filter 'event=stop'容器文件变更查看
# 查看容器文件系统的变更(新增/修改/删除)
docker diff web
# A: 新增文件(Added)
# C: 修改文件(Changed)
# D: 删除文件(Deleted)
# 示例输出
# C /etc/nginx/nginx.conf
# A /var/log/nginx/access.log
# A /usr/share/nginx/html/index.html磁盘空间分析
# 查看 Docker 整体磁盘用量
docker system df
# TYPE TOTAL ACTIVE SIZE RECLAIMABLE
# Images 5 2 1.2GB 800MB (66%)
# Containers 8 3 12.3MB 8.1MB (65%)
# Local Volumes 3 1 58MB 0B (0%)
# Build Cache - - 120MB 80MB (66%)
# 详细版
docker system df -v日志排查流程
# 1. 容器启动失败时先看日志
docker logs web --tail 50
# 2. 检查资源使用
docker stats --no-stream web
# 3. 进入容器排查
docker exec -it web sh
# 4. 查看容器内应用日志
cat /var/log/app/error.log
# 5. 查看 Docker 事件
docker events --filter 'container=web' --since 1h小结
docker logs -f --tail N实时查看容器日志;--since/--until按时间过滤。- 日志驱动决定日志去向,生产环境建议用 fluentd/syslog 集中管理。
docker stats实时监控 CPU/内存/网络,docker events查看事件流。docker diff查看容器文件变更,有助于确认配置文件是否生效。docker system df分析 Docker 磁盘空间使用。
