DevOps/Docker

Docker Compose

잔망루피 2022. 5. 5. 18:58
반응형

Compose file

  • 구성
    • version
    • services
      • 실행할 컨테이너들을 정의
      • image
        • 컨테이너를 생성할 때 사용할 이미지
      • build
        • 도커파일로부터 이미지를 빌드
        • context
          • Dockerfile이 있는 위치
      • environment
        • 환경 변수 설정
      • command
        • 컨테이너가 실행될 때 수행할 명령어
      • depends_on
        • 명시된 컨테이너가 먼저 생성되고 실행
      • ports
        • 개방할 포트 지정
      • expose
        • 링크로 연계된 컨테이너에게만 공개할 포트 설정
      • volumes
        • 컨테이너에 볼륨 마운트
      • restart
        • 컨테이너가 종료될 때 재시작하는 방법
        • no
          • 재시작 x
        • always
          • 항상 재시작
        • on-failure
          • 오류가 있을 때 재시작
    • network
    • volume
    • config
    • secret

 

 

Compose file 예시

version: '3'

services:
  spring-boot:
    restart: 'on-failure'
    build: .
    ports:
      - '8000:8000'
  mysql:
    image: mysql:5.7
    ports:
      - "3306:3306"
    volumes:
      - auth-mysql-vol:/var/lib/mysql
    env_file:
      - .env
    restart: always
  redis:
    image: redis:latest
    ports:
      - "6379:6379"
    volumes:
      - auth-redis-vol:/var/lib/redis
    restart: always

volumes:
  auth-mysql-vol:
  auth-redis-vol:

 

Docker Compose 명령어

docker-compose -f {도커 컴포즈 파일} up -d

up 도커 컴포즈 파일로 컨테이너를 생성

-f 도커 컴포즈 파일 지정

-d 백그라운드에 실행

 

docker-compose up --build

도커 이미지 빌드 후 컨테이너 실행

--build는 캐싱된 이미지를 확인하지 않고 다시 빌드

 

docker-compose down

실행 중인 컨테이너 중지하고 삭제

 

docker-compose restart

컨테이너 다시 시작

 

 

Ubuntu에 Docker Compose 설치

sudo apt-get update
sudo apt-get install docker-compose-plugin
docker compose version

버전까지 뜨면 성공이다.

 

 


참고 👇

https://docs.docker.com/compose/gettingstarted/

 

Try Docker Compose

 

docs.docker.com

 

https://docs.docker.com/compose/install/linux/

 

Install the Compose plugin

Download and install Docker Compose on Linux with this step-by-step handbook. This plugin can be installed manually or by using a repository.

docs.docker.com

 

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

 

Compose Build Specification

Learn about the Compose Build Specification

docs.docker.com

 

반응형