【问题标题】:Can't build Docker multi-stage image using ARG in COPY instruction无法在 COPY 指令中使用 ARG 构建 Docker 多阶段映像
【发布时间】:2018-08-23 08:57:44
【问题描述】:

我正在尝试使用 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

您是否已经经历过这种情况? 干杯, 奥利维尔

【问题讨论】:

    标签: docker dockerfile


    【解决方案1】:

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

    --> ARG/ENV 替换不适用于 ADDCOPY 中的 -- 值。

    就我而言,在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 2021-11-06
      • 2021-10-17
      • 1970-01-01
      • 2020-09-08
      • 2020-05-21
      相关资源
      最近更新 更多