Docker compose: Invalid interpolation format for "environment" option in service

41,455

Solution 1

For anyone getting this error while trying to pass the DJANGO Secret Key, if your secret key contains '$' add another '$ after it'

DJANGO_SECRET_KEY: "tj...........t2$8" # Original Key
DJANGO_SECRET_KEY: "tj...........t2$$8"

Solution 2

Try with an ENV file.

$ cat ./Docker/api/api.env
NODE_ENV=test
$ cat docker-compose.yml
version: '3'
services:
  api:
    image: 'node:6-alpine'
    env_file:
     - ./Docker/api/api.env
    environment:
     - NODE_ENV=production

You can escape the $ symbol with another $ [like this $$() ] Reference at: https://docs.docker.com/compose/environment-variables/#the-env-file

Solution 3

If the aws command line utility is embedded inside the container then you can rewrite the commands like this.

environment:
  - AWS_ACCESS_KEY_ID=$$(aws --profile default configure get aws_access_key_id)
  - AWS_SECRET_ACCESS_KEY=$$(aws --profile default configure get aws_secret_access_key)


And if this aws utility is on the host system then
you can set some environment variables on your shell like (.profile or .bashrc etc)

export HOST_ACCESS_KEY_ID=$(aws --profile default configure get aws_access_key_id)
export HOST_AWS_SECRET_ACCESS_KEY=$(aws --profile default configure get aws_secret_access_key)


and then reuse it in docker-compose.yml like

environment:
  - AWS_ACCESS_KEY_ID=${HOST_ACCESS_KEY_ID}
  - AWS_SECRET_ACCESS_KEY=${HOST_AWS_SECRET_ACCESS_KEY}

Solution 4

My issue was caused by the fact, that I had been using v2 of docker-file, whereas such an environment option needed to have defined in the header version 3, instead of 2

version: "3"

Solution 5

AFAIK it is not possible to do this in docker-compose or .env files. But you can set an environment variable and reference that one in your docker-compose file:

$ export AWS_ACCESS_KEY_ID=$(aws --profile default configure get aws_access_key_id)

docker-compose.yaml

environment:
      - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
Share:
41,455
FrancMo
Author by

FrancMo

Updated on October 28, 2020

Comments

  • FrancMo
    FrancMo over 2 years

    Hi in docker compose I have:

     environment:
          - AWS_ACCESS_KEY_ID=$(aws --profile default configure get aws_access_key_id)
          - AWS_SECRET_ACCESS_KEY=$(aws --profile default configure get aws_secret_access_key)
    

    But it returns me an error like in topic. Anyone knows how to pass those variables ?

    Thanks

  • Kaia Leahy
    Kaia Leahy over 3 years
    This does not address the specific problem, which is that they're trying to assign an ENV variable based on the results of a command.