Go (Golang) Dockerfile Generator: Complete Guide
Ultra-Minimal Go Docker Images with Scratch
Go is uniquely suited for Docker because it compiles to a self-contained binary. By setting CGO_ENABLED=0 and using 'scratch' as the final image, you can produce production images under 15MB with zero attack surface — no shell, no package manager, no OS.
# Stage 1: Builder
FROM golang:1.22-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /app/main .
# Stage 2: Runner
FROM alpine:3.18
WORKDIR /app
COPY --from=builder /app/main .
EXPOSE 8080
CMD ["./main"]