Docker-compose

Docker Compose

  • Docker Compose๋ž€?

  • ์ฃผ์š” ๋ช…๋ น์–ด

  • Docker Compose ๊ตฌ์„ฑํ•ด๋ณด๊ธฐ

  • Docker Compose Environment

  • Compose Network ์„ค์ • ์˜ต์…˜

  • docker Compose Version๋ณ„ yml ๋ณ€์ˆ˜

  • ๋ฒ„์ „๋ณ„ ํŠน์ง• ๋ฐ ๊ตฌ์„ฑ

Docker Compose๋ž€?

Docker Compose๋Š” ๋‹ค์ค‘ ์ปจํ…Œ์ด๋„ˆ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ •์˜ํ•˜๊ณ  ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐœ๋ฐœ๋œ ๋„๊ตฌ๋กœ

YAML ํŒŒ์ผ์„ ํ†ตํ•˜์—ฌ ๋‹จ์ผ ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋‘ ์‹คํ–‰ํ•˜๊ฑฐ๋‚˜ ๋ชจ๋‘ ์ข…๋ฃŒ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋ฉฐ

YAML ํŒŒ์ผ์— ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์Šคํƒ์„ ์ •์˜ํ•˜๊ณ  ํ”„๋กœ์ ํŠธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ๋ฃจํŠธ์— ํŒŒ์ผ์„ ์ €์žฅํ•จ์œผ๋กœ

๋‹ค๋ฅธ ์‚ฌ์šฉ์ž๊ฐ€ Repository๋ฅผ ๋ณต์ œํ•˜๊ณ  Compose ์‹คํ–‰๋งŒ์œผ๋กœ ๊ตฌ๋™์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

\

๋‹จ์ผ ํ˜ธ์ŠคํŠธ์˜ ์—ฌ๋Ÿฌ ๊ฒฉ๋ฆฌ ๋œ ํ™˜๊ฒฝ

Compose๋Š” ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ ํ™˜๊ฒฝ์„ ์„œ๋กœ ๊ฒฉ๋ฆฌํ•˜๊ณ  ์—ฌ๋Ÿฌ ๋‹ค๋ฅธ ์ปจํ…์ŠคํŠธ์—์„œ ์ด ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ ์ ‘๊ทผํ•œ๋‹ค.

์‹คํ–‰์‹œ -p ์˜ต์…˜์„ ํ†ตํ•˜์—ฌ ํ”„๋กœ์ ํŠธ ์ด๋ฆ„ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ Default ๊ฐ’์€ ํ”„๋กœ์ ํŠธ ํด๋” ์ด๋ฆ„์ด ๋œ๋‹ค.

docker-dompose up -p {ํ”„๋กœ์ ํŠธ ์ด๋ฆ„}

์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ์‹œ ๋ณผ๋ฅจ ๋ฐ์ดํ„ฐ ๋ณด์กด

์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ์‹œ ๋ณผ๋ฅจ ๋ฐ์ดํ„ฐ ๋ณด์กดํ•˜์—ฌ ๋ฐ์ดํ„ฐ๊ฐ€ ํœ˜๋ฐœ๋˜์ง€ ์•Š๋„๋ก ์ฒ˜๋ฆฌํ•œ๋‹ค.

\

๋ณ€๊ฒฝ๋œ ์ปจํ…Œ์ด๋„ˆ ๋งŒ ์žฌ์ƒ์„ฑ

์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ตฌ์„ฑ์„ ์บ์‹œํ•˜์—ฌ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์€ ์„œ๋น„์Šค๋ฅผ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๋ฉด Compose๋Š” ๊ธฐ์กด ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋‹ค์‹œ ์‚ฌ์šฉํ•œ๋‹ค.

\

๋ณ€์ˆ˜ ๋ฐ ํ™˜๊ฒฝ ๊ฐ„ ๊ตฌ์„ฑ ์ด๋™

Compose ํŒŒ์ผ์˜ ๋ณ€์ˆ˜๋ฅผ ์ง€์›

๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ ํ™˜๊ฒฝ ๋˜๋Š” ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž์— ๋งž๊ฒŒ ์ปดํฌ์ง€์…˜ ์ปค์Šคํ…€์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

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

up -d

docker-compose.yml ํŒŒ์ผ์˜ ๋‚ด์šฉ์— ๋”ฐ๋ผ ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ•˜๊ณ  ์„œ๋น„์Šค๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

up์œผ๋กœ compose๋ฅผ ์‹คํ–‰์‹œ ๋‹จ๊ณ„๋ณ„ ์ง„ํ–‰์‚ฌํ•ญ

  1. ์„œ๋น„์Šค๋ฅผ ๋„์šธ ๋„คํŠธ์›Œํฌ ์„ค์ •

  2. ํ•„์š”ํ•œ ๋ณผ๋ฅจ ์ƒ์„ฑ(ํ˜น์€ ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ณผ๋ฅจ๊ณผ ์—ฐ๊ฒฐ)

  3. ํ•„์š”ํ•œ ์ด๋ฏธ์ง€ ํ’€(pull)

  4. ํ•„์š”ํ•œ ์ด๋ฏธ์ง€ ๋นŒ๋“œ(build)

  5. ์„œ๋น„์Šค ์˜์กด์„ฑ์— ๋”ฐ๋ผ ์„œ๋น„์Šค ์‹คํ–‰

options

  • -d: ์„œ๋น„์Šค ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ์‹คํ–‰. (docker run์—์„œ์˜ -d์™€ ๊ฐ™์Œ)

  • --force-recreate: ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ง€์šฐ๊ณ  ์ƒˆ๋กœ ์ƒ์„ฑ.

  • --build: ์„œ๋น„์Šค ์‹œ์ž‘ ์ „ ์ด๋ฏธ์ง€๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑ

ps

ํ˜„์žฌ ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰ ์ค‘์ธ ๊ฐ ์„œ๋น„์Šค์˜ ์ƒํƒœ๋ฅผ ํ‘œ์‹œํ•œ๋‹ค.

   Name                  Command               State           Ports
---------------------------------------------------------------------------
tj_app_1     docker-entrypoint.sh sh -c ...   Exit 1
tj_mysql_1   docker-entrypoint.sh mysqld      Up       3306/tcp, 33060/tcp

stop, start

์„œ๋น„์Šค๋ฅผ ๋ฉˆ์ถ”๊ฑฐ๋‚˜, ๋ฉˆ์ถฐ ์žˆ๋Š” ์„œ๋น„์Šค๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค.

$ docker-compose stop
Stopping tj_mysql_1 ... done

$ docker-compose start
Starting app   ... done
Starting mysql ... done

down

์„œ๋น„์Šค๋ฅผ ์‚ญ์ œํ•œ๋‹ค. ์ปจํ…Œ์ด๋„ˆ์™€ ๋„คํŠธ์›Œํฌ๋ฅผ ์‚ญ์ œํ•˜๋ฉฐ, ์˜ต์…˜์— ๋”ฐ๋ผ ๋ณผ๋ฅจ๋„ ์‚ญ์ œํ•œ๋‹ค.

$ docker-compose down
Stopping tj_mysql_1 ... done
Removing tj_app_1   ... done
Removing tj_mysql_1 ... done
Removing network tj_default

options

  • --volume: ๋ณผ๋ฅจ๊นŒ์ง€ ๊ฐ™์ด ์‚ญ์ œ (DB ๋ฐ์ดํ„ฐ ์ดˆ๊ธฐํ™” ํ•˜๋Š”๋ฐ ์šฉ์ดํ•จ)

exec

์‹คํ–‰ ์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

$ docker-compose exec django ./manage.py makemigrations

$ docker-compose exec db psql postgres postgres

run

docker run ๊ณผ ๋งˆ์ฐฌ ๊ฐ€์ง€๋กœ ํŠน์ • ๋ช…๋ น์–ด๋ฅผ ์ผํšŒ์„ฑ์œผ๋กœ ์‹คํ–‰ํ•œ๋‹ค. exec๋Š” ํ”„๋กœ์„ธ์„œ๋ฅผ ์‹คํ–‰์‹œ์ผœ๋†“์„๋•Œ ์‚ฌ์šฉ๋˜๊ณ  run์€ batch์„ฑ ์ž‘์—…์— ์‚ฌ์šฉ ํŠนํ™” ๋œ๊ฒƒ์œผ๋กœ ๋ณด์—ฌ์ง„๋‹ค.

options

  • --rm : ํ•ด๋‹น ๋ช…๋ น์–ด๊ฐ€ ์ข…๋ฃŒ๋œ ๋’ค ์ปจํ…Œ์ด๋„ˆ๋„ ์ข…๋ฃŒ

logs

output์œผ๋กœ ๋‚˜์˜จ log๋“ค์„ ํ™•์ธ ํ• ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. docker์˜ logs์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ --follow, -f๋ฅผ ํ•˜์—ฌ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋‚˜์˜ค๋Š” ๋กœ๊ทธ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

