bash Dockerfile 覆盖 ENV 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40334751/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:21:15  来源:igfitidea点击:

Dockerfile Overriding ENV variable

bashdockerenvironment-variables

提问by Proximo

I have a Dockerfile and I'd like to make the API configurable with a default value.

我有一个 Dockerfile,我想让 API 使用默认值进行配置。

FROM socialengine/nginx-spa

ENV API_URL localhost:6007

来自 socialengine/nginx-spa

ENV API_URL 本地主机:6007

So when I run this image I'd to be able to override the localhost:6007 with something like below:

因此,当我运行此图像时,我将能够使用以下内容覆盖 localhost:6007:

docker run -e API_URL=production.com:6007 ui

docker run -e API_URL=production.com:6007 ui

This doesn't work and I can't find a clear explanation of how to do this.

这不起作用,我找不到有关如何执行此操作的明确解释。

Any advice?

有什么建议吗?

回答by larsks

What you have described should work just fine. Given:

你所描述的应该工作得很好。鉴于:

$ cat Dockerfile
FROM socialengine/nginx-spa
ENV API_URL localhost:6007
$ docker build -t ui .
[...]

Consider this:

考虑一下:

$ docker run -it --rm ui env | grep API_URL
API_URL=localhost:6007

Compared to:

相比:

$ docker run -it --rm -e API_URL='production:6007' ui env | grep API_URL
API_URL=production:6007

Passing a -e VARNAME=varvalueon the docker runcommand line will override a default set in your Dockerfile.

-e VARNAME=varvaluedocker run命令行上传递 a将覆盖 Dockerfile 中的默认设置。

If you are seeing different behavior, please update your question to show exactly the command you are running and the associated output.

如果您看到不同的行为,请更新您的问题以准确显示您正在运行的命令和相关的输出。

Update

更新

Here is the complete example, recorded for your viewing pleasure:

这是完整的示例,为您的观看乐趣而记录:

https://asciinema.org/a/a5a2n3exlyh4jkii4k0ivvqmd

https://asciinema.org/a/a5a2n3exlyh4jkii4k0ivvqmd