简体   繁体   English

在运行时为 docker 容器设置环境变量

[英]Setting environment variable for docker container at runtime

I have a requirement to set the environment variable for docker container at runtime.我需要在运行时为 docker 容器设置环境变量。 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 和 $port 的值可用于构造 $url 时,返回以下 url 为空?

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

You have to single quote the shell command to avoid your interactive shell expanding the variable before the docker shell sees them: 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使用单引号,您的 shell 将通过export url=$host:$port; env export url=$host:$port; env to Docker.环境到export url=$host:$port; env

With double quotes, your current shell will first dutifully expand the variables, and therefore just pass url=:; env使用双引号,您当前的 shell 将首先尽职尽责地扩展变量,因此只需传递url=:; env url=:; env

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM