Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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

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

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

Extension Field

https://docs.docker.com/compose/compose-file/#extension-fields

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
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした