Skip to content

Instantly share code, notes, and snippets.

@civilizeddev
Created October 31, 2022 18:03
Show Gist options
  • Save civilizeddev/18072e0d36e45358a221ec98941377ea to your computer and use it in GitHub Desktop.
Save civilizeddev/18072e0d36e45358a221ec98941377ea to your computer and use it in GitHub Desktop.
Minimal Size Golang Docker Image
FROM golang:alpine as builder
COPY . .
RUN apk update && \
apk upgrade && \
apk add --no-cache ca-certificates && \
apk add --update-cache tzdata && \
update-ca-certificates
RUN echo "nobody:x:65534:65534:nobody:/:" > /etc_passwd
ENV GO111MODULE=on
ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GOARCH=amd64
WORKDIR /build
RUN go mod download
RUN go build -a -ldflags '-s' -o /main .
FROM scratch
COPY --from=builder /etc_passwd /etc/passwd
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /usr/local/go/lib/time/zoneinfo.zip /
COPY --from=builder /main .
ENV ZONEINFO=/zoneinfo.zip
EXPOSE 8080
USER nobody
ENTRYPOINT ["./main"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment