JUST DO IT!

์™œ Docker-compose๋ฅผ ์‚ฌ์šฉํ• ๊นŒ? - TIL230615 ๋ณธ๋ฌธ

TIL

์™œ Docker-compose๋ฅผ ์‚ฌ์šฉํ• ๊นŒ? - TIL230615

sunhokimDev 2023. 6. 15. 23:07

๐Ÿ“š KDT WEEK 11 DAY 4 TIL

  • Docker-compose
  • ์‹ค์Šต 1 - Docker-compose ํŒŒ์ผ์ด ์—†์„ ๋•Œ
  • ์‹ค์Šต 2 - Docker-compose ํŒŒ์ผ์ด ์žˆ์„ ๋•Œ

 


 

๐ŸŸฅ Docker-compose

docker-compose.yaml : ๋‹ค์ˆ˜์˜ ๋„์ปค ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ํ™˜๊ฒฝ์„ค์ • ํŒŒ์ผ

๊ฐœ๋ณ„ Container๋ฅผ ๋”ฐ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํ›จ์”ฌ ๋” ์ƒ์‚ฐ์„ฑ์ด ๋†’๋‹ค.

ํŒŒ์ผ์— ๊ฐ ์ปจํ…Œ์ด๋„ˆ๋ณ„๋กœ ์ด๋ฆ„๊ณผ ํฌํŠธํฌ์›Œ๋”ฉ, ๋„์ปค ๋ณผ๋ฅจ, ํ™˜๊ฒฝ๊ณผ ๋„คํŠธ์›Œํฌ ๊ตฌ์„ฑ ๋“ฑ ํ•œ๋ฒˆ์— ๊ธฐ์ˆ ํ•ด๋’€๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ํŽธํ•˜๋‹ค.

 

๋”ฐ๋ผ์„œ ํ•œ๋งˆ๋””๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋„์ปค ์ปจํ…Œ์ด๋„ˆ๋กœ ๊ตฌ์„ฑ๋œ ํ•˜๋‚˜์˜ ์•ฑ์„ ๊ตฌ์„ฑํ•  ๋•Œ์—๋Š” ๊ต‰์žฅํžˆ ํŽธ๋ฆฌํ•œ ํŒŒ์ผ์ด๋‹ค.

๊ทธ ๋ถ€๋ถ„์„ ์‹ค์Šต์œผ๋กœ ์ง์ ‘ ๋น„๊ตํ•ด๋ณด๊ฒ ๋‹ค.

 

docker volume๋„ ๊ธฐ์ˆ ๋˜์–ด ์žˆ๋‹ค.

๋”๋ณด๊ธฐ

์ด์ „ ๊ธ€์—์„œ ๋ฐฐ์› ๋˜ ๋‚ด์šฉ์„ ์กฐ๊ธˆ ์ถ”๊ฐ€ํ•˜์ž๋ฉด,

์ด ํŒŒ์ผ์— docker volume ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ป๊ฒŒ ๋งˆ์šดํŠธ๋ฅผ ํ•  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ๋‚ด์šฉ๋„ ๊ธฐ์ˆ ๋˜์–ด ์žˆ๋‹ค.

 

docker-compose.yaml ํŒŒ์ผ์˜ ์ผ๋ถ€

docker-compose.yaml์„ ๋ณด๋ฉด, volumes: ํ•ญ๋ชฉ์—์„œ docker volume ๋งˆ์šดํŠธ์— ๋Œ€ํ•ด ์ ํ˜€์žˆ๋‹ค.

AIRFLOW_PROJ_DIR์€ airflow๊ฐ€ ์„ธํŒ…๋œ ํด๋”๋ฅผ ์ž๋™์œผ๋กœ ์ง€์นญํ•œ๋‹ค.


 

 

