首页/云/Docker 容器速查/docker run 运行时健康检查参数
基础命令50 条

docker run 运行时健康检查参数

启动时用 --health-cmd 等参数为容器加健康检查,Docker 周期性执行该命令并记录状态,docker ps 的 STATUS 会显示 (healthy)。

Docker 容器速查云更新于 2026-10-12
关键参数--health-cmd 检查命令(必填)
周期参数--health-interval 默认 30s、--health-timeout 默认 30s
其他--health-retries 默认 3、--health-start-period 启动宽限

示例

示例
docker run -d --name web \
  --health-cmd 'wget -qO- http://localhost/ >/dev/null || exit 1' \
  --health-interval 30s --health-timeout 5s --health-retries 3 \
  nginx:1.27-alpine
docker inspect -f '{{.State.Health.Status}}' web
状态会经历 starting → healthy 或 unhealthy;unhealthy 只是标记,不会自动重启容器(Swarm/K8s 另有编排行为)。

易错点

  • 检查命令依赖容器内工具,alpine 没有 curl 时用 wget 或 nc,检查本身报 127 会被判为 unhealthy
  • start-period 太短会把还在初始化的应用误判为 unhealthy,慢启动服务要显式加长

数据来源与参考

  • Docker 官方 CLI 参考:docker run --health-cmd 系列(docs.docker.com)
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