2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

fig.yml でも Yaml anchors/references ですっきり書ける

Last updated at Posted at 2015-02-20

経緯

例えば

  • PHP 5.4、5.5、5.6 のコンテナをこうばばっと Fig で起動したい
  • アプリケーションコードは各コンテナが同じディレクトリを volume でマウント

ということをしたい時、fig.yml をこうするでしょうか。

php54:
  image: example/php54
  ports:
    - "80"
  volumes:
    - /opt/app:/opt/app

php55:
  image: example/php55
  ports:
    - "80"
  volumes:
    - /opt/app:/opt/app

php56:
  image: example/php56
  ports:
    - "80"
  volumes:
    - /opt/app:/opt/app

かなりタルい。image だけ違ってあとは共通、となるとこうなってしまいがちです。

共通項目であるところの portsvolumes
Dockerfile に VOLUME とか EXPOSE とか書けば良い、というのは
確かにその通りなんですが、せっかく fig で呼び出すのだから
fig.yml にある程度まとめておきたい。

そんな時

Yaml の Anchor(&) と Reference(*) を思い出しました。

php54: &php
  image: example/php54
  ports:
    - "80"
  volumes:
    - /opt/app:/opt/app

php55:
  <<: *php
  image: example/php55

php56:
  <<: *php
  image: example/php56

すっきりした :pizza:

参考

「各サービスで使えるグローバルなパラメータ設定したいわ」
「お前それ Yaml の anchor でがんばれよ」

というやりとりが多かった。

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?