๋ช…๋ น์–ด

  • docker-compose build : build ํ‚ค๋กœ ์ง€์ •๋œ ์ด๋ฏธ์ง€๋“ค ๋Œ€์ƒ์œผ๋กœ ๋นŒ๋“œ
  • docker-compose pull : image ํ‚ค๋กœ ์ง€์ •๋œ ์ด๋ฏธ์ง€๋“ค ๋Œ€์ƒ์œผ๋กœ docker hub์—์„œ ๊ฐ€์ ธ์˜ค๊ธฐ
  • docker-compose images : docker-compose๋กœ ์‹คํ–‰๋œ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์–ด๋–ค ์ด๋ฏธ์ง€๋กœ ์‹คํ–‰๋˜์—ˆ๋Š”์ง€ ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅ
  • docker-compose push : ํ•œ๋ฒˆ์— docker hub๋กœ push(๊ถŒํ•œํ•„์š”)

 

  • docker-compose up : ์ด๋ฏธ์ง€๋ฅผ buildํ•˜๊ฑฐ๋‚˜ ๋ฐ›์•„์˜ค๊ณ , ๋„์ปค ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ์‹œ์ž‘ํ•œ๋‹ค.( build + pull + start )
  • docker-compose down : ์‹คํ–‰ํ•œ ๋ชจ๋“  ๋„์ปค ์ปจํ…Œ์ด๋„ˆ๋“ค์„ ํ•œ๋ฒˆ์— ๋ฉˆ์ถ”๊ณ (stop) ์‚ญ์ œ(rm)๊นŒ์ง€ ์ง„ํ–‰
  • docker-compose ls : docker-compose ๊ทธ๋ฃน๋ณ„๋กœ ๋ฆฌ์ŠคํŠธ
  • docker-compose ps : docker-compose๋กœ ์‹คํ–‰๋œ docker container๋“ค์˜ ์ƒํƒœ ์ถœ๋ ฅ

 

 

๐ŸŸฆ ๋‹ค์ˆ˜์˜ Container๋กœ ๊ตฌ์„ฑ๋œ ์˜ˆ์ œ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ํ•ด๋ณด๊ธฐ

๋ณธ ์‹ค์Šต์€ ๋„์ปค ์˜ˆ์ œ๋กœ ์ œ๊ณต๋˜๋Š” vote-application์„ ํ† ๋Œ€๋กœ ์‹ค์Šตํ•˜์˜€๋‹ค.

https://github.com/dockersamples/example-voting-app

 

GitHub - dockersamples/example-voting-app: Example distributed app composed of multiple containers for Docker, Compose, Swarm, a

Example distributed app composed of multiple containers for Docker, Compose, Swarm, and Kubernetes - GitHub - dockersamples/example-voting-app: Example distributed app composed of multiple containe...

github.com

 

์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๊ธฐ ์ „์— ๋„์ปค ์ปจํ…Œ์ด๋„ˆ์™€ ์ด๋ฏธ์ง€๋ฅผ ์ •๋ฆฌํ•ด๋‘๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค.

 

# ๋„์ปค ํด๋ฆฐ์—…
docker container rm -f $(docker container ls -aq)
docker image rm -f $(docker image ls -q)

 

์œ„ ์˜ˆ์ œ ํ”„๋กœ๊ทธ๋žจ์— ๋Œ€ํ•ด ๊ฐ„๋žตํ•˜๊ฒŒ ์„ค๋ช…ํ•˜์ž๋ฉด, ์ด 5๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๋กœ ๊ตฌ์„ฑ๋˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค.

vote, result, worker, redis, postgres

vote์™€ result๋Š” front-end๋กœ์จ ํˆฌํ‘œ ํ™”๋ฉด๊ณผ ํˆฌํ‘œ ๊ฒฐ๊ณผํ™”๋ฉด์„ ์ œ๊ณตํ•˜๊ณ , postgres๋Š” ํˆฌํ‘œ ๊ฒฐ๊ณผ DB ์—ญํ• ์„ ํ•œ๋‹ค.

vote > redis > worker > postgres > result ์ˆœ์„œ๋กœ ๋™์ž‘ํ•œ๋‹ค๊ณ  ๋ณด๋ฉด ์‰ฝ๋‹ค.

 

์ผ๋‹จ ๋‹ค์„ฏ๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ•˜๋‚˜๋กœ ๊ตฌ์„ฑํ•ด์ค„ ๋„คํŠธ์›Œํฌ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

docker network create mynetwork

mynetwork ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋„คํŠธ์›Œํฌ๋ฅผ ํ•˜๋‚˜ ๊ตฌ์„ฑํ–ˆ๋‹ค.

์ด ๋„คํŠธ์›Œํฌ๋ฅผ ์–ธ๊ธ‰ํ•œ ๋‹ค์„ฏ๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ runํ•˜๊ฒŒ ๋  ๋•Œ ์˜ต์…˜์œผ๋กœ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

๋‹ค์„ฏ๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ๋‹ค์„ฏ๋ฒˆ์˜ docker run์ด ํ•„์š”ํ•˜๋‹ค.