$ docker-compose logs -f
Attaching to docker_app_1, docker_mysql_1
app_1    | yarn install v1.22.5
app_1    | info No lockfile found.
app_1    | [1/4] Resolving packages...
app_1    | [2/4] Fetching packages...
app_1    | [3/4] Linking dependencies...
app_1    | [4/4] Building fresh packages...
app_1    | success Saved lockfile.
app_1    | Done in 0.14s.
app_1    | yarn run v1.22.5
app_1    | error Couldn't find a package.json file in "/app"
app_1    | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
mysql_1  | 2021-01-05 04:43:03+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.32-1debian10 started.

options

  • -f, --follow: ์‹ค์‹œ๊ฐ„ ๋กœ๊ทธ ์ถœ๋ ฅ

config

docker-compose์— ์ตœ์ข…์ ์œผ๋กœ ์ ์šฉ๋œ ์„ค์ •์„ ๋ณด์—ฌ์ค€๋‹ค. -f๋ฅผ ์ด์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์„ค์ •ํŒŒ์ผ์„ ๋„์› ์„๋•Œ ํ™•์ธ์— ์šฉ์ดํ•จ

$ docker-compose config
services:
  app:
    command: sh -c "yarn install && yarn run dev"
    environment:
      MYSQL_DB: todos
      MYSQL_HOST: mysql
      MYSQL_PASSWORD: secret
      MYSQL_USER: root
    image: node:12-alpine
    ports:
    - published: 3000
      target: 3000
    volumes:
    - /Users/tj/project/tj/tj-msa/docker:/app:rw
    working_dir: /app
  mysql:
    environment:
      MYSQL_DATABASE: todos
      MYSQL_ROOT_PASSWORD: secret
    image: mysql:5.7
    volumes:
    - todo-mysql-data:/var/lib/mysql:rw
version: '3.8'
volumes:
  todo-mysql-data: {}

Docker Compose ๊ตฌ์„ฑํ•ด๋ณด๊ธฐ.

docker compose ํŒŒ์ผ์€ ํฌ๊ฒŒ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค.

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

\

docker-compose version ํ™•์ธ ๋งํฌ

https://docs.docker.com/compose/compose-file/

์„œ๋น„์Šค ์ •์˜

App Service ์ •์˜

docker run -dp 3000:3000 \
  -w /app -v ${PWD}:/app \
  --network todo-app \
  -e MYSQL_HOST=mysql \
  -e MYSQL_USER=root \
  -e MYSQL_PASSWORD=secret \
  -e MYSQL_DB=todos \
  node:12-alpine \
  sh -c "yarn install && yarn run dev"

์ƒ๋‹จ์˜ Docker์‹คํ–‰๋ช…๋ น์–ด๋ฅผ ์•„๋ž˜์ฒ˜๋Ÿผ Docker Compose๋กœ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

version: "3.8"

services:
  app:
    image: node:12-alpine
    command: sh -c "yarn install && yarn run dev"
    ports:
      - 3000:3000
    working_dir: /app
    volumes:
      - ./:/app
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: root
      MYSQL_PASSWORD: secret
      MYSQL_DB: todos 

Compose ์žฅ์ ์œผ๋กœ๋Š” ์ƒ๋Œ€๊ฒฝ๋กœ ์ž…๋ ฅ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ yaml๋กœ ๊ตฌ์„ฑํ•˜์—ฌ ์„ค์ •ํŒŒ์ผ์˜ ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์ง„๋‹ค.

MySQL ์„œ๋น„์Šค ์ •์˜

docker run -d \
  --network todo-app --network-alias mysql \
  -v todo-mysql-data:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=secret \
  -e MYSQL_DATABASE=todos \
  mysql:5.7
version: "3.8"

services:
  mysql:
    image: mysql:5.7
    volumes:
      - todo-mysql-data:/var/lib/mysql
    environment: 
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: todos

volumes:
  todo-mysql-data:

์ตœ์ข…๋ณธ

version: "3.8"

services:
  app:
    image: node:12-alpine
    command: sh -c "yarn install && yarn run dev"
    ports:
      - 3000:3000
    working_dir: /app
    volumes:
      - ./:/app
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: root
      MYSQL_PASSWORD: secret
      MYSQL_DB: todos

  mysql:
    image: mysql:5.7
    volumes:
      - todo-mysql-data:/var/lib/mysql
    environment: 
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: todos

volumes:
  todo-mysql-data:

docker-compose.yml ํŒŒ์ผ๋ช…์œผ๋กœ YAML ํŒŒ์ผ์„ ์ž‘์„ฑํ•˜๊ณ  docker-compose up ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•˜์—ฌ ์‹œ์ž‘ํ•œ๋‹ค.

