Production-ready Docker file for Golang app

An example of a production-ready multi-stage Docker file for any Golang project. The first stage will build a binary file with the Golang Alpine image. In the second stage, the binary file will be copied from the previous image(golang-alpine) to a new image created from Alpine. So the final image size is small (~5mb alpine + binary file) as no go compiler installation. I don’t use Scratch image as I can’t ssh into the running containers if I need to debug halt containers in prod.

# Build stage
FROM golang:1.15-alpine3.12 AS gobuilder

RUN apk add --no-cache curl git tzdata

# Set timezone info if you need to
# Set Environment valiables you need 

WORKDIR /usr/local/app
COPY . .

RUN go mod vendor
RUN CGO_ENABLED=0 go build -a -o goapp ./cmd

# Final stage
FROM alpine:3.12

RUN apk --no-cache add ca-certificates tzdata
WORKDIR /usr/local/app
COPY . .
COPY --from=gobuilder /usr/local/app/goapp /usr/local/app/
RUN chmod +x /usr/local/app/goapp
WORKDIR /usr/local/app
ENTRYPOINT ["./goapp"]



, ,