首页/云/Docker 容器速查/compose healthcheck 字段
Compose 编排55 条

compose healthcheck 字段

在 compose 文件里给服务定义健康检查:test 命令、间隔、超时、重试与启动宽限期;健康状态也是 up --wait 与依赖等待的依据。

Docker 容器速查云更新于 2026-10-12
字段test / interval / timeout / retries / start_period
disablehealthcheck: disable: true 关闭
生效docker compose ps 显示 healthy/unhealthy

示例

示例
services:
  web:
    image: nginx:1.27-alpine
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://localhost/"]
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 20s
start_period 内的失败不计入重试次数,适合启动慢的应用;健康检查命令要在镜像内真实存在。

易错点

  • 检查命令(curl/wget)不存在时容器会一直 unhealthy,选镜像里有的工具或装好再用
  • interval 过小会让日志与 CPU 开销上升,按服务特性设定合理间隔

数据来源与参考

  • Docker 官方文档:Compose healthcheck 参考(docs.docker.com/reference/compose-file/services)
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