A A
[Docker] Docker Compose: ๋‹ค์ค‘ ์ปจํ…Œ์ด๋„ˆ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ด€๋ฆฌํ•˜๊ธฐ

Docker Compose์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Docker Compose๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์šด์˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋Š” ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. ๋‹จ์ผ ํŒŒ์ผ(docker-compose.yaml)์„ ํ†ตํ•ด ์ด๋ฏธ์ง€ ๋นŒ๋“œ, ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰, ๋ณผ๋ฅจ ๋ฐ ๋„คํŠธ์›Œํฌ ์ƒ์„ฑ ๋“ฑ์„ ๊ฐ„ํŽธํ•˜๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ธ€์—์„œ๋Š” Docker Compose์˜ ๊ธฐ๋ณธ ๊ฐœ๋…๋ถ€ํ„ฐ ์‹ค์Šต ์˜ˆ์ œ๊นŒ์ง€ ๋‹จ๊ณ„๋ณ„๋กœ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


Docker Compose๋ž€?

 

Compose file reference

Find the latest recommended version of the Docker Compose file format for defining multi-container applications.

docs.docker.com

 

Docker Compose๋Š” ๋‹ค์ค‘ ์ปจํ…Œ์ด๋„ˆ Docker ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ •์˜ํ•˜๊ณ  ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. YAML ํ˜•์‹์˜ ํŒŒ์ผ(docker-compose.yaml)์„ ์‚ฌ์šฉํ•˜์—ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„œ๋น„์Šค, ๋„คํŠธ์›Œํฌ, ๋ณผ๋ฅจ ๋“ฑ์„ ์„ ์–ธ์ ์œผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋ณต์žกํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์Šคํƒ์„ ๋‹จ์ˆœํ™”ํ•˜๊ณ , ์ผ๊ด€๋œ ํ™˜๊ฒฝ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฃผ์š” ํŠน์ง•

  • ๊ฐ„ํŽธํ•œ ์„ค์ •: YAML ํŒŒ์ผ์„ ํ†ตํ•ด ์—ฌ๋Ÿฌ ์ปจํ…Œ์ด๋„ˆ์˜ ์„ค์ •์„ ํ•œ ๊ณณ์—์„œ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ผ๊ด„ ๊ด€๋ฆฌ: ๋‹จ์ผ ๋ช…๋ น์–ด๋กœ ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋นŒ๋“œ, ์‹œ์ž‘, ์ค‘์ง€, ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ™˜๊ฒฝ ์ผ๊ด€์„ฑ: ๊ฐœ๋ฐœ, ํ…Œ์ŠคํŠธ, ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ ๋™์ผํ•œ ์„ค์ •์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์˜์กด์„ฑ ๊ด€๋ฆฌ: ์„œ๋น„์Šค ๊ฐ„์˜ ์˜์กด์„ฑ์„ ์ •์˜ํ•˜์—ฌ ์˜ฌ๋ฐ”๋ฅธ ์ˆœ์„œ๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Docker Compose์˜ ์ฃผ์š” ๊ธฐ๋Šฅ

  • ์ด๋ฏธ์ง€ ๋นŒ๋“œ: docker-compose.yaml ํŒŒ์ผ ๋‚ด์—์„œ ๋นŒ๋“œํ•  ์ด๋ฏธ์ง€๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰: ์—ฌ๋Ÿฌ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋™์‹œ์— ์‹คํ–‰ํ•˜๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ณผ๋ฅจ ๋ฐ ๋„คํŠธ์›Œํฌ ๊ด€๋ฆฌ: ํ•„์š”ํ•œ ๋ณผ๋ฅจ๊ณผ ๋„คํŠธ์›Œํฌ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜๊ณ  ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์„œ๋น„์Šค ๊ฐ„ ์˜์กด์„ฑ: ์„œ๋น„์Šค ๊ฐ„์˜ ์˜์กด์„ฑ์„ ์ •์˜ํ•˜์—ฌ ์˜ฌ๋ฐ”๋ฅธ ์ˆœ์„œ๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๊ด€๋ฆฌ: ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ํŒŒ์ผ์ด๋‚˜ ์™ธ๋ถ€ ์†Œ์Šค์—์„œ ๋กœ๋“œํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Docker Compose ์„ค์น˜ํ•˜๊ธฐ

Docker Compose๋Š” Docker Desktop์— ํฌํ•จ๋˜์–ด ์žˆ์–ด ๋ณ„๋„์˜ ์„ค์น˜๊ฐ€ ํ•„์š” ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, Linux ํ™˜๊ฒฝ์—์„œ๋Š” ๋ณ„๋„๋กœ ์„ค์น˜ํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ค์น˜ ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์˜ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.

 

