COPY --from references its own build stage

Remove self-references from COPY --from build dependencies.

Description

A build stage cannot use COPY --from to read its own output. That creates a circular dependency.

Potential impact

Docker stops the build before it can produce the runtime image.

Remediation

Use plain COPY for sources in the build context. In the consuming stage, name the producing stage in COPY --from and check the artifact’s path. If the source is an external image or named context, verify that reference instead.

Examples

Supply the Go source and module files. Build a binary compatible with the target architecture and Alpine’s runtime libraries.

Before

dockerfile
FROM golang:1.22 AS builder
WORKDIR /src
COPY --from=builder /src/app /app
RUN go build -o /src/app .

After

dockerfile
FROM golang:1.22 AS builder
WORKDIR /src
COPY . .
RUN go build -o /src/app .

FROM alpine:3.20
WORKDIR /app
COPY --from=builder /src/app /app/app
CMD ["/app/app"]

The builder produces /src/app. The Alpine stage copies it to /app/app, and CMD makes that binary the default command when the container starts.

References