Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 在运行时为docker容器设置环境变量_Bash_Docker_Docker Compose_Centos_Dockerfile - Fatal编程技术网

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

Bash 在运行时为docker容器设置环境变量,bash,docker,docker-compose,centos,dockerfile,Bash,Docker,Docker Compose,Centos,Dockerfile,我需要在运行时为docker容器设置环境变量。该值只能在运行时通过组合运行时可用的两个其他变量来确定。在更简单的形式中,以下内容不起作用 docker run -ti -e host="name" -e port="123" centos:7 bash -c "export url=$host:$port; env" 返回以下内容,其中当$host和$port的值可用于构造$url时,url为空 。。。 主机名 url=: 端口=12

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

docker run -ti -e host="name" -e port="123" centos:7 bash -c "export url=$host:$port; env"
返回以下内容,其中当$host和$port的值可用于构造$url时,url为空

。。。
主机名
url=:
端口=123
...

您必须单引号引用shell命令,以避免在docker shell看到变量之前,交互式shell展开变量:

docker run -ti -e host="name" -e port="123" centos:7 bash -c 'export url=$host:$port; env'
使用单引号,shell将传递
export url=$host:$port;环境
发送给码头工人


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

您必须单引号引用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;环境
发送给码头工人


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

这是有效的。我想我的问题有一个我所面临的问题的简化形式的例子。实际上,我有一个带有自定义入口点的dockerfile。此环境变量$host应在容器中可用。我可以从日志中确认$url是在容器启动期间设置的。但是当我猛击容器时,这个变量是空的。我想我的问题有一个我所面临的问题的简化形式的例子。实际上,我有一个带有自定义入口点的dockerfile。此环境变量$host应在容器中可用。我可以从日志中确认$url是在容器启动期间设置的。但是当我猛击容器时,这个变量是空的。