Reputation: 1193
I'm using the build docker image from golang:alpine.
My purpose is just to copy the executed binary file to a new scratch image.
Below its my Dockerfile:
############################
# STEP 1 build executable binary
############################
FROM golang@sha256:d481168873b7516b9f34d322615d589fafb166ff5fd57d93e96f64787a58887c AS builder
RUN apk update && apk add --no-cache git tzdata ca-certificates && update-ca-certificates
ADD . $GOPATH/src/piggybank2go
WORKDIR $GOPATH/src/piggybank2go
COPY . .
# Fetch dependencies.
RUN go get -u github.com/golang/dep/cmd/dep
RUN dep ensure -v
# Build executeable binary
RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o $GOPATH/bin/piggybank2go
# RUN go build -o /go/bin/piggybank2go
############################
# STEP 2 build a small image
############################
FROM scratch
# Copy our static executable
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder $GOPATH/bin/piggybank2go $GOPATH/bin/piggybank2go
# Port on which the service will be exposed.
EXPOSE 8081
ENTRYPOINT ["$GOPATH/bin/piggybank2go"]
But I got this error:
Step 12/14 : COPY --from=builder $GOPATH/bin/piggybank2go $GOPATH/bin/piggybank2go
COPY failed: stat /var/lib/docker/overlay2/b37bbe725b51ba50e3082d162e75d4cdee368499e26887c6921486415c089920/merged/bin/piggybank2go: no such file or directory
Upvotes: 2
Views: 1640
Reputation: 10757
Environment variables from the first stage are not available in the second stage. For this reason "$GOPATH" cannot be resolved correctly in the second stage, hence the error.
In the second stage you should know exactly what and to where you are copying:
COPY --from=builder /go/bin/piggybank2go /go/bin/piggybank2go
Upvotes: 0
Reputation: 11425
I think the problem is that the environment variable $GOPATH
only exists in the golang image and not the scratch image. So try change the COPY
-line to:
COPY --from=builder /go/bin/piggybank2go /go/bin/piggybank2go
Upvotes: 3