Skip to content

Instantly share code, notes, and snippets.

@nanmu42
Last active July 27, 2023 10:18
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanmu42/90bf2d3870b64aec20b68ec3c104a610 to your computer and use it in GitHub Desktop.
Save nanmu42/90bf2d3870b64aec20b68ec3c104a610 to your computer and use it in GitHub Desktop.
Minimal-sized Golang Docker Image
FROM golang:1-alpine as golang
RUN apk --no-cache add git zip tzdata ca-certificates
# avoid go path, use go mod
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 go build -ldflags "-s -w -extldflags "-static"" ./cmd/appnamehere
WORKDIR /usr/share/zoneinfo
# -0 means no compression. Needed because go's
# tz loader doesn't handle compressed data.
RUN zip -r -0 /zoneinfo.zip .
FROM scratch
# the timezone data:
ENV ZONEINFO /zoneinfo.zip
COPY --from=golang /zoneinfo.zip /
# the tls certificates:
COPY --from=golang /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# Copy the binary
COPY --from=golang /app/appnamehere/
# example ENV: COS secret ID and secret key
ENV COS_SECRET_ID="" COS_SECRET_KEY=""
# example EXPOSE
EXPOSE 3030
ENTRYPOINT ["/appnamehere"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment