Search code examples
docker-compose

How to do variable substitution in env_file?


I am using docker-compose and I want my env_file to have variable subsitution. I would like to define one variable in the file and then use it in other variables:

APP_ENV=dev-foo
LOCALPATH=/tmp/builddir/${APP_ENV}
GIT_URL=git@github.com:some-org/${APP_ENV}

What is the supported form of variable substitutions in env_file???


Solution

  • Variable substitution is now supported in docker compose

    The syntax is similar to shell substitution.

    Direct substitution

    ${VAR} -> value of VAR
    

    Default value

    ${VAR:-default} -> value of VAR if set and non-empty, otherwise default
    ${VAR-default} -> value of VAR if set, otherwise default
    

    Required value

    ${VAR:?error} -> value of VAR if set and non-empty, otherwise exit with error
    ${VAR?error} -> value of VAR if set, otherwise exit with error
    

    Alternative value

    ${VAR:+replacement} -> replacement if VAR is set and non-empty, otherwise empty
    ${VAR+replacement} -> replacement if VAR is set, otherwise empty
    

    EDIT: Do notice that using docker-compose is deprecated and will not use the latest version. Use docker compose (no dash), which will use the latest compose version.