Also works across git branches if you keep the intermediate build images around, e.g. those <none>
images in the docker images
output.
FROM golang:1.12 AS builder
# enable Go modules support
ENV GO111MODULE=on
WORKDIR $GOPATH/src/github.com/myrepo/myapp
# manage dependencies
COPY go.mod .
COPY go.sum .
RUN go mod download
# Copy src code from the host and compile it
COPY cmd cmd
COPY pkg pkg
RUN CGO_ENABLED=0 GOOS=linux go build -a -o /myapp cmd/main.go
FROM alpine:3.9
RUN apk --no-cache add ca-certificates
COPY --from=builder /myapp /bin
CMD ["/bin/myapp"]
May I suggest this for security's sake: