Dockerizing Go Applications
Go's static binaries shine in Docker — build a minimal image with scratch or distroless base, often under 20MB.
Introduction
Go's static binaries shine in Docker — build a minimal image with scratch or distroless base, often under 20MB. Multi-stage Dockerfile compiles in golang image, copies binary to alpine or scratch runtime.
Production Dockerfiles use non-root user, HEALTHCHECK, and .dockerignore excluding test files. CGO_ENABLED=0 produces fully static binary for scratch. Interviewers ask multi-stage builds, image size optimization, and K8s deployment basics.
This lesson provides production Dockerfile, docker-compose for local dev with Postgres, and build flags for reproducible containers.
The story
Cloudflare ships Go binaries in minimal scratch or distroless images — a statically compiled CGO_ENABLED=0 binary needs no libc, shrinking attack surface and image size from hundreds of MB to under 20 MB. Multi-stage Dockerfiles build with golang:1.22 and copy only the binary into the runtime stage.
Health checks, non-root users, and GOOS=linux GOARCH=amd64 cross-compilation are standard checklist items before pushing to ECR or GCR.
Understanding the topic
Key concepts
- Multi-stage: FROM golang AS builder → FROM scratch/distroless.
- CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w".
- COPY --from=builder /app/main /main — minimal runtime image.
- Non-root USER 65534:65534 for security.
- HEALTHCHECK curl or wget /health endpoint.
- .dockerignore excludes vendor, .git, *_test.go.
Step-by-step explanation
- Stage 1: copy go.mod, go.sum, download deps, copy source, build.
- Stage 2: copy binary only to minimal base image.
- Expose port; ENTRYPOINT ["/main"].
- docker build -t myapp . && docker run -p 8080:8080 -e DATABASE_URL=...
- docker-compose links app + postgres for local dev.
- Push to ECR/GCR; K8s Deployment pulls image.
Practical code example
Multi-stage Dockerfile for minimal production Go image:
# Dockerfile# Build stageFROM golang:1.22-alpine AS builderWORKDIR /appRUN apk add --no-cache git ca-certificatesCOPY go.mod go.sum ./RUN go mod downloadCOPY . .RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /main ./cmd/api# Runtime stageFROM gcr.io/distroless/static-debian12:nonrootCOPY --from=builder /main /mainCOPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/EXPOSE 8080USER nonroot:nonrootENTRYPOINT ["/main"]# docker-compose.yml snippet# services:# app:# build: .# ports: ["8080:8080"]# environment:# DATABASE_URL: postgres://user:pass@db:5432/app# db:# image: postgres:16-alpine
Line-by-line code explanation
FROM golang:1.22-alpine AS builderstarts the first stage from an image that has the Go 1.22 toolchain, and names the stage builder so later lines can copy files from it. WORKDIR /app sets the folder where the next commands run.RUN apk add --no-cache git ca-certificatesinstalls git (which Go may need to download modules) and CA certificates for HTTPS, without keeping the package index cache.COPY go.mod go.sum ./copies only the dependency files first, and RUN go mod download then fetches the modules. Docker can reuse this cached layer on later builds as long as those two files have not changed.RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /main ./cmd/apibuilds the program from the cmd/api package into /main. CGO_ENABLED=0 produces a statically linked binary, and -s -w leave out the symbol table and debug information to make it smaller.FROM gcr.io/distroless/static-debian12:nonrootstarts the second, final stage from a very small image with no shell or package manager. Only what is copied into this stage ends up in the final image.COPY --from=builder /main /maincopies the compiled binary out of the builder stage. The next line also copies the CA certificate bundle from the builder, so the program can verify HTTPS servers.USER nonroot:nonrootmakes the container run as a non-root user. EXPOSE 8080 above it only documents which port the app uses; it does not publish it.ENTRYPOINT ["/main"]sets the program that runs when the container starts. The array form runs the binary directly, without a shell.DATABASE_URL: postgres://user:pass@db:5432/appcomes from the commented docker-compose snippet. It points the app at a host named db, which is the name of the Postgres service defined below it in the same file.
Key takeaway: Distroless has no shell — smaller attack surface. -ldflags -s -w strips debug info. nonroot user is security best practice.
Real-world use
Where you'll use this in production
- Kubernetes Deployment container images.
- CI/CD pipeline build and push to container registry.
- Local dev environment with docker-compose stack.
- AWS Lambda custom runtime with provided.al2023 + bootstrap binary.
Best practices
- Multi-stage build — never ship compiler in production image.
- Pin base image digests for reproducibility.
- Run as non-root user.
- Include ca-certificates for HTTPS outbound calls.
- HEALTHCHECK for orchestrator readiness.
- Use BuildKit cache mounts for go mod download speed.
Common mistakes
- Single stage image — 800MB+ with full Go SDK.
- Running as root in container.
- Forgetting ca-certificates — TLS calls fail in scratch.
- Not using .dockerignore — slow builds copying .git.
- Baking secrets into image layers.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhy multi-stage Dockerfile?+
Answer
2BeginnerQuestionCGO_ENABLED=0 purpose?+
Answer
3IntermediateQuestionscratch vs alpine?+
Answer
4IntermediateQuestionOptimize Docker build cache?+
Answer
5AdvancedQuestionDesign CI pipeline building Go Docker image.+
Answer
Summary
Multi-stage Dockerfile produces minimal static binary images. CGO_ENABLED=0 and distroless base for secure tiny containers. Non-root user and HEALTHCHECK for production readiness. docker-compose for local app + database development. Next lesson: building microservices.