1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

この記事では、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

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?