Skip to content

Instantly share code, notes, and snippets.

@phrozen
Created February 22, 2021 21:56
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 phrozen/ae36c0086bc5f3eb1c8e630d3b619c37 to your computer and use it in GitHub Desktop.
Save phrozen/ae36c0086bc5f3eb1c8e630d3b619c37 to your computer and use it in GitHub Desktop.
Golang Dockerfile template for binary only images
# 1. Build a statically linked binary
FROM golang:alpine as builder
# Install git to install dependencies (skip if vendored)
RUN apk add --update --no-cache git
# Copy source code to working directory
COPY . /src
WORKDIR /src
# Add some env vars for the build process
ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GOARCH=amd64
# Build the statically linked binary
RUN go build -ldflags="-w -s" -o /app
# 2. Build a new image from scratch with just the app binary
FROM scratch
COPY --from=builder /app /app
# 3. Run the app binary.
ENTRYPOINT ["/app"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment