LoginSignup
3

More than 5 years have passed since last update.

docker-compose 3.4から追加されたExtension Fieldを使ってdocker-compose.yml でアンカー エイリアスを使う

Last updated at Posted at 2018-06-21

ymlのアンカーとエイリアスを使うのは前々から出来たのですが、 docker-compose v3.4 以前は services 内にアンカーを書かなくてはいけなく少し不便だなと思っていました。

参考記事: docker-compose V3 で extends っぽい事をする

v3.4 から更に管理しやすくなったのでメモ。

Extension Field

v3.4 でExtension Fieldが追加されました。servicesなどを記述できるトップレベルに、自由にフィールドを追加できます。

こんな感じの docker-compose.yml があるとします。

version: '3'

services:

  web: &web
    build: .
    restart: always
    image: app
    env_file: ./.env
    command: ["./wait-for-it.sh", "mysql:9200", "--", "nodemon"]
    depends_on:
      - redis
      - mysql

  worker:
    environment:
      WORKER: "true"
    <<: *web

  redis:
    image: redis:latest

  mysql:
    image: mysql:5

v3.4からは次のようにできます。

version: '3.4'

x-template: &template
  build: .
  restart: always
  image: app
  env_file: ./.env
  command: ["./wait-for-it.sh", "mysql:9200", "--", "nodemon"]
  depends_on:
    - redis
    - mysql

services:

  web:
    <<: *template

  worker:
    environment:
      WORKER: "true"
    <<: *template

  redis:
    image: redis:latest

  mysql:
    image: mysql:5

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
What you can do with signing up
3