60

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

FrancMo
  • 2,459
  • 2
  • 19
  • 39
  • 2
    Possible duplicate of [How can I escape a $ dollar sign in a docker compose file?](https://stackoverflow.com/questions/40619582/how-can-i-escape-a-dollar-sign-in-a-docker-compose-file) – kenorb Feb 16 '19 at 13:45

6 Answers6

89

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"
tomstan11
  • 1,047
  • 7
  • 9
15

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

Nicola Ben
  • 10,615
  • 8
  • 41
  • 65
  • 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. – Kaia Leahy Oct 01 '19 at 11:01
  • This used to work, but as of compose 2.7 values in .env files are expanded as well. So the solution is to wrap the value in single quotes within the .env file. More discussion on this compose issue: https://github.com/docker/compose/issues/9704 – Evan Byrne Aug 12 '22 at 18:03
9

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}
AdrieanKhisbe
  • 3,899
  • 8
  • 37
  • 45
fly2matrix
  • 2,351
  • 12
  • 13
7

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"

FantomX1
  • 1,577
  • 2
  • 15
  • 23
6

If you are using a colon sign in order to define a default value you gonna need to use the minus sign.
so instead of:

ENV_VAR=${ENV_VAR:100}

you wanna use:

ENV_VAR=${ENV_VAR:-100}
Tom Carmi
  • 385
  • 1
  • 5
  • 18
4

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}
adebasi
  • 3,535
  • 2
  • 19
  • 33