Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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
Command line Docker环境变量不工作?_Command Line_Dockerfile - Fatal编程技术网

Command line Docker环境变量不工作?

Command line Docker环境变量不工作?,command-line,dockerfile,Command Line,Dockerfile,我在将Dockerfile中的环境变量附加到入口点时遇到问题 由于Dockerfile包含敏感数据,我将尝试展示一个类似的示例: FROM ubuntu:latest . . . ENV test_var hello ENTRYPOINT ["keyword"] CMD ["sh", "-c", "echo $test_var"] 我从以下位置获得CMD执行: 和 在我看来,跑步时: sudo docker run -it nameOfImage 关键字应在附加$test_var的

我在将Dockerfile中的环境变量附加到入口点时遇到问题

由于Dockerfile包含敏感数据,我将尝试展示一个类似的示例:

FROM ubuntu:latest
.
.
.
ENV test_var hello

ENTRYPOINT ["keyword"] 

CMD ["sh", "-c", "echo $test_var"]
我从以下位置获得CMD执行: 和

在我看来,跑步时:

sudo docker run -it nameOfImage 
关键字应在附加$test_var的情况下执行。以下两项中的任何一项都应起作用:

keyword $test_var

keyword hello
然而,这对我来说根本不起作用。使用运行时,环境变量正在正确替换:

RUN echo $test_var

但是,CMD无法使用它。有什么建议吗?谢谢。

我认为Dockerfile不允许您以这种方式执行替换,但是有一种解决方法,将shell脚本传递到入口点

Dockerfile

entrypoint.sh

你会看到结果的

hello

谢谢你的回复,我应该澄清一下,如果“关键字”对程序有影响的话,它是一个可执行文件。这样做是否与:sudo docker run-it nameOfImage paramater1 parameter2相同?如果只想简单地打印环境变量(如test_var)的值,可以执行
sudo docker run nameOfImage printenv test_var
。假设您仍在使用Dockerfile,则只需使用我的Dockerfile的FROM和ENV行即可。如果您想使用我完整发布的Dockerfile,但想打印其他变量的值,可以执行
sudo docker run nameOfImage
#/bin/bash
echo $(printenv $1) #in this case 'echo' is 'keyword'
hello