容器显示 Up 不等于服务可用:主进程还活着,但应用已经连不上数据库或端口没在监听,照样会被挂到负载均衡后面接流量。
加 HEALTHCHECK,用 curl 或专门的探活接口定期探测,四个参数都要写:interval 探测间隔、timeout 单次超时、retries 连续失败几次判定不健康、start_period 启动宽限期。
最后这个别省,慢启动的应用会被反复判死。
依赖关系用 depends_on 的 service_healthy 条件,等真正健康再起。
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
CMD curl -fsS http://localhost:8080/healthz || exit 1
# docker compose
depends_on:
db:
condition: service_healthy
# 查看探测结果
docker inspect --format '{{json .State.Health}}' <容器>
THE END






暂无评论内容