๋ฐฑ๊ทธ๋ผ์šด๋“œ ์‹คํ–‰ ์˜ต์…˜ -d

docker-compose up -d

๋งŒ์•ฝ docker-compose.yml์ด ์•„๋‹Œ ๋‹ค๋ฅธ ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„  docker-compose -f {YAML_FILE_PATH} up ํ˜•์‹์œผ๋กœ ๋ช…๋ น์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

ex) $ docker-compose -f docker-compose-custom.yml up docker-compose -f docker-compose.yml -f docker-compose-test.yml up ํ˜•ํƒœ๋กœ ๋‘๊ฐœ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. YAML์„ ๋‘๊ฐœ ์ด์ƒ ์„ค์ •ํ•  ๊ฒฝ์šฐ ์•ž์— ์žˆ๋Š” ์„ค์ •๋ณด๋‹ค ๋’ค์— ์žˆ๋Š” ํŒŒ์ผ์ด ์šฐ์„ ์‹œ ๋œ๋‹ค.

Docker Compose Environment

.env file

ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ค์ •์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฐธ์กฐ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

web:
  image: "webapp:${TAG}"

๊ธฐ๋ณธ์ ์œผ๋กœ docker-compose up์„ ํ•œ ์ƒํƒœ์—์„œ๋Š” .env ํŒŒ์ผ์„ ์ฐพ์•„ ํŒŒ์ผ ๋‚ด๋ถ€์— ์žˆ๋Š” ๊ฐ’์„ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

$ cat .env
TAG=v1.5

$ cat docker-compose.yml
version: '3'
services:
  web:
    image: "webapp:${TAG}"$

$ docker-compose config
version: '3'
services:
  web:
    image: 'webapp:v1.5'


$ export TAG=v2.0
$ docker-compose config

version: '3'
services:
  web:
    image: 'webapp:v2.0'

์—ฌ๋Ÿฌ ํŒŒ์ผ์—์„œ ๋™์ผํ•œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•  ๊ฐ’์„ ์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด Compose์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์šฐ์„  ์ˆœ์œ„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. Compose file

  2. Shell environment variables

  3. Environment file

  4. Dockerfile

  5. Variable is not defined

์ƒํ™ฉ์— ๋”ฐ๋ฅธ .env ์ ์šฉ๋ฒ•

dev, prod, local ํ™˜๊ฒฝ์— ๋”ฐ๋ผ์„œ .env.dev, .env.prod, .env.local ํ˜•์‹์œผ๋กœ ํ• ๋‹น์„ ์ค„ ์ˆ˜ ์žˆ๋Š”๋ฐ --env-file ์˜ต์…˜์œผ๋กœ environment ํŒŒ์ผ ์ง€์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

$ docker-compose --env-file ./config/.env.dev up

CLI Environment

์•„๋ž˜์˜ ๋ช…๋ น์–ด๋“ค์€ ๊ฐ๊ฐ ๊ทธ ์•„๋ž˜์˜ compose ํŒŒ์ผ์ด ์ผ์น˜ํ•˜๊ฒŒ ๋งตํ•‘๋œ๋‹ค.

$ docker run -e VARIABLE1

web:
  environment:
    - VARIABLE1
$ docker-compose run -e DEBUG=1 web python console.py

web:
  environment:
    - DEBUG=1

Docker Cli Environment Variable

CLI์—์„œ ์‹คํ–‰ํ• ๋•Œ์˜ ์ฐธ๊ณ  ๋ณ€์ˆ˜ ํ‚ค ๊ฐ’ https://docs.docker.com/compose/reference/envvars/

Compose Network ์„ค์ • ์˜ต์…˜

version: "3.9"
services:
  web:
    build: .
    ports:
      - "8000:8000"
  db:
    image: postgres
    ports:
      - "8001:5432"

์œ„ Compose๋ฅผ ์‹คํ–‰ํ–ˆ์„๋•Œ์˜ ๋„คํŠธ์›Œํฌ๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ณผ์ •์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  1. myapp_default ๋„คํŠธ์›Œํฌ ์ƒ์„ฑ

  2. web ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ƒ์„ฑ. (web์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ myapp_default์— ์ ‘์†)

  3. db ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ƒ์„ฑ. (db์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ myapp_default์— ์ ‘์†)

  • ๋„คํŠธ์›Œํฌ๊ฐ€ ๊ตฌ์„ฑ๋œ ์ดํ›„ postgres://db:5432 ์ฃผ์†Œ๋กœ ์ ‘์† ๊ฐ€๋Šฅ

  • ๋‚ด๋ถ€ ์ปจํ…Œ์ด๋„ˆ ๋ผ๋ฆฌ๋Š” postgres://db:5432๋กœ ์ ‘์†ํ•˜๊ณ  ์™ธ๋ถ€์—์„  postgres://{DOCKER_IP}:8001๋กœ ์ ‘์†

  • ์ปจํ…Œ์ด๋„ˆ ์—…๋ฐ์ดํŠธ๋ฅผ ์œ„ํ•ด ๋‹ค์‹œ ์‹œ์ž‘ํ•  ๊ฒฝ์šฐ์—๋Š” ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„์€ ๊ฐ™์ง€๋งŒ ๋‹ค๋ฅธ IP๋กœ ๋„คํŠธ์›Œํฌ๊ฐ€ ์ƒ์„ฑ๋˜์–ด IP ๋ฒ ์ด์Šค๋Š” IP๋ฅผ ๋‹ค์‹œ ์ฐพ์•„์„œ ์—ฐ๊ฒฐํ•˜์—ฌ์•ผ ํ•˜๊ณ  ๋‹ค๋ฅธ ์ปจํ…Œ์ด๋„ˆ์—์„  ๊ธฐ์กด์— ์ ‘์†ํ–ˆ๋˜ ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„์œผ๋กœ ์žฌ ์—ฐ๊ฒฐ์„ ์‹œ๋„ํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

๋‹ค๋ฅธ ์„œ๋น„์Šค์˜ ์ปจํ…Œ์ด๋„ˆ์— ์—ฐ๊ฒฐํ•œ๋‹ค. ์ด๋ฆ„๋งŒ ์ง€์ •ํ•˜๊ฑฐ๋‚˜ {name}:{alias}ํ˜•์‹์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. depend_on ์ฒ˜๋Ÿผ ๋””ํŽœ๋˜์‹œ ๊ด€๊ณ„๊ฐ€ ๋งบ์–ด์ง€๋ฉฐ ๋„คํŠธ์›Œํฌ๋ž‘ ๊ฐ™์ด ์“ฐ์ผ๋•Œ๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ๋„คํŠธ์›Œํฌ๊ฐ€ ๊ด€๋ฆฌ๋˜์–ด ๋„คํŠธ์›Œํฌ๋งŒ ์‚ฌ์šฉํ•˜๊ธธ ๊ถŒ์žฅ

version: "3"
services:
  web:
    build: .
    links:
      - "redis"
      - "db:database"
  db:
    image: postgres

Custom Network

Custom Network๋ฅผ ํ†ตํ•ด ์ข€ ๋” ๋ณต์žกํ•œ ๋„คํŠธ์›Œํฌ ํ† ํด๋กœ์ง€๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค

version: "3"
services:
  proxy:
    build: ./proxy
    networks:
      - frontend
  app:
    build: ./app
    networks:
      - frontend
      - backend
  db:
    image: postgres
    networks:
      - backend
networks:
  frontend:
    # Use a custom driver
    driver: custom-driver-1
  backend:
    # Use a custom driver which takes special options
    driver: custom-driver-2
    driver_opts:
      foo: "1"
      bar: "2"

top-level network ์˜ต์…˜์—์„œ ์ปค์Šคํ…€ํ•˜๊ฒŒ ์ง€์ •์ด ๊ฐ€๋Šฅ

์œ„์™€ ๊ฐ™์ด ์„ค์ •์„ ํ•˜๋ฉด app์—์„œ๋งŒ proxy, db์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๊ณ  db์™€ proxy๋Š” ์„œ๋กœ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ์„ค์ •์ด๋œ๋‹ค.

IPv4, IPv6

IPv6 ์‚ฌ์šฉํ•˜๋ ค๋ฉด enable_ipv6๋ฅผ ๋ฐ˜๋“œ์‹œ true๋กœ ํ•ด์•ผํ•œ๋‹ค.

version: "2.4"

services:
  app:
    image: busybox
    command: ifconfig
    networks:
      app_net:
        ipv4_address: 172.16.238.10
        ipv6_address: 2001:3984:3989::10

networks:
  app_net:
    driver: bridge
    enable_ipv6: true
    ipam:
      driver: default
      config:
        - subnet: 172.16.238.0/24
          gateway: 172.16.238.1
        - subnet: 2001:3984:3989::/64
          gateway: 2001:3984:3989::1

Default Network

default๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž์ฒด ๋„คํŠธ์›Œํฌ๋ฅผ ์ง€์ •ํ•˜๋ฉด์„œ ์•ฑ ์ „์ฒด์˜ ๊ธฐ๋ณธ์ ์ธ ๋„คํŠธ์›Œํฌ ๊ตฌ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

version: "3"
services:

  web:
    build: .
    ports:
      - "8000:8000"
  db:
    image: postgres

networks:
  default:
    # Use a custom driver
    driver: custom-driver-1

Use Pre Existing Network

๊ธฐ์กด์˜ ์™ธ๋ถ€์˜ ๋„คํŠธ์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด external์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ช…์‹œํ•œ๋‹ค

networks:
  default:
    external:
      name: my-pre-existing-network

docker Compose Version ๋ณ„ yml ๋ณ€์ˆ˜

Versioning

docker-compose.yml ์ƒ๋‹จ์— ์ž‘์„ฑํ•˜๋Š” versioning ์ž‘์„ฑ ๋ฐฉ๋ฒ•

  • Version 1์—์„œ๋Š” ๋ฒ„์ €๋‹์„ ์ƒ๋žต

  • Version 2๋ถ€ํ„ฐ ๋งˆ์ด๋„ˆ ๋ฒ„์ „(2.x)๊นŒ์ง€ ์„ค์ •ํ•ด์•ผ ํ•จ (์ƒ๋žต์‹œ 2.0์œผ๋กœ ์ ์šฉ๋œ๋‹ค.)

  • Version 3์€ ๋„์ปค ์Šค์›œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉ๋˜๋„๋ก ๋””์ž์ธ ๋จ

๊ฐ docker ๋ฒ„์ „ ๋ณ„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ docker-compose.yml ๋ฒ„์ „

Compose file format
Docker Engine release
Compose Version

3.8

19.03.0+

1.26.0+

3.7

18.06.0+

-

3.6

18.02.0+

-

3.5

17.12.0+

-

3.4

17.09.0+

-

3.3

17.06.0+

-

3.2

17.04.0+

-

3.1

1.13.1+

-

3.0

1.13.0+

1.10.0+

2.4

17.12.0+

1.21.0+

2.3

17.06.0+

1.16.0+

2.2

1.13.0+

1.13.0+

2.1

1.12.0+

1.9.0+

2.0

1.10.0+

1.6.0+

1.0

1.9.1.+

Compose ๋ฒ„์ „์— ๋”ฐ๋ฅธ ์ฐจ์ด์ 

  • Structure๊ฐ€ ํ—ˆ์šฉํ•˜๋Š” ํ‚ค ๊ฐ’๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜

  • ์‹คํ–‰ํ•ด์•ผํ•˜๋Š” ์ตœ์†Œ Docker Engine ๋ฒ„์ „

  • ๋„คํŠธ์›Œํ‚น๊ณผ ๊ด€๋ จ๋œ Compose์˜ ๋™์ž‘

Compose ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜

Version 2.x -> 3.x ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜

Compose ๋ฒ„์ „ 2์™€ 3์˜ ๊ตฌ์กฐ๋Š” ๋™์ผํ•˜์ง€๋งŒ ์‚ญ์ œ๋œ ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์ด ์žˆ์–ด์„œ 3.X์— ๋งž๊ฒŒ ์ˆ˜์ •ํ•˜์—ฌ์ค€๋‹ค.

  • volume_driver

version: "3.8"
services:
  db:
    image: postgres
    volumes:
      - data:/var/lib/postgresql/data
volumes:
  data:
    driver: mydriver
  • ์„œ๋น„์Šค์—์„œ ๋ณผ๋ฅจ ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์„ค์ •ํ•˜๋Š” ๋Œ€์‹  top-level volumes option์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ณผ๋ฅจ์„ ์ •์˜ ํ•˜๊ณ  ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์ง€์ •

  • volumes_from: ์„œ๋น„์Šค๊ฐ„์— ๋ณผ๋ฅจ์„ ๊ณต์œ ํ•˜๋ ค๋ฉด top-level volumes option์„ ์‚ฌ์šฉํ•˜์—ฌ ์ •์˜ํ•˜๊ณ  ์„œ๋น„์Šค ์ˆ˜์ค€ ๋ณผ๋ฅจ ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ณต์œ ํ•˜๋Š” ๊ฐ ์„œ๋น„์Šค์—์„œ ์ฐธ์กฐ

  • cpu_shares, cpu_quota, cpuset, mem_limit, memswap_limit๋Š” 3.x์˜ resource๋กœ ๋Œ€์ฒด

