Go (Golang) Tutorial 0/45 lessons ~6 min read Lesson 40

    Dockerizing Go Applications

    Go's static binaries shine in Docker — build a minimal image with scratch or distroless base, often under 20MB.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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.
    Visual overview
    Go Source
    go build
    Docker Image
    ECR / GCR
    Kubernetes Pod

    Step-by-step explanation

    1. Stage 1: copy go.mod, go.sum, download deps, copy source, build.
    2. Stage 2: copy binary only to minimal base image.
    3. Expose port; ENTRYPOINT ["/main"].
    4. docker build -t myapp . && docker run -p 8080:8080 -e DATABASE_URL=...
    5. docker-compose links app + postgres for local dev.
    6. Push to ECR/GCR; K8s Deployment pulls image.

    Practical code example

    Multi-stage Dockerfile for minimal production Go image:

    dockerfile
    # Dockerfile
    # Build stage
    FROM golang:1.22-alpine AS builder
    WORKDIR /app
    RUN apk add --no-cache git ca-certificates
    COPY go.mod go.sum ./
    RUN go mod download
    COPY . .
    RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /main ./cmd/api
    # Runtime stage
    FROM gcr.io/distroless/static-debian12:nonroot
    COPY --from=builder /main /main
    COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
    EXPOSE 8080
    USER nonroot:nonroot
    ENTRYPOINT ["/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 builder starts 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-certificates installs 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/api builds 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:nonroot starts 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 /main copies 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:nonroot makes 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/app comes 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.

    5 questions
    1BeginnerQuestionWhy multi-stage Dockerfile?+

    Answer

    Build with full SDK; runtime only binary — tiny secure image.
    2BeginnerQuestionCGO_ENABLED=0 purpose?+

    Answer

    Pure Go static binary without C deps — runs on scratch/distroless.
    3IntermediateQuestionscratch vs alpine?+

    Answer

    scratch empty — smallest; alpine has shell+apk — easier debug.
    4IntermediateQuestionOptimize Docker build cache?+

    Answer

    Copy go.mod/sum first, download deps, then copy source.
    5AdvancedQuestionDesign CI pipeline building Go Docker image.+

    Answer

    lint → test -race → build binary → docker build → scan → push tagged digest → deploy to staging.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.