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

Setting environment variable for docker container at runtime

我需要在运行时为 docker 容器设置环境变量。该值只能在运行时通过组合两个在运行时可用的其他变量来确定。在更简单的形式中,以下内容不起作用。

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

returns 下面当$host 和$port 的值可用于构造$url ?

时url 为空
...
host=name
url=:
port=123
...

您必须单引号 shell 命令以避免您的交互式 shell 在 docker shell 看到它们之前扩展变量:

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

使用单引号,您的 shell 会将 export url=$host:$port; env 传递给 Docker。

使用双引号,您当前的 shell 将首先尽职地扩展变量,因此只需传递 url=:; env