version: "3.8"
services:
  redis:
    image: redis:alpine
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 50M
        reservations:
          cpus: '0.25'
          memory: 20M
  • extends์€ ์ œ๊ฑฐ๋˜์–ด ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ด์•ผํ•จ

  • group_add์€ 3.x์—์„œ ์ œ๊ฑฐ๋จ

  • pids_limit์€ 3.x์— ๋„์ž…๋˜์ง€ ์•Š์Œ

  • networks - link_local_ips ์˜ต์…˜์€ 3.x์— ๋„์ž…๋˜์ง€ ์•Š์Œ

Version 1 -> 2.x ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜

  • ๋ชจ๋“ ๊ฒƒ์„ ํ•œ๋‹จ๊ณ„์”ฉ ๋“ค์—ฌ์“ฐ๊ธฐ๋ฅผ ํ•˜๊ณ  services:๋ฅผ ์‚ฝ์ž…

  • ์ตœ์ƒ๋‹จ์— version: 2.x ์ง€์ •

  • dockerfile์€ build ํ•˜์œ„๋กœ ์˜ฎ๊ธด๋‹ค.

build:
  context: .
  dockerfile: Dockerfile-alternate
  • log_driver, log_opt๋Š” logging ํ•˜์œ„๋กœ ๋‘๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝํ•œ๋‹ค.

logging:
  driver: syslog
  options:
    syslog-address: "tcp://192.168.0.42:123"
  • net๋Š” network_mode๋กœ ๋Œ€์ฒด

net: host    ->  network_mode: host
net: bridge  ->  network_mode: bridge
net: none    ->  network_mode: none

net: "container:web"  ->  network_mode: "service:web"

# net: "container:[container name/id]" ํ˜•ํƒœ๋Š” ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ํ•„์š”๊ฐ€ ์—†์Œ
net: "container:cont-name"  ->  network_mode: "container:cont-name"
net: "container:abc12345"   ->  network_mode: "container:abc12345"
  • volumes named volumes์ด ์žˆ๋Š” ๊ฒฝ์šฐ top-level volumes option์—์„œ data๋ฅผ ์„ ์–ธํ•ด์•ผ ํ•จ

version: "2.4"
services:
  db:
    image: postgres
    volumes:
      - data:/var/lib/postgresql/data
volumes:
  data: {}
  • ๊ธฐ๋ณธ์ ์œผ๋กœ ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์œผ๋กœ ๋ณผ๋ฅจ์„ ์„ ์–ธํ•˜์ง€๋งŒ data๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ณผ๋ฅจ์„ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ์œผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์™ธ๋ถ€๋กœ ์„ ์–ธ์„ํ•œ๋‹ค.

volumes:
  data:
    external: true

๋ฒ„์ „๋ณ„ ํŠน์ง• ๋ฐ ๊ตฌ์„ฑ

Version.1

https://docs.docker.com/compose/compose-file/compose-file-v1/

  • yml ๋ฌธ์„œ์— ๋ฒ„์ „๋ช…์ด ์ƒ๋žต

  • volumes, networks or build arguments ์‚ฌ์šฉ ๋ถˆ๊ฐ€

    • networking์ด ์ง€์›๋˜์ง€ ์•Š์Œ

    • ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ๋Š” ๊ธฐ๋ณธ bridge ๋„คํŠธ์›Œํฌ ์— ๋ฐฐ์น˜ ๋˜๊ณ  ํ•ด๋‹น IP ์ฃผ์†Œ์˜ ๋‹ค๋ฅธ ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ์—์„œ ์—ฐ๊ฒฐ ๊ฐ€๋Šฅ

    • ์ปจํ…Œ์ด๋„ˆ ๊ฐ„ ๊ฒ€์ƒ‰์„ ํ™œ์„ฑํ™” ํ•˜๋ ค๋ฉด links ๋ฅผ ์‚ฌ์šฉ

links https://docs.docker.com/compose/compose-file/compose-file-v1/#links

web:
  build: .
  ports:
    - "5000:5000"
  volumes:
    - .:/code
  links:
    - redis
redis:
  image: redis

Version.2

