Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 932
FrancMo Avatar asked Apr 18 '18 10:04

FrancMo


5 Answers

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"
like image 174
tomscoding Avatar answered Nov 19 '22 01:11

tomscoding


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

like image 15
Nicola Ben Avatar answered Nov 19 '22 02:11

Nicola Ben


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}
like image 9
fly2matrix Avatar answered Nov 19 '22 01:11

fly2matrix


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"

like image 7
FantomX1 Avatar answered Nov 19 '22 00:11

FantomX1


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}
like image 4
adebasi Avatar answered Nov 19 '22 01:11

adebasi