0

I have a requirement to set the environment variable for docker container at runtime. The value can only be determined at runtime by combination two other variables which are available at runtime. In a simpler form, the following is not working.

docker run -ti -e host="name" -e port="123" centos:7 bash -c "export url=$host:$port; env"

returns the following where url is empty when the value of $host and $port is available to construct $url ?

...
host=name
url=:
port=123
...

1 Answer 1

2

You have to single quote the shell command to avoid your interactive shell expanding the variable before the docker shell sees them:

docker run -ti -e host="name" -e port="123" centos:7 bash -c 'export url=$host:$port; env'

With single quotes, your shell will pass export url=$host:$port; env to Docker.

With double quotes, your current shell will first dutifully expand the variables, and therefore just pass url=:; env

1
  • this works. I thought my question had an example of simplified form of problem that I am facing. Actually, I have a dockerfile with a custom entrypoint. This environment variable $host should be available in container. I can confirm from the logs that the $url is set during the start up of container. But when I bash into the container, this variable is empty.
    – nashter
    Jul 2, 2020 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.