๐Ÿ’พ ๋„์ปค ์ปดํฌ์ฆˆ์˜ ๊ธฐ๋ณธ ๊ตฌ์„ฑ ์š”์†Œ๋ฃฐ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํ•ญ๋ชฉ๋งŒ ์ •๋ฆฌํ•˜์˜€๊ณ , ๋‹ค๋ฅธ ํ•ญ๋ชฉ๋“ค์„ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด docker-compose๋ฅผ ํ™•์ธํ•˜์„ธ์š”.


Docker compose๋ž€?

Docker Compose๋ฅผ ์ด์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ(container)๋กœ ๊ตฌ์„ฑ๋œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ํ•˜๋‚˜์˜ ํŒŒ์ผ(YAML)์— ์ •์˜ํ•ด๋†“๊ณ  ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด ํ•œ ๋ฒˆ์— ์˜ฌ๋ฆฌ๊ฑฐ๋‚˜ ๋‚ด๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Docker Compose๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœย docker-compose.ymlย ํŒŒ์ผ์„ ์„ค์ • ํŒŒ์ผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ๋ณธ ๊ตฌ์กฐ

docker-compose.ymlย ํŒŒ์ผ์€ ๋Œ€๋žต์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค.

version: "3.7"
services:
  web:
    # ์ปจํ…Œ์ด๋„ˆ ์„œ๋น„์Šค, ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค์ •
  db:
    # ์ปจํ…Œ์ด๋„ˆ ์„œ๋น„์Šค, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์ •
networks:
  # ๋„คํŠธ์›Œํฌ ์„ค์ •
volumes:
  # ๋ณผ๋ฅจ ์„ค์ •

Docker Compose์—์„œ ์„œ๋น„์Šค๋Š” ๋…๋ฆฝ๋œ ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋Œ์•„๊ฐ€๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ตฌ์„ฑ ์š”์†Œ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๋จผ์ € ํ”„๋กœ์ ํŠธ์—์„œ ๊ฐœ๋ฐœํ•˜๊ณ  ์žˆ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ž์ฒด๊ฐ€ ์„œ๋น„์Šค๊ฐ€ ๋  ๊ฒƒ์ด๊ณ , ๊ทธ ๋ฐ–์— ํ•ด๋‹น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์˜์กดํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋“ฑ๋„ ์„œ๋น„์Šค๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ๋ฉ”์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ •์ƒ์ ์œผ๋กœ ๊ตฌ๋™๋˜๊ธฐ ์œ„ํ•ด์„œ ํ•„์š”ํ•œ ๊ธฐ๋ฐ˜ ์‹œ์Šคํ…œ๊นŒ์ง€ Docker Compose๋กœ ์„ค์ •ํ•œ๋‹ค๊ณ  ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

.env ํŒŒ์ผ

docker-compose.yml ํŒŒ์ผ๊ณผ ๊ฐ™์€ ๊ฒฝ๋กœ์— .env ํŒŒ์ผ์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. .env ํŒŒ์ผ์— ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์ •์˜ ํ•˜๋ฉด docker-compose.yml ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# .env

MY_ENV="prod"
# docker-compose.yml
version: '3.7'
services:
  web:
    build:
      context: web/
      args:
        PROFILE: ${MY_ENV}





๊ตฌ์„ฑ ์š”์†Œ

docker-compose.yml์ž‘์„ฑ์‹œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํ•ญ๋ชฉ์„ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

build

buildย ํ•ญ๋ชฉ์€ ํ•ด๋‹น ์„œ๋น„์Šค์˜ ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๋กœ,ย Dockerfile์ด ์œ„์น˜ํ•˜๋Š” ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

## docker-compose.ymlย ํŒŒ์ผ๊ณผ ๋™์ผํ•œ ๋””๋ ‰ํ† ๋ฆฌ์— ์œ„์น˜ํ•œย Dockerfile ์„ ์‚ฌ์šฉํ•ด์„œ web ์„œ๋น„์Šค์˜ ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œ
version: '3.7'
services:
  web:
    build: .
## Dockerfile์ด ์•„๋‹Œ ๋‹ค๋ฅธ ์ด๋ฆ„์˜ ํŒŒ์ผ๋กœ ๋นŒ๋“œ๋ฅผ ํ•˜๊ณ  ์‹ถ๊ฑฐ๋‚˜, ๋นŒ๋“œ ์ธ์ž๋ฅผ ๋„˜๊ฒจ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ
version: '3.7'
services:
  web:
    build:
      context: ./app
      dockerfile: Dockerfile-dev
      args:
        arg1: "development"
	    arg2: "docker"



