无法在COPY指令中使用ARG构建Docker多阶段镜像

5

我正在尝试使用Docker构建一个多阶段镜像,在其中使用外部镜像作为一个阶段。我试图使用ARG或ENV定义外部镜像的版本,但似乎不支持。

例如,这个第一个版本没有参数替换,可以工作

Dockerfile

FROM ubuntu:18.04

# This is working
COPY --from=hello-world:latest /hello /hello

Docker构建
$ docker build --no-cache -t test .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM ubuntu:18.04
 ---> 16508e5c265d
Step 2/2 : COPY --from=hello-world:latest /hello /hello
 ---> 2d52b43d730b
Successfully built 2d52b43d730b
Successfully tagged test:latest

尽管这个使用参数替换的第二个版本无法工作,Dockerfile仍然如下所示:

FROM ubuntu:18.04

ARG HELLO_VERSION
ENV HELLO_VERSION ${HELLO_VERSION:-latest}

RUN echo "HELLO_VERSION" $HELLO_VERSION
# This argument substitution is NOT working --I tried both ARG and ENV separately
COPY --from=hello-world:${HELLO_VERSION} /hello /hello

Docker构建
$ docker build --no-cache -t test .
Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM ubuntu:18.04
 ---> 16508e5c265d
Step 2/5 : ARG HELLO_VERSION
 ---> Running in bf1c94ecd0ea
Removing intermediate container bf1c94ecd0ea
 ---> 33608ed5d441
Step 3/5 : ENV HELLO_VERSION ${HELLO_VERSION:-latest}
 ---> Running in 6bf864ba9e4f
Removing intermediate container 6bf864ba9e4f
 ---> d08f20e7ccb6
Step 4/5 : RUN echo "HELLO_VERSION" $HELLO_VERSION
 ---> Running in cd973f372eb4
HELLO_VERSION latest
Removing intermediate container cd973f372eb4
 ---> b0893a822140
Step 5/5 : COPY --from=hello-world:${HELLO_VERSION} /hello /hello
invalid from flag value hello-world:${HELLO_VERSION}: invalid reference format

你已经遇到过这个问题了吗? 祝好, Olivier
1个回答

9

好的,我发现这是Docker方面的一个公开问题。 https://github.com/moby/moby/issues/35018

--> 在ADDCOPY中,对于--值,ARG/ENV替换不起作用。

对于我的情况,在等待Docker修正之际,有一个解决方法。 https://github.com/docker/cli/issues/996

ARG HELLO_VERSION
FROM hello-world:${HELLO_VERSION:-latest} as hello

FROM ubuntu:18.04
COPY --from=hello /hello /hello

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接