Skip to content

Instantly share code, notes, and snippets.

@timoyuen
Forked from embano1/README.MD
Created March 18, 2021 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timoyuen/6b897cf5506e0a06eb59579ac3fdff3f to your computer and use it in GitHub Desktop.
Save timoyuen/6b897cf5506e0a06eb59579ac3fdff3f to your computer and use it in GitHub Desktop.
Dockerfile for Go modules and built cache efficiency

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"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment