Answer a question

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
...

Answers

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

Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