39

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
  • 1,804
  • 2
  • 15
  • 28
  • 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

5 Answers5

55

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"
tomscoding
  • 689
  • 6
  • 7
11

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
  • 9,055
  • 6
  • 36
  • 59
  • 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
8

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,719
  • 7
  • 35
  • 45
fly2matrix
  • 2,063
  • 10
  • 13
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
  • 2,895
  • 2
  • 17
  • 31
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"

FantomX1
  • 1,317
  • 2
  • 11
  • 20