docker compose

 

docs.docker.com

์„ค์น˜ ํ™•์ธ

docker compose version

์˜ˆ์‹œ ์ถœ๋ ฅ

Docker Compose version v2.20.3

Docker Compose ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•

Docker Compose๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” docker-compose.yaml ํŒŒ์ผ์„ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์ด ํŒŒ์ผ์— ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ชจ๋“  ์„œ๋น„์Šค๋ฅผ ์ •์˜ํ•˜๊ณ , Docker Compose ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ด๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Compose ํŒŒ์ผ ๊ตฌ์กฐ

Compose ํŒŒ์ผ์€ YAML ํ˜•์‹์œผ๋กœ ์ž‘์„ฑ๋˜๋ฉฐ, ์ฃผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
version: '3.8' # Compose ํŒŒ์ผ ๋ฒ„์ „

services: # ์„œ๋น„์Šค ์ •์˜
  service_name: # ์„œ๋น„์Šค ์ด๋ฆ„
    image: image_name # ์‚ฌ์šฉํ•  Docker ์ด๋ฏธ์ง€
    build: # ์ด๋ฏธ์ง€ ๋นŒ๋“œ ์„ค์ • (์„ ํƒ ์‚ฌํ•ญ)
      context: ./path # ๋นŒ๋“œ ์ปจํ…์ŠคํŠธ
      dockerfile: Dockerfile # ์‚ฌ์šฉํ•  Dockerfile
      args: # ๋นŒ๋“œ ์ธ์ˆ˜
        - ARG_NAME=value
    container_name: container_name # ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„ (์„ ํƒ ์‚ฌํ•ญ)
    environment: # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •
      - VAR_NAME=value
    ports: # ํฌํŠธ ๋งคํ•‘
      - "host_port:container_port"
    volumes: # ๋ณผ๋ฅจ ๋งˆ์šดํŠธ
      - volume_name:/container/path
    networks: # ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ
      - network_name
    depends_on: # ์„œ๋น„์Šค ์˜์กด์„ฑ
      - other_service

networks: # ๋„คํŠธ์›Œํฌ ์ •์˜
  network_name:
    driver: bridge

volumes: # ๋ณผ๋ฅจ ์ •์˜
  volume_name:
    driver: local

์ปจํ…Œ์ด๋„ˆ ๋นŒ๋“œ ๋ฐ ์‹คํ–‰

Compose ํŒŒ์ผ์„ ์ž‘์„ฑํ•œ ํ›„, ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋ชจ๋“  ์„œ๋น„์Šค๋ฅผ ๋นŒ๋“œํ•˜๊ณ  ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

docker compose up

๋ฐฑ๊ทธ๋ผ์šด๋“œ ์‹คํ–‰

๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด -d ์˜ต์…˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

docker compose up -d

์ปจํ…Œ์ด๋„ˆ ๊ด€๋ฆฌ

Docker Compose๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์†์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์š” ๋ช…๋ น์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ์‹คํ–‰ ๋ฐ ๋นŒ๋“œ: docker compose up --build
  • ์ค‘์ง€ ๋ฐ ์‚ญ์ œ: docker compose down
  • ๋กœ๊ทธ ํ™•์ธ: docker compose logs -f
  • ํŠน์ • ์„œ๋น„์Šค ์‹คํ–‰: docker compose up -d service_name

Example) MySQL๊ณผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์—ฐ๋™ํ•˜๊ธฐ

Docker Compose๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ MySQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์—ฐ๋™ํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ์‹ค์Šตํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

ํ”„๋กœ์ ํŠธ ์ค€๋น„

๋จผ์ €, ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ•„์š”ํ•œ ํŒŒ์ผ์„ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค.

mkdir mbti-docker-compose
cd mbti-docker-compose

docker-compose.yaml ํŒŒ์ผ ์ž‘์„ฑ

์•„๋ž˜ ์˜ˆ์ œ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ MySQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ •์˜ํ•œ docker-compose.yaml ํŒŒ์ผ์ž…๋‹ˆ๋‹ค.

version: '3.8'

name: mbti # ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋””๋ ‰ํ† ๋ฆฌ ์ด๋ฆ„์œผ๋กœ ์ง€์ •๋จ

services:
  app: # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…Œ์ด๋„ˆ
    image: daehyunbigbread/mbti:mysql
    build:
      args:
        - NODE_VERSION=20.15.1
      dockerfile: ./Dockerfile
      pull: true
      context: .
    container_name: app
    environment:
      - PORT=3000
      - DB_HOST=db
      - DB_PORT=3306
      - DB_NAME=db_mbti
      - DB_USERNAME=user_mbti
      - DB_PASSWORD=pw_mbti
    networks:
      - mbti-net
    ports:
      - "4000:3000"
    depends_on:
      - db

  db: # MySQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ปจํ…Œ์ด๋„ˆ
    image: mysql:8.3.0
    container_name: db
    environment:
      - MYSQL_ROOT_PASSWORD=pw_root
      - MYSQL_DATABASE=db_mbti
      - MYSQL_USER=user_mbti
      - MYSQL_PASSWORD=pw_mbti
    networks:
      - mbti-net
    volumes:
      - mbti-vol:/var/lib/mysql

networks:
  mbti-net:
    name: mbti-net

volumes:
  mbti-vol:
    name: mbti-vol

Docker Compose๋กœ ์„œ๋น„์Šค ์‹คํ–‰

docker-compose.yaml ํŒŒ์ผ์ด ์œ„์น˜ํ•œ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์„œ๋น„์Šค๋ฅผ ๋นŒ๋“œํ•˜๊ณ  ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

docker compose up --build -d

์‹คํ–‰ ๊ฒฐ๊ณผ

[+] Building 2.1s (10/10) FINISHED
[+] Running 2/4
 โœ” Network mbti-net   Created
 โœ” Volume mbti-vol    Created
 โœ” Container db       Started
 โœ” Container app      Started

์„œ๋น„์Šค ๊ฐ„ ์˜์กด์„ฑ ์„ค์ •

 

Control startup order

How to control service startup and shutdown order in Docker Compose

docs.docker.com

 

Compose ํŒŒ์ผ์—์„œ depends_on ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋น„์Šค ๊ฐ„์˜ ์˜์กด์„ฑ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” app ์„œ๋น„์Šค๊ฐ€ db ์„œ๋น„์Šค์— ์˜์กดํ•˜๋„๋ก ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋Š” app ์„œ๋น„์Šค๊ฐ€ db ์„œ๋น„์Šค๊ฐ€ ์‹œ์ž‘๋œ ํ›„์— ์‹คํ–‰๋˜๋„๋ก ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.

์ฐธ๊ณ : depends_on์€ ์ปจํ…Œ์ด๋„ˆ์˜ ์‹œ์ž‘ ์ˆœ์„œ๋งŒ ๋ณด์žฅํ•˜๋ฉฐ, ์„œ๋น„์Šค๊ฐ€ ์™„์ „ํžˆ ์ค€๋น„๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค.
์„œ๋น„์Šค์˜ ์ค€๋น„ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด healthcheck๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
services:
  app:
    # ...
    depends_on:
      db:
        condition: service_healthy

  db:
    # ...
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 3

 


Docker Compose ์ฃผ์š” ๋ช…๋ น์–ด

Docker Compose๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์•Œ์•„๋‘๋ฉด ์ข‹์€ ์ฃผ์š” ๋ช…๋ น์–ด๋“ค์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค.
docker compose up Compose ํŒŒ์ผ์— ์ •์˜๋œ ๋ชจ๋“  ์„œ๋น„์Šค๋ฅผ ๋นŒ๋“œํ•˜๊ณ  ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
docker compose up -d ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์„œ๋น„์Šค๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
docker compose down ๋ชจ๋“  ์„œ๋น„์Šค๋ฅผ ์ค‘์ง€ํ•˜๊ณ  ์ปจํ…Œ์ด๋„ˆ, ๋„คํŠธ์›Œํฌ, ๋ณผ๋ฅจ์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
docker compose build Compose ํŒŒ์ผ์— ์ •์˜๋œ ๋ชจ๋“  ์„œ๋น„์Šค๋ฅผ ๋นŒ๋“œํ•ฉ๋‹ˆ๋‹ค.
docker compose logs ์„œ๋น„์Šค์˜ ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
docker compose restart ๋ชจ๋“  ์„œ๋น„์Šค๋ฅผ ์žฌ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
docker compose stop ๋ชจ๋“  ์„œ๋น„์Šค๋ฅผ ์ค‘์ง€ํ•ฉ๋‹ˆ๋‹ค.
docker compose start ์ค‘์ง€๋œ ์„œ๋น„์Šค๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
docker compose exec [service] ์‹คํ–‰ ์ค‘์ธ ์„œ๋น„์Šค์˜ ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์—์„œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
docker compose ps ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ ์„œ๋น„์Šค์˜ ์ƒํƒœ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
docker compose pull Compose ํŒŒ์ผ์— ์ •์˜๋œ ๋ชจ๋“  ์„œ๋น„์Šค์˜ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
docker compose push Compose ํŒŒ์ผ์— ์ •์˜๋œ ๋ชจ๋“  ์„œ๋น„์Šค์˜ ์ด๋ฏธ์ง€๋ฅผ ํ‘ธ์‹œํ•ฉ๋‹ˆ๋‹ค.

์ถ”๊ฐ€ ํŒ ๋ฐ ์ฃผ์˜์‚ฌํ•ญ

Docker Compose๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋ช‡ ๊ฐ€์ง€ ํŒ๊ณผ ์ฃผ์˜์‚ฌํ•ญ์„ ์•Œ์•„๋‘๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค.

1. ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ํŒŒ์ผ ํ™œ์šฉ

์—ฌ๋Ÿฌ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ๊ด€๋ฆฌํ•  ๋•Œ๋Š” .env ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ณ , Compose ํŒŒ์ผ์—์„œ ์ด๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • .env ํŒŒ์ผ ์˜ˆ์‹œ:
PORT=3000
DB_HOST=db
DB_PORT=3306
DB_NAME=db_mbti
DB_USERNAME=user_mbti
DB_PASSWORD=pw_mbti
MYSQL_ROOT_PASSWORD=pw_root
MYSQL_DATABASE=db_mbti
MYSQL_USER=user_mbti
MYSQL_PASSWORD=pw_mbti
  • docker-compose.yaml ํŒŒ์ผ์—์„œ ์ฐธ์กฐ:
environment:
  - PORT=${PORT}
  - DB_HOST=${DB_HOST}
  - DB_PORT=${DB_PORT}
  - DB_NAME=${DB_NAME}
  - DB_USERNAME=${DB_USERNAME}
  - DB_PASSWORD=${DB_PASSWORD}

2. ๋ณผ๋ฅจ๊ณผ ๋„คํŠธ์›Œํฌ ๊ด€๋ฆฌ

Compose ํŒŒ์ผ ๋‚ด์—์„œ ๋ณผ๋ฅจ๊ณผ ๋„คํŠธ์›Œํฌ๋ฅผ ์ •์˜ํ•˜์—ฌ, ์„œ๋น„์Šค ๊ฐ„์˜ ๋ฐ์ดํ„ฐ ๊ณต์œ ์™€ ๋„คํŠธ์›Œํฌ ํ†ต์‹ ์„ ์†์‰ฝ๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3. ์„œ๋น„์Šค ์Šค์ผ€์ผ๋ง

Compose ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋™์ผํ•œ ์„œ๋น„์Šค๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ๋กœ ์Šค์ผ€์ผ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์ผ ํ˜ธ์ŠคํŠธ ํ™˜๊ฒฝ์—์„œ๋งŒ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋ถ„์‚ฐ ํ™˜๊ฒฝ์—์„œ๋Š” Docker Swarm์ด๋‚˜ Kubernetes์™€ ๊ฐ™์€ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

docker compose up --scale app=3 -d

4. ๋นŒ๋“œ ์บ์‹œ ํ™œ์šฉ

์ด๋ฏธ์ง€๊ฐ€ ์ด๋ฏธ ๋นŒ๋“œ๋œ ๊ฒฝ์šฐ, docker compose build ๋ช…๋ น์–ด๋Š” ๋ณ€๊ฒฝ๋œ ๋ถ€๋ถ„๋งŒ ๋‹ค์‹œ ๋นŒ๋“œํ•˜์—ฌ ๋นŒ๋“œ ์‹œ๊ฐ„์„ ๋‹จ์ถ•์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

5. Healthcheck ์„ค์ •

์„œ๋น„์Šค๊ฐ€ ์™„์ „ํžˆ ์‹œ์ž‘๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด healthcheck๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์˜์กด์„ฑ์ด ์žˆ๋Š” ์„œ๋น„์Šค์˜ ์•ˆ์ •์ ์ธ ์‹œ์ž‘์„ ๋ณด์žฅํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค.

services:
  db:
    image: mysql:8.3.0
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 3

6. Compose ํŒŒ์ผ ๋ฒ„์ „ ๊ด€๋ฆฌ

Compose ํŒŒ์ผ์˜ ๋ฒ„์ „์„ ์ ์ ˆํžˆ ๊ด€๋ฆฌํ•˜์—ฌ Docker์˜ ์ตœ์‹  ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜๊ณ , ํ˜ธํ™˜์„ฑ์„ ์œ ์ง€ํ•˜์„ธ์š”. ํ˜„์žฌ๋Š” version: '3.8'์ด ๋งŽ์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.


์ฐธ๊ณ  ์ž๋ฃŒ