C.W.K.
Stream
Lesson 02 of 08 · published

Docker Compose — service, network, volume

~12 min · yaml, docker, compose

Level 0평문
0 XP0/64 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

한 YAML 파일로 멀티 컨테이너 앱

compose.yaml (또는 docker-compose.yaml, 둘 다 유효) 가 stack 묘사 — 여러 컨테이너, 네트워크, 볼륨, 의존성. docker compose up 가 전체 stack 온라인. 같은 YAML 문법으로 Kubernetes 와; 매우 다른 의미 (단일 호스트, 조정 없음).

건드릴 세 최상위 섹션

  • services — 컨테이너. 각각 이름 붙고, image 또는 build, ports, environment, volumes, depends_on.
  • volumes — 이름 붙은 영구 디스크 (Postgres 데이터, ML 모델 캐시).
  • networks — 보통 기본 하나; 교차 stack 와이어링엔 추가 선언.

depends_on 미묘함

기본으로 depends_on = '이 순서로 시작' — '준비될 때까지 기다림' 아님. 진짜 readiness 엔 condition: service_healthy 사용 + 의존된 service 에 healthcheck 정의. plain 형태는 footgun: Postgres 가 연결 받기 전 앱 시작.

version: "3.8" 줄은 죽었어. Compose Spec v2 (2020+) 가 version: 필드 무시. 새 파일은 생략. version: "3.8" 가진 옛 파일 여전히 작동하지만 필드는 장식. 옛 튜토리얼에서 의미 있는 척 복사하지 마.

Code

진짜 healthcheck 가진 세 service stack·yaml
services:
  postgres:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: dev
      POSTGRES_DB: pippa
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 3s
      retries: 10

  api:
    build: .
    ports:
      - "8000:8000"
    environment:
      DATABASE_URL: postgresql://postgres:dev@postgres:5432/pippa
    depends_on:
      postgres:
        condition: service_healthy

  worker:
    build: .
    command: ["python", "worker.py"]
    depends_on:
      postgres:
        condition: service_healthy

volumes:
  pgdata:
옵션 service 에 profile·yaml
services:
  api:
    image: ghcr.io/cwk/api
    # 항상 실행

  observability:
    image: grafana/grafana
    profiles: ["obs"]
    # docker compose --profile obs up 으로만 실행

  loadtest:
    image: locustio/locust
    profiles: ["perf"]
    # docker compose --profile perf up 으로만 실행
검증 (anchor 확장)·bash
# 변수 다 해석, anchor 확장, 정규화 형태 표시
docker compose config

# 빠른 syntax 체크
docker compose config --quiet && echo OK

# service 별 해석된 env 보기
docker compose config --format json | jq '.services.api.environment'

External links

Exercise

여러 service 가진 compose.yaml 작성한 거 골라. 가장 아래 의존성 (db, redis, 무엇이든) 에 진짜 healthcheck 추가. 모든 의존 service 의 depends_on 을 plain 형태에서 healthy-condition 형태로 전환. docker compose down && docker compose up 실행 — 의존 service 들이 dep 가 healthy 될 때까지 기다리는 거 확인. 무료 신뢰성.

Progress

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

댓글 0

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

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