我有一个用例,其中我在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
...
运行容器并检查ARG
和ENV
: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/