bash Docker Run 和变量替换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35555190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Docker Run and variable substitution
提问by Abimbola Esuruoso
I have a variable in a script called image_name
我在一个名为的脚本中有一个变量 image_name
image_name="my-app"
And I am attempting to pass this variable to a docker container as seen below:
我正在尝试将此变量传递给 docker 容器,如下所示:
docker build -t $image_name .
docker run --rm --name $image_name -e "app_dir=${image_name}" $image_name
But this variable is not set and is blank within the context of the docker build
但是这个变量没有设置并且在 docker build 的上下文中是空白的
This is a snippet from the Dockerfile below:
这是来自以下 Dockerfile 的片段:
....
RUN echo dir is $app_dir
....
This is a snippet of the build output below:
这是以下构建输出的片段:
....
Step 2 : RUN echo dir is $app_dir
---> Running in db93a939d701
dir is
---> c9f5e2a657d5
Removing intermediate container db93a939d701
....
Anyone know how to do the variable substitution?
有人知道如何进行变量替换吗?
采纳答案by Luís Bianchin
Notice that you are using the variable inside the Dockerfile
. That variable will be evaluated at build time (docker build
), so you need to pass its value at that moment. That can be achieved with the --build-arg
param.
请注意,您正在使用Dockerfile
. 该变量将在构建时 ( docker build
)进行评估,因此您需要在那时传递其值。这可以通过--build-arg
参数实现。
Changing your example, it would be:
更改您的示例,它将是:
docker build --build-arg app_dir=${image_name} -t $image_name .
docker run --rm --name $image_name $image_name
回答by Abimbola Esuruoso
This is achievable using the build-arg
parameter of the docker build
command
这可以使用命令的build-arg
参数来实现docker build
See: https://github.com/docker/docker/issues/6822#issuecomment-168170031for an example
参见:https: //github.com/docker/docker/issues/6822#issuecomment-168170031示例