首页/云/云与容器速查/Compose 健康检查配置
容器与 Docker106 条

Compose 健康检查配置

在服务下定义 healthcheck:test 命令、周期、超时与启动宽限。它是 depends_on 条件、up --wait 与编排调度的共同依据,也是容器自愈的基础。

云与容器速查云更新于 2026-10-12
test["CMD", "curl", "-f", "http://localhost/health"] 或 CMD-SHELL 单串
周期interval / timeout / retries / start_period(启动宽限期内失败不计)
状态查看docker compose ps 显示 (healthy)/(unhealthy)
与编排联动service_healthy 条件、up --wait 都依赖它

示例

示例
healthcheck:
  test: ["CMD-SHELL", "curl -fsS http://127.0.0.1:8080/healthz || exit 1"]
  interval: 15s
  timeout: 5s
  retries: 3
  start_period: 30s
健康检查命令必须在容器内存在:alpine/distroless 里没 curl 是常见踩坑,可用 wget 或应用自带探活接口。

易错点

  • start_period 设太短:慢启动服务被反复判定 unhealthy
  • 把健康检查写成重逻辑(查库全表):探测本身成了负载源

数据来源与参考

  • Docker Docs:Compose healthcheck

相关工具

​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