はじめに
この記事では、Docker Compose で複数の yaml ファイルを利用する方法について記載します。
開発環境
開発環境は以下の通りです。
- Windows 11
- Docker Engine 26.1.1
- Docker Compose 2
複数ファイルの利用
--file
オプションを使うと複数の yaml ファイルを利用することができます。
上書き
以下のように複数の yaml ファイルが存在し、同一の定義がされている場合、後の yaml ファイルの定義が上書きされます。
compose.yaml
services:
app:
environment:
- DEBUG=true
ports:
- "${APP_HOST_MACHINE_PORT:-8000}:8000"
volumes:
- type: bind
source: ./src
target: /my-work
build: ./docker/app
compose-production.yaml
services:
app:
environment:
- DEBUG=false
docker compose --file compose.yaml --file compose-production.yaml config
DBUG
のみ compose-production.yaml
の定義通り false
になり、それ以外は compose.yaml
の定義になっています。
services:
app:
build:
context: C:\Users\ymori\Documents\GitHub\docker-mail-app\docker\app
dockerfile: Dockerfile
environment:
DEBUG: "false"
networks:
default: null
ports:
- mode: ingress
target: 8000
published: "8000"
protocol: tcp
volumes:
- type: bind
source: C:\Users\ymori\Documents\GitHub\docker-mail-app\src
target: /my-work
追記
定義の追記も可能です。
compose.yaml
services:
app:
environment:
- DEBUG=true
ports:
- "${APP_HOST_MACHINE_PORT:-8000}:8000"
volumes:
- type: bind
source: ./src
target: /my-work
build: ./docker/app
compose-ci.yam
services:
proxy:
image: 1.27.0
docker compose --file compose.yaml --file compose-ci.yaml config
compose.yaml
には含まれていない proxy
サービスを確認できます。
services:
app:
build:
context: C:\Users\ymori\Documents\GitHub\docker-mail-app\docker\app
dockerfile: Dockerfile
environment:
DEBUG: "true"
networks:
default: null
ports:
- mode: ingress
target: 8000
published: "8000"
protocol: tcp
volumes:
- type: bind
source: C:\Users\ymori\Documents\GitHub\docker-mail-app\src
target: /my-work
proxy:
image: 1.27.0
networks:
default: null