原创
Dockerfile 中 ARG 的使用与其作用域(scope)探讨
使用 ARG 可以有用的复用 Dockerfile。每次镜像更新,只需要动态的在 build 下令中传入新的参数值即可。
0x01 结论
- 在第一个 FROM 之前的所有 ARG , 在所有 FROM 中生效, 仅在 FROM 中生效
- 在 FROM 后的 ARG, 仅在当前 FROM 作用域生效。即尽在当前 阶段 (stage) 生效
对照组剖析
在随后的 Dockerfile 中, 只界说了一个变量 image , 并在 FROM 和 stage 中重复使用
- 对照组1: stage1 和 stage11 均在 FROM 中使用了变量 $image: 作用域在所有 FROM 中
- 乐成拉取 FROM $image 并完成 layer 构建
- 但是在 RUN 中无法准确输出效果,即 image 的值 alpine:3.12
2 . 对照组2: stage1 vs stage2: 作用域在 FROM stage 内部 - 在 stage2 的作用域中声明晰 ARG image,且能准确输出效果。
3 . 对照组3: stage2 vs stage21: 作用域仅在当前 FROM stage 内部 - 虽然 stage2 在 stage21 上方且声明晰 ARG image, 但 stage21 仍然不能不能准确输出效果。
0x02 实验历程
- 建立 Dockerfile 如下
## 在第一个 FROM 之前的所有 ARG , 在所有 FROM 中生效, 仅在 FROM 中生效
ARG image
FROM $image as stage1
RUN echo "stage1 -> base from image is : $image "
# result: stage1 -> base from image is :
FROM $image as stage11
RUN echo "stage11 -> base from image is : $image "
# result: stage11 -> base from image is :
FROM alpine:3.12 as stage2
## 在 FROM 后的 ARG, 仅在当前 FROM 作用域生效。即尽在当前 阶段 (stage) 生效
ARG image
RUN echo "stage2 -> base from image is : $image "
# stage2 -> base from image is : alpine:3.12
FROM alpine:3.12 as stage21
RUN echo "stage21 -> base from image is : $image "
# stage21 -> base from image is :
- 执行 docker build 下令
# docker build --build-arg image=alpine:3.12 --no-cache .
- build 效果展示
Sending build context to Docker daemon 3.072kB
Step 1/10 : ARG image
Step 2/10 : FROM $image as stage1
---> d6e46aa2470d
Step 3/10 : RUN echo "stage1 -> base from image is : $image "
---> Running in ecb7be5dd9cc
stage1 -> base from image is : ### image 效果未输出
Removing intermediate container ecb7be5dd9cc
---> 04807c8d53be
Step 4/10 : FROM $image as stage11
---> d6e46aa2470d
Step 5/10 : RUN echo "stage11 -> base from image is : $image "
---> Running in a90e45076345
stage11 -> base from image is : ### image 效果未输出
Removing intermediate container a90e45076345
---> f2dbce837a1b
Step 6/10 : FROM alpine:3.12 as stage2
---> d6e46aa2470d
Step 7/10 : ARG image
---> Running in 5c8cec4c2f22
Removing intermediate container 5c8cec4c2f22
---> 999d9990bd91
Step 8/10 : RUN echo "stage2 -> base from image is : $image "
---> Running in 4407dcb0e0bb
stage2 -> base from image is : alpine:3.12 ### image 效果输出
Removing intermediate container 4407dcb0e0bb
---> e5ddd7a84f81
Step 9/10 : FROM alpine:3.12 as stage21
---> d6e46aa2470d
Step 10/10 : RUN echo "stage21 -> base from image is : $image "
---> Running in 64a0a3bb090c
stage21 -> base from image is : ### image 效果未输出
Removing intermediate container 64a0a3bb090c
---> 82665f9a1037
Successfully built 82665f9a1037
0x03 参考文档
- set-build-time-variables—build-arg
0x04 to be continue
在以后的时间, 笔者将继续讨论 ARG 在 docker buildx 多节构建时的影响和使用。
,,欢迎进入Allbet电脑版下载(Allbet Game):www.aLLbetgame.us,欧博官网是欧博集团的官方网站。欧博官网开放Allbet注册、Allbe代理、Allbet电脑客户端、Allbet手机版下载等业务。
©著作权归作者所有:来自51CTO博客作者mb5fdcad8719a20的原创作品,如需转载,请注明出处,否则将追究法律责任
Java
0
珍藏
上一篇:使用 docker buildx... 下一篇:Dockerfile构建指南-怎...
环球UG官网声明:该文看法仅代表作者自己,与本平台无关。转载请注明:电银付app下载(dianyinzhifu.com):Dockerfile 中 ARG 的使用与其作用域(scope)探讨
欧博网址www.allbetgame.us欢迎进入欧博网址(Allbet Gaming),欧博网址开放会员注册、代理开户、电脑客户端下载、苹果安卓下载等业务。本可爱喜欢~