Emily
Emily

Reputation: 33

Why variable assignment doesn't work as expected in docker-compose commands?

I have the following in my docker-compose.yml:

my-service:
  image: amazon/aws-cli
  entrypoint: /bin/sh -c
  command: >
    '
      a=1899
      echo "The value of \"a\" is $a"
    '

And when I run it, I see The value of "a" is ., so for some reason, the variable assignment is not working as I would expect. Do you know what's going on?

I tried simplifying my docker compose to the very minimum, but still the same problem. I would expect that variable assignment and outputing would be the same than in a bash script.

Upvotes: 3

Views: 314

Answers (1)

votelessbubble
votelessbubble

Reputation: 805

You will need to escape the $ sign. If you do not escape the sign, then it will try to take the value from your host environment rather that from inside the docker container.

So you can change your command to

my-service:
  image: amazon/aws-cli
  entrypoint: /bin/sh -c
  command: >
    '
      a=1899
      echo "The value of \"a\" is $$a"
    '

If you want to pass it from host instead, you can do this instead

my-service:
  image: amazon/aws-cli
  entrypoint: /bin/sh -c
  command: >
    '
      echo "The value of \"a\" is $a"
    '
a=1899 docker-compose up

Upvotes: 1

Related Questions