下载安卓APP箭头
箭头给我发消息

客服QQ:3315713922

Dockerfile 中 ARG 的使用与其作用域(Scope)探究

作者:匿名     来源: 系统运维点击数:402发布时间: 2022-11-30 09:54:09

标签: DockerfileARG作用域

  使用 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:stage1​ vs stage2: 作用域在 FROM stage 内部

  在 stage2​ 的作用域中声明了 ARG image,且能正确输出结果。

  •   对照组3: stage2​ vs stage21​: 作用域仅在当前 FROM stage 内部

  虽然 stage2​ 在 stage21​ 上方且声明了 ARG image​, 但 stage21 仍然不能不能正确输出结果。

  0x02 实验过程

  创建 Dockerfile 如下:

  复制

     1.

  2.  ## 在第一个 FROM 之前的所有 ARG , 在所有 FROM 中生效, 仅在 FROM 中生效

  3.  ARG image

      4.

  5.  FROM $image as stage1

  6.  RUN echo "stage1 -> base from image is : $image "

  7.  # result: stage1 -> base from image is :

      8.

  9.  FROM $image as stage11

  10.  RUN echo "stage11 -> base from image is : $image "

  11.  # result: stage11 -> base from image is :

      12.

  13.  FROM alpine:3.12 as stage2

  14.  ## 在 FROM 后的 ARG, 仅在当前 FROM 作用域生效。即尽在当前 阶段 (stage) 生效

  15.  ARG image

  16.  RUN echo "stage2 -> base from image is : $image "

  17.   # stage2 -> base from image is : alpine:3.12

      18.

  19.  FROM alpine:3.12 as stage21

  20.  RUN echo "stage21 -> base from image is : $image "

  21.  # stage21 -> base from image is :

  执行docker build 命令:

  复制

  1.  # docker build --build-arg image=alpine:3.12 --no-cache .

  build 结果展示:

  复制

  1.  Sending build context to Docker daemon 3.072kB

  2.  Step 1/10 : ARG image

  3.  Step 2/10 : FROM $image as stage1

  4.  ---> d6e46aa2470d

  5.  Step 3/10 : RUN echo "stage1 -> base from image is : $image "

  6.  ---> Running in ecb7be5dd9cc

  7.  stage1 -> base from image is : ### image 结果未输出

  8.  Removing intermediate container ecb7be5dd9cc

  9.  ---> 04807c8d53be

  10.  Step 4/10 : FROM $image as stage11

  11.  ---> d6e46aa2470d

  12.  Step 5/10 : RUN echo "stage11 -> base from image is : $image "

  13.  ---> Running in a90e45076345

  14.  stage11 -> base from image is : ### image 结果未输出

  15.  Removing intermediate container a90e45076345

  16.  ---> f2dbce837a1b

  17.  Step 6/10 : FROM alpine:3.12 as stage2

  18.  ---> d6e46aa2470d

  19.  Step 7/10 : ARG image

  20.  ---> Running in 5c8cec4c2f22

  21.  Removing intermediate container 5c8cec4c2f22

  22.  ---> 999d9990bd91

  23.  Step 8/10 : RUN echo "stage2 -> base from image is : $image "

  24.  ---> Running in 4407dcb0e0bb

  25.  stage2 -> base from image is : alpine:3.12 ### image 结果输出

  26.  Removing intermediate container 4407dcb0e0bb

  27.  ---> e5ddd7a84f81

  28.  Step 9/10 : FROM alpine:3.12 as stage21

  29.  ---> d6e46aa2470d

  30.  Step 10/10 : RUN echo "stage21 -> base from image is : $image "

  31.  ---> Running in 64a0a3bb090c

  32.  stage21 -> base from image is : ### image 结果未输出

  33.  Removing intermediate container 64a0a3bb090c

  34.  ---> 82665f9a1037

  35.  Successfully built 82665f9a1037

  0x03 参考文档

  set-build-time-variables—build-arg

  0x04 to be continue

  在以后的时间, 笔者将继续讨论 ARG 在 docker buildx 多节构建时的影响和使用。

  来源: 高薪运维

  >>>>>>点击进入系统运维专题

赞(11)
踩(0)
分享到:
华为认证网络工程师 HCIE直播课视频教程