# Docker file์˜ ์œ„์น˜๋ฅผ ์•Œ๋ ค ๋„์ปค๋ฅผ ๋นŒ๋“œ
docker build -t vote ./vote
docker build -t result ./result
docker build -t worker ./worker

docker run -d --name=vote -p 5001:80 --network mynetwork vote
docker run -d --name=result -p 5002:80 --network mynetwork result
docker run -d --name=worker --network mynetwork worker

# ๊ณต์‹ ์ด๋ฏธ์ง€์˜ ๊ฒฝ์šฐ run์„ ํ†ตํ•ด ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ
docker run -d --name=redis --network mynetwork redis
docker run --name=db -e POSTGRES_PASSWORD=password --network mynetwork postgres

postgres์˜ ๊ฒฝ์šฐ ์‹คํ–‰ํ•˜๋ฉด์„œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์„ค์ •์ด ํ•„์š”ํ•˜๋ฏ€๋กœ ์กฐ๊ธˆ ๋ช…๋ น์–ด๊ฐ€ ๋‹ค๋ฅด๋‹ค.

๋‚˜๋Š” vote๋ฅผ 5001 ํฌํŠธ๋กœ, result๋ฅผ 5002 ํฌํŠธ๋กœ ์—ด์–ด๋‘์—ˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ ์ž์œ ๋‹ค.

 

์‹คํ–‰์ด ์™„๋ฃŒ๋˜๋ฉด postgres ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ตฌ๋™๋˜๊ณ  ์žˆ์„ ๊ฒƒ์ด๋‹ค.

์ƒˆ๋กœ์šด ํ„ฐ๋ฏธ๋„์„ ์—ด์–ด docker ps๋ฅผ ํ•ด๋ณด๋ฉด 5๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์—ด๋ ค์žˆ์–ด์•ผ ํ•œ๋‹ค.

 

์–ธ๊ธ‰ํ–ˆ๋˜ 5๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์—ด๋ ค์žˆ๋‹ค.

 

์ด์ œ ์—ด์—ˆ๋˜ ํฌํŠธ๋ฅผ ํ†ตํ•ด ๋กœ์ปฌํ˜ธ์ŠคํŠธ๋กœ ์ ‘์†ํ•ด๋ณด์ž.

 

vote ํ™”๋ฉด

 

localhost:5001์— ์ ‘์†ํ•˜๋ฉด vote์— ์ ‘์†๋˜๊ณ , ํด๋ฆญํ•ด์„œ ํˆฌํ‘œ๋„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

localhost:5002์— ์ ‘์†ํ•˜๋ฉด result ํ™”๋ฉด์ด ๋ณด์ด๊ณ , ํˆฌํ‘œ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค.

 

ํ•˜์ง€๋งŒ ํˆฌํ‘œ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ”๋กœ๋ฐ”๋กœ ํˆฌํ‘œ ๊ฒฐ๊ณผ์— ๋ฐ˜์˜๋˜์ง€๋Š” ์•Š๋Š”๋‹ค.

์™œ๋ƒํ•˜๋ฉด postgres ์ถ”๊ฐ€์„ค์ •์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.. ์ด ๋ถ€๋ถ„์ด ์ง€๊ธˆ ์‹ค์Šต์—์„œ ์ค‘์š”ํ•œ ๊ฑด ์•„๋‹ˆ๋ฏ€๋กœ ๋„˜์–ด๊ฐ€๊ฒ ๋‹ค.

 

 


 

๐ŸŸฉ ์ด๋ฒˆ์—๋Š” docker-compose๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์—ฐ๊ฒฐํ•ด๋ณด๊ธฐ

 

์˜ˆ์ œ ํ”„๋กœ๊ทธ๋žจ์—๋„ docker-compose ํŒŒ์ผ์„ ์ œ๊ณตํ•˜์ง€๋งŒ, ์ƒˆ๋กœ์šด docker-compose ํŒŒ์ผ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌ์„ฑํ•˜์˜€๋‹ค.

 

services:
  vote:
    build: ./vote
    # use python rather than gunicorn for local dev
    command: python app.py
    ports:
      - "5001:80"

  result:
    build: ./result
    # use nodemon rather than node for local dev
    entrypoint: nodemon server.js
    ports:
      - "5002:80"

  worker:
    build: ./worker

  redis:
    image: redis:alpine

  db:
    image: postgres:15-alpine
    environment:
      POSTGRES_USER: "postgres"
      POSTGRES_PASSWORD: "postgres"