image

ํ”„๋กœ์ ํŠธ์—์„œ ์ง์ ‘ ๊ฐœ๋ฐœํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š”, ์ด๋ฏธ์ง€๋ฅผ ์ง์ ‘ ๋นŒ๋“œํ•˜๋Š” ๋Œ€์‹ ์— ์ด๋ฏธ์ง€ ์ €์žฅ์†Œ(repository)๋กœ ๋ถ€ํ„ฐ ์ด๋ฏธ์ง€๋ฅผ ๋‚ด๋ ค๋ฐ›์•„์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค.

# image ํ•ญ๋ชฉ์€ ์ด๋ฏธ์ง€ ์ €์žฅ์†Œ๋กœ ๋ถ€ํ„ฐ ๋‚ด๋ ค๋ฐ›์„ ์ด๋ฏธ์ง€์˜ ์ด๋ฆ„๊ณผ ํƒœ๊ทธ๋ฅผ ๋ช…์‹œํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋ฉ
version: '3.7'
services:
  db:
    image: postgres:13
  cache:
    image: redis



ports

portsย ํ•ญ๋ชฉ์€ ์™ธ๋ถ€๋กœ ๋…ธ์ถœ์‹œํ‚ฌ ํฌํŠธ์˜ ๋งตํ•‘์„ ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”์ธ๋“œ(bind)๊ฐ€ ํ•„์š”ํ•œ ํ˜ธ์ŠคํŠธ ์™ธ๋ถ€ ํฌํŠธ์™€ ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€ ํฌํŠธ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

services:
  web:
    ports:
      - "8000:8000"
  db:
    ports:
      - "5432:5432"



volumes

volumesย ํ•ญ๋ชฉ์€ ๋ณผ๋ฅจ ์„ค์ •์„ ์œ„ํ•ด ์“ฐ์ž…๋‹ˆ๋‹ค. ๋งˆ์šดํŠธ(mount)๊ฐ€ ํ•„์š”ํ•œ ํ˜ธ์ŠคํŠธ์˜ ๊ฒฝ๋กœ์™€ ์ปจํ…Œ์ด๋„ˆ์˜ ๊ฒฝ๋กœ๋ฅผ ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค.

# ํ˜ธ์ŠคํŠธ์˜ ๊ฒฝ๋กœ๋กœ ๋ณผ๋ฅจ ์„ค์ •
services:
  web:
    volumes:
      - .:/web
# ๋ฏธ๋ฆฌ ์ƒ์„ฑํ•œ docker volume์œผ๋กœ ๋ณผ๋ฅจ ์„ค์ •
services:
  frontend:
    image: node:lts
    volumes:
      - myapp:/home/node/app

volumes:
  myapp:
    external: true



depends_on

depends_on ํ•ญ๋ชฉ์€ ์„œ๋น„์Šค ๊ฐ„ ์˜์กด ๊ด€๊ณ„๋ฅผ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์˜ฌ๋ผ์˜ค๊ธฐ ์ „์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ๋ ˆ๋””์Šค ์„œ๋น„์Šค๊ฐ€ ๋จผ์ € ์˜ฌ๋ผ์™€์•ผ ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

version: "3.7"
services:
  web:
    build: .
    depends_on:
      - db
      - redis
  redis:
    image: redis
  db:
    image: postgres



command

commandย ํ•ญ๋ชฉ์€ ํ•ด๋‹น ์„œ๋น„์Šค๊ฐ€ ์˜ฌ๋ผ์˜ฌ ๋•Œย Dockerfile ์˜ย CMDย ๋ช…๋ น๋ฌธ์„ ๋ฌด์‹œํ•˜๊ณ  ์‹คํ–‰ํ•  ๋ช…๋ น์–ด๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

services:
  web:
    command: node .



environment

environmentย ํ•ญ๋ชฉ์€ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: wordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress
# ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
environment:
  - RACK_ENV=development
  - SHOW=true



๋ ˆํผ๋Ÿฐ์Šค ์ฐธ๊ณ 

docker-compose




๐Ÿ™‡๐Ÿปโ€โ™‚๏ธ ์ฐธ๊ณ ์‚ฌ์ดํŠธ

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