
📖 진도: 4장 / 211~229p
run 커맨드를 여러 번 사용할수도 있지만, 이는 관리가 어려움docker-compose.yaml 파일을 사용하여 여러 컨테이너를 하나의 애플리케이션으로 관리할 수 있음docker-compose.yaml 파일을 정의해야 함docker-compose.yaml을 사용하지 않는 경우
# docker run -d --name mysql \
alicek106/composetest:mysql \
mysqld
# docker run -d -p 80:80 \
--link mysql:db --name web \
alicek106/composetest:web \
apachectl -DFOREGROUND
docker-compose.yaml 파일을 사용하는 경우
services:
web:
image: alicek106/composetest:web
ports:
- 80:80
links:
- mysql:db
command: apachectl -DFOREGROUND
mysql:
image: alicek106/composetest:mysql
command: mysqld
graph LR
A[docker-compose.yml] --> B[프로젝트<br/>ubuntu]
B --> C[서비스<br/>web]
B --> D[서비스<br/>mysql]
C --> E[컨테이너<br/>web x2]
D --> F[컨테이너<br/>mysql]
style A fill:#fff,stroke:#333,stroke-width:1px
style B fill:#f0f0f0,stroke:#333,stroke-width:1px
style C fill:#d0f0ff,stroke:#333,stroke-width:1px
style D fill:#d0f0ff,stroke:#333,stroke-width:1px
style E fill:#c0ffc0,stroke:#333,stroke-width:1px
style F fill:#c0ffc0,stroke:#333,stroke-width:1px
docker compose scale mysql=2와 같은 명령어로 컨테이너 수를 조정할 수 있음docker compose up -d mysql과 같은 명령어로 컴포즈 파일에 정의된 특정 서비스의 컨테이너만 시작할 수도 있음docker compose -p <project_name>과 같은 옵션으로 지정할 수 있음도커 컴포즈 파일은 크게 세 부분으로 나뉨
서비스 정의
services:
web:
image: alicek106/composetest:web # 베이스로 사용할 이미지
links:
- mysql:db # 다른 서비스와 연결 - deprecated
- redis:cache
depends_on: # 의존성 설정
- mysql
- redis
environment:
- MYSQL_HOST=mysql
- MYSQL_PORT=3306
# 혹은
MYSQL_HOST: mysql
MYSQL_PORT: 3306
command: apachectl -DFOREGROUND
그러나 links와 depends_on은 컨테이너의 가동만을 확인하며, 실제 어플리케이션의 구동은 확인하지 않음. 아래와 같은 방법으로 확인 가능
yaml
...
entrypoint: ./sync_script.sh mysql:3306
검증 스크립트
until (<상태확인 명령어>); do
echo "depend container is not available yet"
sleep `
done
echo "depends_on container is ready"
네트워크 정의
services:
myservice:
image: nginx
networks:
- mynetwork
networks:
mynetwork:
driver: overlay # 도커 스웜에서 overlay 네트워크 사용
ipam: # IP Address Manager 사용을 위한 옵션. 상세한 설정을 위해 사용
driver: mydriver
config:
sublet: 172.20.0.0/16 # 서브넷 범위
ip_range: 172.20.5.0/24 # 사용 가능한 IP 범위
gateway: 172.20.5.1 # 서브넷 내에서 사용할 게이트웨이 주소
other_network:
external: true # 외부 네트워크 사용
볼륨 정의
services:
mysql:
volumes:
- my_test_data:/data
volumes:
my_test_data:
driver: local
other_volume:
external: true
YAML 파일 검증하기
docker compose config 명령어로 파일 형식 검증 가능-f ../some/other/file.yml 옵션을 통해 다른 파일을 지정할 수 있음YAML 파일에 네트워크를 별도로 지정하지 않으면, 도커 컴포즈는 각 프로젝트별로 브리지 타입 네트워크를 생성docker compose up 뿐 아니라, docker compose scale 명령어로 생성된 컨테이너도 이 네트워크에 속함docker-compose.yml 파일은 docker stack 명령어로 도커 스웜 모드에서 사용할 수 있음graph TD
A[User] -->|"User interacts with</br>the Docker Engine"| B[Docker Engine]
B -->|"Engine communicates</br>with containerd"| C[containerd]
C --> D[runC]
C -->|"containerd spins up runC</br>or other OCI compliant runtime</br>to run containers"| D2[runC]
C --> E["..."]
C --> F["..."]
style A fill:#f2f2f2,stroke:#333,stroke-width:1px
style B fill:#d9edf7,stroke:#31708f,stroke-width:1px
style C fill:#dff0d8,stroke:#3c763d,stroke-width:1px
style D fill:#fcf8e3,stroke:#8a6d3b,stroke-width:1px
style D2 fill:#fcf8e3,stroke:#8a6d3b,stroke-width:1px
style E fill:#fcf8e3,stroke:#8a6d3b,stroke-width:1px
style F fill:#fcf8e3,stroke:#8a6d3b,stroke-width:1px
containerd를 사용containerd는 컨테이너를 실행하기 위해 runC를 사용containerd를 사용하기 위한 도구임