You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Compose中Actuator健康检查异常,容器显示Unhealthy

问题原因及解决办法

核心原因

Docker容器的健康检查命令是在容器内部执行的,你当前配置的健康检查用了localhost:8082,但容器内的Spring Boot服务实际监听的是8081端口,8082是宿主机的端口映射,容器内部根本没有这个端口,所以健康检查请求会失败,容器被标记为unhealthy。

两种curl调用的差异

  • 宿主机执行curl localhost:8082/actuator/health:请求从宿主机发起,通过Docker的端口映射规则8082:8081,把请求转发到容器内的8081端口,所以能正常获取到{status:UP}的响应。
  • 容器内部执行curl localhost:8082/actuator/health:请求在容器内部发起,容器内没有进程监听8082端口,这个请求直接失败,导致健康检查脚本返回非0状态码,Docker因此标记容器为unhealthy。

修复方案

修改docker-compose中的健康检查命令,使用容器内部的8081端口:

healthcheck:
  test: ["CMD-SHELL", "curl --fail localhost:8081/actuator/health | grep '\"status\":\"UP\"' || exit 1"]
  interval: 30s
  timeout: 10s
  retries: 3

注意:如果你的镜像中没有预装curl,需要先在Dockerfile中安装(比如Debian/Ubuntu镜像用apt-get install -y curl,Alpine镜像用apk add --no-cache curl)。另外,用grep '\"status\":\"UP\"'比单纯grep UP更准确,避免匹配到响应中其他包含UP的内容。

内容的提问来源于stack exchange,提问作者Pu6

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 17:04:54