LoginSignup
23
16

More than 5 years have passed since last update.

docker-compose V3 で extends っぽい事をする

Posted at

docker-compose の V2 にはextendsってのがありまして、これを使って別のサービスの設定を継承する事が出来たんですが、V3になってからは無くなってしまったようです。。

---
# docker-compose.common.yml

version: '2'


services:
  base:
    image: php:7.2-apache
    volumes:
      - ./index.php:/var/www/html/index.php
    environment:
      TITLE: ドッカーコンポーズ

# docker-compose.yml

---
version: '2'


services:
  front:
    extends:
      file: ./docker-compose.common.yml
      service: base
    container_name: front
    ports:
      - 8000:80
    environment:
      MESSAGE: フロントだよ

  admin:
    extends:
      file: ./docker-compose.common.yml
      service: base
    container_name: admin
    ports:
      - 8001:80
    environment:
      MESSAGE: アドミンだよ

上記を version: '3' にすると以下のエラーが出ます。

ERROR: The Compose file './docker-compose.yml' is invalid because:
Unsupported config option for services.admin: 'extends'
Unsupported config option for services.front: 'extends'

なので、yamlの アンカーとエイリアス を使ってあげると、それっぽい事が出来るようになりますよ。

---
version: '3'


services:
  front: &front  # アンカー
    image: php:7.2-apache
    container_name: front
    volumes:
      - ./index.php:/var/www/html/index.php
    ports:
      - 8000:80
    environment:
      TITLE: ドッカーコンポーズ
      MESSAGE: フロントだよ

  admin:
    <<: *front  # エイリアス
    container_name: admin
    ports:
      - 8001:80
    environment:
      TITLE: ドッカーコンポーズ
      MESSAGE: アドミンだよ
23
16
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
23
16