10

It seems that I am not understanding something about variable substitution in the following page (my variable NUM is not registering): https://github.com/compose-spec/compose-spec/blob/master/spec.md#Interpolation

See screenshot below. Running this on mac OSX.

docker-compose up

1
  • 2
    What versions of Docker and Docker Compose?
    – Andy Shinn
    Dec 22, 2015 at 3:35

3 Answers 3

5

Regarding docker-compose variable substitution, it can depend on how NUM has been set.
set NUM=5 would only set it in the current shell, not for another process.
Make sure to type:

export NUM=5
3
  • The question is referring compose's variable substitution mechanism, which uses variables from the host, not the container. Dec 22, 2015 at 15:02
  • @AdrianMouat Oh... you mean github.com/docker/compose/blob/…
    – VonC
    Dec 22, 2015 at 15:11
  • @AdrianMouat I have rewritten the answer accordingly.
    – VonC
    Dec 22, 2015 at 15:18
2

It is mentioned in the docs:

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 you forget and use a single dollar sign ($), Compose interprets the value as an environment variable and will warn you:

The VAR_NOT_INTERPOLATED_BY_COMPOSE is not set. Substituting an empty string.

According to that, line 03 of your compose file should be:

command: echo $$NUM
1

In addition to $$ solution provided by @ayman-nedjmeddine above you also need to do following, to make shell variables available in compose you have two options

Option 1

  • log in as root , set your variable and execute docker-compose
  • root>export NUM=5
  • root>docker-compose up

Option 2

  • use sudo -E from user shell, -E will propagate user shell env to sudo,
  • provide sudo access to docker/docker-compose
  • add :SETENV: to the command in sudoer file to use -E option in sudo
  • eg:

    sudo visudo -f /etc/sudoers.d/docker-compose
        ALL   ALL=(ALL:ALL) NOPASSWD:SETENV: /usr/local/bin/docker-compose
    sudo visudo -f /etc/sudoers.d/docker
        ALL   ALL=(ALL:ALL) NOPASSWD:SETENV: /usr/bin/docker
    
  • finally use

    user1>export NUM=5
    user1>sudo -E docker-compose up
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.