全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-21 6 分钟 ✍️ juanwangdev

服务依赖与健康检查

多容器应用需要确保服务依赖就绪,下面介绍依赖与健康检查配置。

depends_on 局限

YAML
# 仅等待容器启动,不等待服务就绪
services:
  app:
    build: .
    depends_on:
      - db  # db 容器启动后立即启动 app,但数据库可能未就绪

  db:
    image: postgres:15

depends_on 仅等待容器启动,不检查服务是否真正可用。

healthcheck 配置

YAML
services:
  db:
    image: postgres:15
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s      # 检查间隔
      timeout: 5s        # 超时时间
      retries: 5         # 重试次数
      start_period: 30s  # 启动宽限期

检查命令示例

YAML
# PostgreSQL
healthcheck:
  test: ["CMD-SHELL", "pg_isready -U postgres"]

# MySQL
healthcheck:
  test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]

# Redis
healthcheck:
  test: ["CMD", "redis-cli", "ping"]

# HTTP 服务
healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost:3000/health"]

# 自定义脚本
healthcheck:
  test: ["CMD", "/app/healthcheck.sh"]

配合使用

YAML
version: '2.1'  # 需要 2.1+ 支持 condition

services:
  web:
    image: nginx
    depends_on:
      app:
        condition: service_healthy

  app:
    build: .
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 10s
      timeout: 5s
      retries: 3

  db:
    image: postgres:15
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3

重试策略

YAML
services:
  app:
    build: .
    depends_on:
      db:
        condition: service_healthy
    # 启动后等待 db 就绪
    command: >
      sh -c "
        until pg_isready -h db -U postgres; do
          echo 'Waiting for db...'
          sleep 2
        done
        echo 'DB ready, starting app'
        node server.js
      "      

查看健康状态

Bash
# 查看所有服务健康状态
docker compose ps

# 查看容器健康状态
docker inspect --format='{{.State.Health.Status}}' my-app-db-1

# 查看健康检查日志
docker inspect --format='{{json .State.Health}}' my-app-db-1

完整示例

YAML
version: '3.8'

services:
  api:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    environment:
      DATABASE_URL: postgres://postgres:password@db:5432/myapp
      REDIS_URL: redis://redis:6379

  db:
    image: postgres:15
    volumes:
      - db-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    environment:
      POSTGRES_PASSWORD: password
      POSTGRES_DB: myapp

  redis:
    image: redis:7-alpine
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3

volumes:
  db-data:

要点总结

  • depends_on 仅等待容器启动,不保证服务可用
  • healthcheck 定期检查服务健康状态,支持多种检查方式
  • 使用 condition: service_healthy 等待健康检查通过
  • 启动宽限期(start_period)避免服务启动阶段误判
  • 生产环境必须配置健康检查,结合 depends_on 保证启动顺序

📝 发现内容有误?点击此处直接编辑

← 上一篇 扩缩容与负载均衡
下一篇 → 环境变量与配置文件
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库