C.W.K.
Stream
Lesson 03 of 04 · published

depends_on + healthcheck — 진짜 준비될 때까지 기다려

~14 min · compose, production

Level 0Container 호기심
0 XP0/36 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

고전 시작 race

API 가 Postgres 의존. Compose 가 둘 다 시작. API container 200ms 만에 up. Postgres 는 4초 걸려야 connection 받을 준비. API 가 connect 시도, 실패, exit. docker compose up 이 API 죽었다고 보고. 해결은 retry loop X — depends_on: condition: service_healthy.

service_healthy 는 HEALTHCHECK 통과 기다림

의존성에 HEALTHCHECK 추가 (compose 파일이나 Dockerfile). 의존하는 서비스는 그 체크 그린 될 때까지 기다림.

Code

API 가 DB 진짜 준비될 때까지 기다림·yaml
services:
  api:
    build: ./api
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started

  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: secret
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 3s
      retries: 5
      start_period: 10s

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

External links

Exercise

기존 compose stack 가져와 (또는 API + Postgres 미니멀로 빌드). race 확인: HEALTHCHECK 빼고, DB 시작에 5초 sleep 추가, API 실패하는 거 봐. 그 다음 HEALTHCHECK + condition: service_healthy 추가, API 가 인내심 있게 기다리는 거 확인.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.