Docker-compose

From 탱이의 잡동사니
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Overview

docker-compose 내용 정리.

Basic

Define and run multi-container application with Docker. Docker-compose 는 컨테이너를 여러 개 동작시키는 도커 어플리케이션을 정의하고 실행하는 도구이다.

Usage:
  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file
                              (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name
                              (default: directory name)
  --verbose                   Show more output
  --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the
                              name specified in the client certificate
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)
  --compatibility             If set, Compose will attempt to convert keys
                              in v3 files to their non-Swarm equivalent

docker-compose.yml

docker-compose 에서는 컨테이너 실행에 사용되는 옵션과 컨테이너 간 의존성을 모두 docker-compose.yml 파일에 적어두고, docker-compose 명령어를 사용하여 컨테이너를 실행 및 관리한다.

docker-compose.yml 파일은 옵션을 미리 정의한 문서라고 볼 수 있다. 다음과 같은 내용으로 이루어져 있다.

version: '3'

services:
  db:
    image: postgres
    volumes:
      - ./docker/data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=sampledb
      - POSTGRES_USER=sampleuser
      - POSTGRES_PASSWORD=samplesecret
      - POSTGRES_INITDB_ARGS=--encoding=UTF-8

  django:
    build:
      context: .
      dockerfile: ./compose/django/Dockerfile-dev
    environment:
      - DJANGO_DEBUG=True
      - DJANGO_DB_HOST=db
      - DJANGO_DB_PORT=5432
      - DJANGO_DB_NAME=sampledb
      - DJANGO_DB_USERNAME=sampleuser
      - DJANGO_DB_PASSWORD=samplesecret
      - DJANGO_SECRET_KEY=dev_secret_key
    ports:
      - "8000:8000"
    command: 
      - python manage.py runserver 0:8000
    volumes:
      - ./:/app/

version

docker-compose.yml 파일의 첫 줄에는 파일 규격 버전을 적는다. 파일의 규격에 따라 지원하는 옵션이 달라지는데, "3"이라고 적으면 3으로 시작하는 최신버전을 사용한다는 의미이다. 상세한 내용은 이곳<ref>https://docs.docker.com/compose/compose-file/compose-versioning/</ref>을 참조하자.

services

services:

이 항목 믙에 실행하려는 컨테이너들을 정의한다. docker-compose 에서는 컨테이너 대신 서비스라는 개념을 사용한다.

db

services:
  db:

위의 예제에서는 postgres 서비스의 이름을 db로 정의하였다.

image

services:
  db:
    image: postgres

db 서비스에서 사용할 도커 이미지를 정의한다. 여기서는 dockerhub 의 공식 postgres 이미지를 사용한다.

volumes

services:
  db:
    volumes:
      - ./docker/data:/var/lib/postgresql/data

docker run 으로 db 컨테이너를 실행할 때 --volume 옵션을 사용하여 데이터베이스의 데이터를 로컬 컴퓨터에 저장하는 부분과 같다.

environment

services:
  db:
    environment:
      - POSTGRES_DB=sampledb
      - POSTGRES_USER=sampleuser
      - POSTGRES_PASSWORD=samplesecret
      - POSTGRES_INITDB_ARGS=--encoding=UTF-8

docker run 명령어 사용시 -e 옵션으로 지정되는 내용을 나타낸다.

See also

References

<references />