https://docs.docker.com/compose/compose-file/compose-file-v2/

  • Compose 1.6.0 ์ด์ƒ Docker Engine of version 1.10.0+.์—์„œ ๋™์ž‘

  • yml ๋ฌธ์„œ์— ๋ฒ„์ „๋ช…์„ ๋งˆ์ด๋„ˆ ๋ฒ„์ „๊นŒ์ง€ ์ž‘์„ฑ ex) 2.1

  • ๋ชจ๋“  ์„œ๋น„์Šค๋Š” services ํ‚ค ์•„๋ž˜์— ์„ ์–ธ

  • volumes์— Named volumes ์ƒ์„ฑ ๊ฐ€๋Šฅ

  • networks์— Network ์ƒ์„ฑ ๊ฐ€๋Šฅ

  • ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์ฒด์˜ ๊ธฐ๋ณธ ๋„คํŠธ์›Œํฌ์— ์—ฐ๊ฒฐ๋˜๋ฉฐ ์„œ๋น„์Šค ์ด๋ฆ„๊ณผ ๋™์ผํ•œ ํ˜ธ์ŠคํŠธ ์ด๋ฆ„์—์„œ ๊ฒ€์ƒ‰ ๊ฐ€๋Šฅ ์ด๊ฒƒ์€ ๋งํฌ๊ฐ€ ๊ฑฐ์˜ ๋ถˆํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธ.

links ๋™์ž‘ ์›๋ฆฌ ์ด ์ปจํ…Œ์ด๋„ˆ์˜ /etc/hosts ํŒŒ์ผ์— ๊ทธ ๋‚ด์šฉ์ด ์ถ”๊ฐ€๋˜์–ด์„œ ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋‹ค๋ฅธ ์ปจํ…Œ์ด๋„ˆ ๋“ค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

192.168.1.186 db
192.168.1.186 database
192.168.1.187 redis

๋„คํŠธ์›Œํฌ https://docs.docker.com/compose/networking/

Version2 ์˜ˆ์‹œ

version: "2.4"
services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/code
    networks:
      - front-tier
      - back-tier
  redis:
    image: redis
    volumes:
      - redis-data:/var/lib/redis
    networks:
      - back-tier
volumes:
  redis-data:
    driver: local
networks:
  front-tier:
    driver: bridge
  back-tier:
    driver: bridge

์ถ”๊ฐ€๋œ ๋งค๊ฐœ๋ณ€์ˆ˜

  • network - aliases

  • Network - depends_on:

depends_on

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

์‹คํ–‰์‹œ depends_on์— ๋ช…์‹œ๋œ db, redis๊ฐ€ ๋œฌ ๋’ค์— ํ•ด๋‹น ์„œ๋น„์Šค๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

Version 2.1

์ถ”๊ฐ€๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜

  • link_local_ips

  • build, service - isolation

  • volumes, networks, build - labels

  • volumes - name

  • userns_mode

  • healthcheck

  • sysctls

  • pids_limit

  • oom_kill_disable

  • cpu_period

Version 2.2

์ถ”๊ฐ€๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜

  • init

  • scale

  • cpu_rt_runtime, cpu_rt_period

  • build - network์—

Version 2.3

์ถ”๊ฐ€๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜

  • build - target, extra_hosts,shm_size

  • healthcheck - start_period

  • service - runtime

  • volumns - LONG SYNTAX ์ œ๊ณต

  • device_cgroup_rules

LONG SYNTAX

 volumes:
   - type: volume
     source: mydata
     target: /data
     volume:
       nocopy: true
   - type: bind
     source: ./static
     target: /opt/app/static

Version 3

https://docs.docker.com/compose/compose-file/compose-file-v3/ ์‚ญ์ œ๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜

  • volume_driver

  • volumes_from

  • cpu_shares

  • cpu_quota

  • cpuset

  • mem_limit

  • memswap_limit

  • extends

  • group_add

์ถ”๊ฐ€๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜

Version 3.1

์ถ”๊ฐ€๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜

Version 3.2

์ถ”๊ฐ€๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜

Version 3.3

์ถ”๊ฐ€๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜

Version 3.4

์ถ”๊ฐ€๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜

Version 3.5

์ถ”๊ฐ€๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜

Version 3.6

์ถ”๊ฐ€๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜

  • volume mount type - tmpfs

Version 3.7

์ถ”๊ฐ€๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜

Version 3.8

์ถ”๊ฐ€๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜

์ฐธ๊ณ ํ•œ ๋ฌธ์„œ https://docs.microsoft.com/ko-kr/visualstudio/docker/tutorials/use-docker-compose https://www.44bits.io/ko/post/almost-perfect-development-environment-with-docker-and-docker-compose#%EB%8F%84%EC%BB%A4-%EC%BB%B4%ED%8F%AC%EC%A6%88%EC%9D%98-%EC%A3%BC%EC%9A%94-%EB%AA%85%EB%A0%B9%EC%96%B4 https://docs.docker.com/compose/environment-variables/ https://docs.docker.com/compose/env-file/ https://docs.docker.com/compose/reference/envvars/

Last updated