gpt4 book ai didi

docker - 如何在运行时读取dockerfile中的值

转载 作者:行者123 更新时间:2023-12-02 19:16:43 24 4
gpt4 key购买 nike

我有一个用例,其中我在openshift配置映射中存储两个参数,如下所示-

DT_URL="https://www.something.com"
DT_TOKEN="1234ABCD"
现在,我希望使用 Jenkins 在运行时获取这些值。在我的dockerfile中,我的参数如下-
ARG URL=${DT_URL}
ARG TOKEN=${DT_TOKEN}
不幸的是我没有得到任何值(value)。我将不胜感激任何帮助。

最佳答案

ARG用于定义仅在图像构建期间有效的变量。ENV可用于定义在镜像构建时和容器运行时中存在的环境变量。
这里的问题是,使用ARGs选项在构建镜像时只能传递--build-arg
但是,没有什么能阻止您在Dockerfile中将ARG的值分配给ENV
这是一个例子:
创建一个Dockerfile

FROM alpine
ARG MY_ARG
ENV MY_ENV=$MY_ARG
RUN echo "the env is:" $MY_ENV
RUN echo "the arg is:" $MY_ARG
构建图像:
docker build -t arg-env-example --build-arg MY_ARG=some_value .
在构建期间,ARG和ENV都被分配了some_value:
...
Step 4/5 : RUN echo "the env is:" $MY_ENV
---> Running in 9a383134c927
the env is: some_value
Removing intermediate container 9a383134c927
---> 8a2d83609296
Step 5/5 : RUN echo "the arg is:" $MY_ARG
---> Running in b881d9f4208e
the arg is: some_value
...
运行容器并检查ARGENV:
docker run -it arg-env-example sh

/ # echo $MY_ENV
some_value << this is the value that was passed at build-time to MY_ARG
/ # echo $MY_ARG
<< nothing here :)
/ #

关于docker - 如何在运行时读取dockerfile中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63180142/

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