Build stage referenced by number

Use a descriptive stage name in COPY --from.

Description

COPY --from=0 selects the first build stage. Adding or reordering stages can change which stage that number selects.

Potential impact

A later build may copy the wrong binary or fail to find it.

Remediation

Give the producing stage a unique name with AS builder. Refer to it with COPY --from=builder wherever you need its output.

Examples

Set GO_BUILD_IMAGE and RUNTIME_IMAGE to supported images. Check that /src/app can run with the target architecture and runtime libraries.

Before

dockerfile
ARG GO_BUILD_IMAGE
ARG RUNTIME_IMAGE
FROM ${GO_BUILD_IMAGE}
WORKDIR /src
COPY . .
RUN go build -o app .

FROM ${RUNTIME_IMAGE}
COPY --from=0 /src/app /app/app
CMD ["/app/app"]

After

dockerfile
ARG GO_BUILD_IMAGE
ARG RUNTIME_IMAGE
FROM ${GO_BUILD_IMAGE} AS builder
WORKDIR /src
COPY . .
RUN go build -o app .

FROM ${RUNTIME_IMAGE}
COPY --from=builder /src/app /app/app
CMD ["/app/app"]

References