gpt4 book ai didi

docker - 使用 DockerFile 中的命令结果解析变量

转载 作者:行者123 更新时间:2023-12-05 00:58:14 25 4
gpt4 key购买 nike

我需要用命令的结果填充 dockerfile 中的变量

就像在 bash 中 var=$(date)

编辑 1

日期就是一个例子。
就我而言,我使用 FROM phusion/baseimage:0.9.17所以我想在每个建筑物使用最后一个版本所以我使用这个curl -v --silent api.github.com/repos/phusion/baseimage-docker/tags 2>&1 | grep -oh 'rel-.*",' | head -1 | sed 's/",//' | sed 's/rel-//' ==> 0.9.17。
但我不知道我如何用 dockerfile 在 var 中解析它以获得这个结果

ENV verbaseimage=curl...
FROM phusion/baseimage:$verbaseimage

结果

在我的用例中
FROM phusion/baseimage:latest

但是对于其他情况,这个问题仍未解决

最佳答案

旧的解决方法是 mentioned here (issue 2637: Feature request: expand Dockerfile ENV $VARIABLES in WORKDIR ) :

One work around that I've used, is to have a file in my context called "build-env". What I do is source it and run my desired command in the same RUN step. So for example:


build-env :
VERSION=stable
Dockerfile :
FROM radial/axle-base:latest
ADD build-env /build-env
RUN source build-env && mkdir /$VERSION
RUN ls /

但是对于 date ,这可能不像您想要的那么精确。

其他解决方法在 issue 2022 "Dockerfile with variable interpolation" .

在 docker 1.9(2015 年 10 月末)中,您将拥有“ support for build-time environment variables to the 'build' API (PR 9176)”和“ Support for passing build-time variables in build context (PR 15182)”。
docker build --build-arg=[]: Set build-time variables

您可以在 Dockerfile 中使用 ENV 指令来定义变量值。这些值保留在构建的镜像中。然而,往往坚持并不是你想要的。用户希望根据他们构建镜像的主机以不同方式指定变量。

A good example is http_proxy or source versions for pulling intermediate files. The ARG instruction lets Dockerfile authors define values that users can set at build-time using the ---build-arg flag:


$ docker build --build-arg HTTP_PROXY=http://10.20.30.2:1234 .

This flag allows you to pass the build-time variables that are accessed like regular environment variables in the RUN instruction of the Dockerfile.
Also, these values don't persist in the intermediate or final images like ENV values do.



so I want at each building use the last version so I use this


curl -v --silent api.github.com/repos/phusion/baseimage-docker/tags 2>&1 | grep -oh 'rel-.*",' | head -1 | sed 's/",//' | sed 's/rel-//' ==> 0.9.17. 

如果您想使用该图像的最新版本,您需要做的就是使用标签“ latest”。 ' 与 FROM directive :
FROM phusion/baseimage:latest

另请参阅“ The misunderstood Docker tag: latest ”:它并不总是引用实际的最新版本,但在这种情况下,它应该可以工作。

如果您真的想使用 curl|parse 选项,请使用它生成具有正确值的 Dockerfile(就像在模板中处理以生成正确的文件一样)。
不要尝试直接在 Dockerfile 中使用它。

关于docker - 使用 DockerFile 中的命令结果解析变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33201271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com