docker-compose ํŒŒ์ผ์€ services: ๋ฐ‘์œผ๋กœ ๊ฐ๊ฐ์˜ ๊ตฌ์„ฑํ•  ์ปจํ…Œ์ด๋„ˆ๋“ค์„ ์ž‘์„ฑํ•˜๊ฒŒ ๋œ๋‹ค.

vote, result, worker, redis, db(postgres)๋กœ ์•„๊นŒ์™€ ๊ฐ™์ด 5๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 

vote, result, worker๋Š” build: ๋ฅผ ํ†ตํ•ด ๊ฐ๊ฐ์˜ docker file ์œ„์น˜๋ฅผ ์•Œ๋ฆฐ๋‹ค.

vote์™€ result๋Š” ํฌํŠธํฌ์›Œ๋”ฉ์ด ํ•„์š”ํ–ˆ์œผ๋‹ˆ port: ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

command: ์™€ entrypoint: ๋Š” docker file์— ์ž‘์„ฑ๋œ CMD์™€ ENTRYPOINT๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 

redise์™€ db๋Š” ๊ณต์‹ ์ด๋ฏธ์ง€์ด๋ฏ€๋กœ image: ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์–ด๋–ค ์ด๋ฏธ์ง€๋ฅผ docker hub์—์„œ ๋ฐ›์•„์˜ฌ์ง€ ์ž‘์„ฑํ•˜๊ณ ,

postgres์˜ ๊ฒฝ์šฐ ํ™˜๊ฒฝ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ–ˆ์œผ๋‹ˆ environment: ๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ์‚ฌํ•ญ์„ ์ž‘์„ฑํ•œ๋‹ค.

 

์ด๋Œ€๋กœ ์˜ˆ์ œ ํ”„๋กœ๊ทธ๋žจ ํด๋”์— docker-compose.mac.yml ํŒŒ์ผ๋กœ ์ €์žฅํ•˜์˜€๋‹ค.

์ด์ œ๋Š” ์‹คํ–‰ํ•˜๋Š” ์ผ๋งŒ ๋‚จ์•˜๋‹ค!

 

 

์ผ๋‹จ ์•„๊นŒ ์‹ค์Šตํ–ˆ๋˜ ๋‚ด์šฉ์ด ์žˆ๋‹ค๋ฉด ์ปจํ…Œ์ด๋„ˆ์™€ ์ด๋ฏธ์ง€ ์ฒญ์†Œ๋ฅผ ํ•ด๋‘์ž.

 

# ๋„์ปค ํด๋ฆฐ์—…
docker container rm -f $(docker container ls -aq)
docker image rm -f $(docker image ls -q)

 

๊ทธ๋ฆฌ๊ณ  ์˜ˆ์ œ ํ”„๋กœ๊ทธ๋žจ ํด๋”์—์„œ ๋‹ค์Œ์˜ ๋ช…๋ น์–ด๋งŒ ์ž…๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

# docker-compose up
docker-compose -f docker-compose.mac.yml up

 

ํŒŒ์ผ์˜ ์ด๋ฆ„์ด docker-compose.yml ์ด๊ฑฐ๋‚˜ docker-compose.yaml ์ด๋ผ๋ฉด docker-compose up ์œผ๋กœ ์ถฉ๋ถ„ํ•˜๋‹ค.

ํ•˜์ง€๋งŒ ๋‚˜๋Š” ์ƒˆ๋กœ์šด docker-compose ํŒŒ์ผ์„ ๋„ฃ๊ณ  ์ด๋ฆ„๋„ ๋ฐ”๊ฟง์œผ๋‹ˆ ์œ„์˜ ๋ช…๋ น์–ด์ฒ˜๋Ÿผ, -f ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด ์ด๋ฆ„์„ ์•Œ๋ ค์•ผํ•œ๋‹ค.

 

๋ฐ”๋กœ ๋กœ์ปฌํ˜ธ์ŠคํŠธ๋กœ ์ ‘์†ํ•ด์„œ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.

 

์ž˜ ๋™์ž‘ํ•œ๋‹ค!

 

์ฒซ ๋ฒˆ์งธ ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ network๊ฐ€ ์ƒ๋žต๋˜๋„ ๋™์ž‘ํ•˜๋Š” ์ด์œ ๋Š”, docker-compose์—์„œ๋Š” ์ž๋™์œผ๋กœ ํ•˜๋‚˜์˜ ๋„คํŠธ์›Œํฌ๋กœ ๋ฌถ์–ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

