본문 바로가기
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 — 컨테이너들이야. 각각 이름을 갖고 imagebuild, ports, environment, volumes, depends_on 을 달아.
  • volumes — 이름 붙은 영구 디스크 (Postgres 데이터, ML 모델 캐시 같은 것).
  • networks — 보통은 기본 하나로 충분해. stack 을 서로 이어야 할 때만 더 선언하고.

depends_on 미묘함

기본 depends_on 은 '이 순서로 띄워라' 일 뿐이야. '준비될 때까지 기다려라' 가 아니고. 진짜로 기다리게 하려면 condition: service_healthy 를 쓰고, 기다릴 대상 service 에 healthcheck 를 정의해줘야 해. 그냥 쓴 형태는 발등 찍기 딱 좋아. 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

직접 쓴 compose.yaml 중에 service 가 여럿인 걸 골라. 제일 밑에 깔린 의존 대상에 (db 든 redis 든) 진짜 healthcheck 를 달아. 그리고 거기 기대는 service 들의 depends_on 을 전부 healthy-condition 형태로 바꿔. docker compose down && docker compose up 을 돌려서 다들 얌전히 기다리는지 확인해. 공짜로 얻는 안정성이야.

Progress

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

댓글 0

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

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