Olivier
Olivier

Reputation: 2162

Can't build Docker multi-stage image using ARG in COPY instruction

I'm trying to build a multi-stage image with Docker, where I'm using an external image as a stage. I'm trying to define the external image version with ARG or ENV, but it looks like it's not supported.

eg. This first version, without argument substitution is working

Dockerfile

FROM ubuntu:18.04

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

Docker build

$ 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

While this second version with argument substitution is NOT working 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 build

$ 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

Have you already experienced this? Cheers, Olivier

Upvotes: 5

Views: 3209

Answers (1)

Olivier
Olivier

Reputation: 2162

OK. I discovered it's an open issue on Docker side. https://github.com/moby/moby/issues/35018

--> ARG/ENV substitution is NOT working for -- values in ADD and COPY.

For my case, there is a work around while waiting for correction in 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

Upvotes: 10

Related Questions