services์— ์ค€ ์ด๋ฆ„์œผ๋กœ ํ˜ธ์ŠคํŠธ ์ด๋ฆ„์ด ์ƒ์„ฑ๋˜์–ด ์ด๋ฆ„์„ ๋‚ด๋ถ€ IP๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ์–ด, docker๋ผ๋ฆฌ ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ๋œ๋‹ค.

 

โš™๏ธ docker-compose.yml ๋‚ด์šฉ ์•Œ์•„๋ณด๊ธฐ

 

0. docker-compose.yml ํŒŒ์ผ์—์„œ airflow ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ค์ •ํ•˜๊ธฐ

x-airflow-common: 
    &airflow-common 
    image: ${AIRFLOW_IMAGE_NAME:-apache/airflow:2.5.1}
    environment:   
        &airflow-common-env   
        AIRFLOW_VAR_DATA_DIR: /opt/airflow/data     
        AIRFLOW__API__AUTH_BACKENDS: 'airflow.api.auth.backend.basic_auth,airflow.api.auth.backend.session'

 

์œ„ ๋‚ด์šฉ์€ ๋‚ด airflow์˜ docker-compose.yml ๋‚ด์šฉ ์ผ๋ถ€์ด๋‹ค.

 

environment์— ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ํฌ๊ฒŒ AIRFLOW ๋’ค์˜ ์–ธ๋”๋ฐ” ๊ฐœ์ˆ˜๋กœ ๊ตฌ๋ถ„๋œ๋‹ค.

  • AIRFLOW_ : airflow์˜ variables ๊ฐ™์€ ๋ณ€์ˆ˜๋ฅผ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด๋‘˜ ์ˆ˜ ์žˆ๋‹ค.
    • AIRFLOW_VAR_๋ณ€์ˆ˜์ด๋ฆ„ : variables์— : ๋’ค์— ํ•ด๋‹นํ•˜๋Š” ๋‚ด์šฉ์„ ๋ณ€์ˆ˜์ด๋ฆ„์— ์ €์žฅํ•œ๋‹ค.
  • AIRFLOW__ : airflow.cfg ์˜ ํ•ด๋‹น ์„น์…˜์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
    • AIRFLOW__API__AUTH_BACKENDS : airflow.cfg์˜ api ์„น์…˜์— ํ•ด๋‹นํ•˜๋Š” auth_backend ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.

airflow.cfg์˜ ๊ฒฝ์šฐ AIRFLOW ๋’ค์˜ ์–ธ๋”๋ฐ”๊ฐ€ ๋‘ ๊ฐœ, ํ•ด๋‹น ์„น์…˜ ๋‹ค์Œ์œผ๋กœ ์–ธ๋”๋ฐ”๊ฐ€ ๋‘ ๊ฐœ์ธ ๊ฒƒ์„ ์ฃผ์˜ํ•ด์•ผํ•œ๋‹ค.

์ด ํ™˜๊ฒฝ๋ณ€์ˆ˜๋“ค์€ docker compose up์„ ํ•˜๋ฉฐ ์ž๋™์œผ๋กœ ์„ค์ •๋˜์ง€๋งŒ, Airflow ์›น UI์™€ ๊ฐ™์€ ์™ธ๋ถ€์—์„œ ํ‘œ์ถœ๋˜์ง„ ์•Š๋Š”๋‹ค.

 

 

 

1. build: ๋นŒ๋“œ ๊ด€๋ จ ์ •๋ณด๋ฅผ ๋„˜๊ธด๋‹ค.

 

ex)

worker:
    build: ./worker
worker:
    build:
        context: ./woker
        dockerfile : Dockerfile.dev
        arg:
            ENVIRONMENT: development
    

 

  • context: ๋„์ปค ํŒŒ์ผ์˜ ์œ„์น˜๋ฅผ ๋„˜๊ฒจ ๋นŒ๋“œ (์ฒซ ๋ฒˆ์งธ ์˜ˆ์‹œ์ฒ˜๋Ÿผ ์ค„์—ฌ์“ฐ๊ธฐ๋„ ํ•จ)
  • dockerfile: ๋„์ปคํŒŒ์ผ์˜ ์ด๋ฆ„(Dockerfile.yml์ด ์•„๋‹Œ ๋‹ค๋ฅธ ์ด๋ฆ„์ผ ๋•Œ ์‚ฌ์šฉ)
  • args: ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋„˜๊ธฐ๊ธฐ

 

