gpt4 book ai didi

docker multistage build 因多个 --build-arg 而失败

转载 作者:行者123 更新时间:2023-12-04 06:38:39 28 4
gpt4 key购买 nike

我正在使用这个 Dockerfile

ARG IMAGE_ONE
FROM ${IMAGE_ONE}
RUN cat /etc/debian_version

ARG IMAGE_TWO
FROM ${IMAGE_TWO}
RUN cat /etc/debian_version

但它失败了,因为它没有使用第二个 var IMAGE_TWO:
$ docker build --no-cache --build-arg IMAGE_ONE=debian:7 --build-arg IMAGE_TWO=debian:8 .
Sending build context to Docker daemon 2.048kB
Step 1/6 : ARG IMAGE_ONE
Step 2/6 : FROM ${IMAGE_ONE}
---> 90c038768099
Step 3/6 : RUN cat /etc/debian_version
---> Running in f842d9cf4f17
7.11
Removing intermediate container f842d9cf4f17
---> 0f7f7afdd8a6
Step 4/6 : ARG IMAGE_TWO
---> Running in ed3d36f2f9cb
Removing intermediate container ed3d36f2f9cb
---> ae4ae3cabc02
Step 5/6 : FROM ${IMAGE_TWO}
--->
Step 6/6 : RUN cat /etc/debian_version
---> Running in 6f1c165e2765
OCI runtime create failed: container_linux.go:296:
starting container process caused "exec: \"/bin/sh\":
stat /bin/sh: no such file or directory": unknown

docker 版本:
$ docker --version
Docker version 17.12.0-ce, build c97c6d6

我的 Dockerfile 有问题还是 docker build 命令有问题?

最佳答案

原因是因为 IMAGE_TWO 不在同一个范围内检查这个
https://docs.docker.com/engine/reference/builder/#scope

基本上是 ARG IMAGE_TWO仍然是第一阶段的一部分,在该阶段结束时超出范围,不会成为第二阶段的一部分。

在开始时声明参数允许 IMAGE_TWO 处于第二阶段。

ARG IMAGE_ONE
ARG IMAGE_TWO
FROM ${IMAGE_ONE}
RUN cat /etc/debian_version

FROM ${IMAGE_TWO}
RUN cat /etc/debian_version
docker build --build-arg=IMAGE_ONE=debian:7 --build-arg=IMAGE_TWO=debian:8 .

关于docker multistage build 因多个 --build-arg 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48324659/

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