Skip to content

Latest commit

 

History

History
45 lines (33 loc) · 1.67 KB

12-interpolation.md

File metadata and controls

45 lines (33 loc) · 1.67 KB

Interpolation

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 are supported. Default values can be defined inline using typical shell syntax: latest

  • ${VARIABLE:-default} evaluates to default if VARIABLE is unset or empty in the environment.
  • ${VARIABLE-default} evaluates to default only if VARIABLE is unset in the environment.

Similarly, the following syntax allows you to specify mandatory variables:

  • ${VARIABLE:?err} exits with an error message containing err if VARIABLE is unset or empty in the environment.
  • ${VARIABLE?err} exits with an error message containing err if VARIABLE is unset in the environment.

Interpolation can also be nested:

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

Other extended shell-style features, such as ${VARIABLE/foo/bar}, are not supported by the Compose specification.

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 the Compose implementation can't resolve a substituted variable and no default value is defined, it MUST warn the user and substitute 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 MUST be applied before merge on a per-file-basis.