2. depends_on: ์–ด๋–ค ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋™์ž‘ํ•œ ํ›„์— ํ•ด๋‹น ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ๋ช…๋ น

 

ex)

worker:
    depends_on:
        db:
            conditions: service_healthy

 

condition์œผ๋กœ ๊ฐ€๋Šฅ ํ•œ ๊ฐ’

  • service_started : ๊ทธ ์„œ๋น„์Šค๊ฐ€ ์‹œ์ž‘ํ•œ ๋’ค
  • service_healthy : ๊ทธ ์„œ๋น„์Šค์˜ ์ƒํƒœ๊ฐ€ ์ •์ƒ์ผ ๋•Œ ( healthcheck ์˜ต์…˜์— ์˜ํ•ด)
  • service_completed_successfully : ๊ทธ ์„œ๋น„์Šค๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ข…๋ฃŒ๋œ ํ›„์—

 

3. environment: ์ปจํ…Œ์ด๋„ˆ ์•ˆ์—์„œ ์‹คํ–‰๋  ๋•Œ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋“ค์„ ์ง€์ • ( ๋„์ปคํŒŒ์ผ์˜ env)

4. networks : ํŠน์ • ๋„คํŠธ์›Œํฌ๋กœ ๋ฌถ๊ณ  ์‹ถ์„ ๋•Œ ์ง€์ •, ๋„คํŠธ์›Œํฌ๋Š” service:์™€ ๊ฐ™์€ ๋‹จ์—์„œ networks:๋กœ ์„ ์–ธํ•œ๋‹ค.

5. volumes: ๋„์ปค ๋ณผ๋ฅจ ์ง€์ •, : ์„ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ์— ์ด๋ฆ„์„, ์˜ค๋ฅธ์ชฝ์— ์ €์žฅํ•˜๊ณ  ์‹ถ์€ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•œ๋‹ค.

 

ex)

db:
    enviroment:
        POSTGRES_USER:"postgres"
        POSTGRES_PASSWORD:"postgres"
    volumes:
        -"db-data:/var/lib/postgresql/data"
    healthcheck:
        test: /healthchecks/postgres.sh
        interval: "5s"
    networks:
        - back-tier

 

์—ฌ๋Ÿฌ ์„œ๋น„์Šค์—์„œ ๊ณต์œ ํ•˜๋Š” ๊ณตํ†ต ๊ตฌ์„ฑ์„ ๋งŒ๋“ค ๋•Œ:

ex) airflow์˜ ๊ฒฝ์šฐ

x-airflow-common:
    &airflow-common
    image: ${AIRFLOW_IMAGE_NAME:-apache/airflow:2.5.1}
    environment:
        &airflow-common-env
        AIRFLOW__CORE__EXECUTOR: CeleryExecutor
        …
        _PIP_ADDITIONAL_REQUIREMENTS: ${_PIP_ADDITIONAL_REQUIREMENTS:-}
    volumes:
        - ${AIRFLOW_PROJ_DIR:-.}/dags:/opt/airflow/dags
        - ${AIRFLOW_PROJ_DIR:-.}/logs:/opt/airflow/logs
        - ${AIRFLOW_PROJ_DIR:-.}/plugins:/opt/airflow/plugins
    user: "${AIRFLOW_UID:-50000}:0"
    depends_on:
        &airflow-common-depends-on
        redis:
            condition: service_healthy
        postgres:
            condition: service_healthy

x-airflow-common : ์—ฌ๋Ÿฌ ์„œ๋น„์Šค์—์„œ ๊ณต์œ ํ•˜๋Š” ๊ณตํ†ต ๊ตฌ์„ฑ์„ ์ •์˜ํ•œ๋‹ค, ๋ณดํ†ต anchor๋ผ๊ณ  ๋ถ€๋ฆ„

&airflow-common :๋ณ„์นญ ์ง€์ •, ๋‹ค๋ฅธ ์„œ๋น„์Šค์—์„œ airflow-common์„ ๋ถˆ๋Ÿฌ์˜ด์œผ๋กœ์จ ๊ณตํ†ต ๊ตฌ์„ฑ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ

 

ex)

airflow-scheduer:
    <<: *airflow-common : airflwo-common