148

I have a YAML scalar that is throwing the following error when I try to evaluate my docker-compose.yml file:

ERROR: Invalid interpolation format for "environment" option in service "time_service": "${Time.now}"

YAML:

---
version: '2'
services:
  time_service:
    build: "."
    environment:
      TIME: "${Time.now}"

How can I maintain the same string output as written, but avoid having the docker-compose interpret it as faulty string interpolation?

Anthon
  • 69,918
  • 32
  • 186
  • 246
Nathan Hanna
  • 4,643
  • 3
  • 28
  • 32
  • 1
    It is not the YAML parser that is interpreting that string. YAML doesn't know about `${}`. Interpreting is done by `docker-compose` and that is written in Python, so the tag ruby was inappropriate as well. – Anthon Nov 15 '16 at 23:05

2 Answers2

227

You can use a $$ (double-dollar sign) when your configuration needs a literal dollar sign.

You are hitting the docker-compose variable substitution, which is well documented here:

Both $VARIABLE and ${VARIABLE} syntax are supported. Extended shell-style features, such as ${VARIABLE-default} and ${VARIABLE/foo/bar}, are not supported.

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.

docker-compose is written in Python, as you see on github, the doubling mechanism to get the original meaning of special characters can be found in many programs, I needed to use this myself, while programming, as far back in 1984.

Anthon
  • 69,918
  • 32
  • 186
  • 246
  • what is the functional difference between the variable with and without the curly braces? `$VARIABLE` vs `${VARIABLE}`? – Zaffer Jul 24 '21 at 14:01
  • 1
    If the `$VARIABLE` is directly followed alphabetic character (`$VARIABLEX`, you'll have to use the more verbose `${VARIABLE}X` – Anthon Jul 24 '21 at 19:21
  • Note that `${VARIABLE-default}` *"extended shell-style feature"* is now supported (but still not `${VARIABLE/foo/bar}`) and that **Docker Compose V2** is no longer written in **Python** but in **Go**. – AymDev Nov 21 '22 at 10:28
50

Found the answer by copying the suggestion for % characters in this post

It requires a double dollar sign $$.

So I needed "$${Time.now}", which evaluates to "${Time.now}"

Nathan Hanna
  • 4,643
  • 3
  • 28
  • 32