補間


Values in a Compose file can be set by variables and interpolated at runtime. Compose files use a Bash-like syntax ${VARIABLE}. Both $VARIABLE and ${VARIABLE} syntax is supported.

ブレース (波カッコ) 表現は以下の書式がサポートされています。

  • 単純な置き換え
    • ${VAR} -> VAR の値
  • デフォルト値の指定
    • ${VAR:-default} -> VAR が設定済または空でないならその値、それ以外は default
    • ${VAR-default} -> VAR が設定済ならその値、それ以外は default
  • 値指定の必須
    • ${VAR:?error} -> VAR が設定済または空でないならその値、それ以外はエラー終了
    • ${VAR?error} -> VAR が設定済ならその値、それ以外はエラー終了
  • 別の指定値
    • ${VAR:+replacement} -> VAR が設定済または空でないなら replacement、そうでないなら空
    • ${VAR+replacement} -> VAR が設定済なら replacement、そうでないなら空

補間表現はネスト化することもできます。

  • ${VARIABLE:-${FOO}}
  • ${VARIABLE?$FOO}
  • ${VARIABLE:-${FOO:-default}}

これ以外にシェル形式の機能にある ${VARIABLE/foo/bar} といったものは Compose ではサポートされていません。

Compose processes any string following a $ sign as long as it makes it a valid variable definition - either an alphanumeric name ([_a-zA-Z][_a-zA-Z0-9]*) or a braced string starting with ${. In other circumstances, it will be preserved without attempting to interpolate a value.

You can use a $$ (double-dollar sign) when your configuration needs a literal dollar sign. This also prevents Compose from interpolating a value, so a $$ allows you to refer to environment variables that you don't want processed by Compose.

web:
  build: .
  command: "$$VAR_NOT_INTERPOLATED_BY_COMPOSE"

If Compose can't resolve a substituted variable and no default value is defined, it displays a warning and substitutes the variable with an empty string.

As any values in a Compose file can be interpolated with variable substitution, including compact string notation for complex elements, interpolation is applied before a merge on a per-file basis.

Interpolation applies only to YAML values, not to keys. For the few places where keys are actually arbitrary user-defined strings, such as labels or environment, an alternate equal sign syntax must be used for interpolation to apply. For example:

services:
  foo:
    labels:
      "$VAR_NOT_INTERPOLATED_BY_COMPOSE": "BAR"
services:
  foo:
    labels:
      - "$VAR_INTERPOLATED_BY_COMPOSE=BAR"