Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

docker

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.

like image 464
Emily Avatar asked Oct 17 '25 00:10

Emily


1 Answers

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
like image 110
marmikshah Avatar answered Oct 19 '25 13:10

marmikshah