docker多级构建失败,多个--build-arg

时间:2018-01-18 15:21:30

标签: docker containers dockerfile docker-image docker-multi-stage-build

我正在使用这个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命令错了吗?

1 个答案:

答案 0 :(得分:12)

原因是因为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 .