Step Over to Go Part 38 of 50
  1. 1 Goodbye .csproj, Hello go.mod
  2. 2 The Phrasebook: C# Concepts in Go Terms
  3. 3 You Know .NET, Now Meet the Go Toolchain
  4. 4 No Enums? No Problem (Sort Of)
  5. 5 Generics: Late to the Party
  6. 6 The Million Dollar Mistake, Differently
  7. 7 Your Class Is Now a Struct (And That's Fine)
  8. 8 Where Did My Properties Go?
  9. 9 if err != nil: Your New Reality
  10. 10 Panic and Recover: The Emergency Exit
  11. 11 The switch Statement You Always Wanted
  12. 12 Stack vs Heap: Go's Escape Analysis
  13. 13 Pointers Without the Pain
  14. 14 Slices Are Not Arrays (And Neither Is List<T>)
  15. 15 Channels: The sync You Didn't Know You Wanted
  16. 16 Context Is King
  17. 17 Async/Await vs Goroutines: A Mindset Shift
  18. 18 select: Multiplexing Like a Pro
  19. 19 Mutexes and WaitGroups: When Channels Aren't the Answer
  20. 20 The Empty Interface and Type Assertions
  21. 21 Composition Over Inheritance (For Real This Time)
  22. 22 Implicit Interfaces: Scary Then Brilliant
  23. 23 Benchmarks and Profiling Out of the Box
  24. 24 Mocking in a Language Without Mockito
  25. 25 Multi-Module Repos: Monorepo Thinking
  26. 26 Organising a Real Project
  27. 27 Testing Without a Framework
  28. 28 Configuration Without IOptions<T>
  29. 29 Database Access: database/sql vs Entity Framework
  30. 30 Debugging: Delve vs Visual Studio's Comfort
  31. 31 Generate All the Things
  32. 32 GORM and Friends: When You Do Want an ORM
  33. 33 HTTP Services: net/http vs ASP.NET Core
  34. 34 JSON: Struct Tags and the Marshal Dance
  35. 35 Linting and Formatting: gofmt Is Non-Negotiable
  36. 36 Logging: slog and the Structured Logging Story
  37. 37 Shrinking Binaries and Build Tags
  38. 38 Dockerfile for Go: Simpler Than You'd Think
  39. 39 GitHub Actions for Go Projects
  40. 40 Health Checks and Readiness Probes
  41. 41 Kubernetes and Go: A Natural Fit
  42. 42 AWS Lambda and Go: Cold Starts That Don't Hurt
  43. 43 Metrics, Traces, and Logs: The OpenTelemetry Way
  44. 44 Single Binary Deploys: The Killer Feature
  45. 45 Versioning Your Modules
  46. 46 What I Actually Miss From C#
  47. 47 What I'll Never Go Back For
  48. 48 Six Months In: Was It Worth It?
  49. 49 Forty-Eight Posts on Moving From C# to Go, in the Right Order
  50. 50 Range-Over-Func Iterators for People Who Miss LINQ
About this series

Dockerfile for Go: Simpler Than You'd Think

Go's static binaries make for tiny Docker images. Where a .NET container might be 200MB+, a Go container can be under 20MB. Sometimes under 10MB.

Update, November 2026: The golang:1.22 and alpine:3.19 pins below are out of support. Pin golang:1 and let go.mod choose the toolchain. What I got wrong about Go has the details, checked against Go 1.27.

This isn't just about bragging rights. Smaller images mean faster pulls, faster scaling, smaller attack surface, and lower storage costs.

The Simplest Dockerfile

FROM golang:1.22 AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /app/server ./cmd/server

FROM scratch
COPY --from=builder /app/server /server
ENTRYPOINT ["/server"]

That's it. A multi-stage build producing a minimal image.

What's Happening

Stage 1: Build

  • Start from official Go image
  • Copy and download dependencies first (layer caching)
  • Copy source and build
  • CGO_ENABLED=0 ensures static linking

Stage 2: Run

  • scratch is an empty image (literally nothing)
  • Copy just the binary
  • Run it

Result: an image containing only your binary.

Image Size Comparison

docker images
REPOSITORY          TAG       SIZE
myapp-go            latest    12MB
myapp-dotnet-aot    latest    45MB
myapp-dotnet-scd    latest    210MB

Go with scratch: ~10-15MB .NET AOT: ~40-80MB .NET self-contained: ~200-300MB

The scratch vs distroless Decision

scratch

Literally empty. No shell, no utilities, no nothing.

FROM scratch
COPY myapp /myapp
ENTRYPOINT ["/myapp"]

Pros:

  • Smallest possible
  • Minimal attack surface
  • Nothing to exploit

Cons:

  • No shell for debugging (docker exec won't help)
  • No CA certificates (HTTPS fails)
  • No timezone data

distroless

Google's minimal images, just enough to run binaries:

FROM gcr.io/distroless/static-debian12
COPY myapp /myapp
ENTRYPOINT ["/myapp"]

Pros:

  • Includes CA certificates
  • Includes timezone data
  • Still very small (~2MB base)
  • Some debugging possible

Cons:

  • Slightly larger than scratch
  • Still no shell

alpine

Minimal Linux with shell:

FROM alpine:3.19
RUN apk add --no-cache ca-certificates tzdata
COPY myapp /myapp
ENTRYPOINT ["/myapp"]

Pros:

  • Has a shell
  • Easy debugging
  • Package manager available
  • ~5MB base

Cons:

  • Uses musl libc (usually fine for Go)
  • Larger than scratch/distroless
  • More attack surface

A Production Dockerfile

# Build stage
FROM golang:1.22-alpine AS builder

WORKDIR /app

# Dependencies first for layer caching
COPY go.mod go.sum ./
RUN go mod download

# Copy source
COPY . .

# Build with optimizations
ARG VERSION=dev
ARG COMMIT=unknown
RUN CGO_ENABLED=0 GOOS=linux go build \
    -trimpath \
    -ldflags="-s -w -X main.version=${VERSION} -X main.commit=${COMMIT}" \
    -o server ./cmd/server

# Runtime stage
FROM gcr.io/distroless/static-debian12

# Copy binary
COPY --from=builder /app/server /server

# Copy any config files needed
COPY --from=builder /app/config.yaml /config.yaml

# Non-root user (distroless provides nonroot user)
USER nonroot:nonroot

EXPOSE 8080

ENTRYPOINT ["/server"]

Build It

docker build \
  --build-arg VERSION=$(git describe --tags) \
  --build-arg COMMIT=$(git rev-parse --short HEAD) \
  -t myapp:latest .

Handling HTTPS and Certificates

If your app makes HTTPS calls, you need CA certificates.

Option 1: distroless (includes them)

FROM gcr.io/distroless/static-debian12

Option 2: Copy from builder

FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

Option 3: alpine base

FROM alpine:3.19
RUN apk add --no-cache ca-certificates

Timezone Data

Go needs timezone data for time.LoadLocation():

Option 1: distroless (includes it)

Option 2: Embed in binary (Go 1.15+)

import _ "time/tzdata"

Adds ~500KB to binary but works everywhere.

Option 3: Copy from builder

COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo

Layer Caching Strategy

Order your Dockerfile for maximum cache hits:

# 1. Base image (rarely changes)
FROM golang:1.22 AS builder

# 2. Dependencies (change occasionally)
COPY go.mod go.sum ./
RUN go mod download

# 3. Source code (changes frequently)
COPY . .

# 4. Build
RUN go build -o /app ./cmd/server

If only your source changes, steps 1-2 are cached.

Comparing to .NET Dockerfiles

.NET self-contained:

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY *.csproj ./
RUN dotnet restore
COPY . .
RUN dotnet publish -c Release -o /app --self-contained -r linux-x64

FROM mcr.microsoft.com/dotnet/runtime-deps:8.0
COPY --from=build /app .
ENTRYPOINT ["./MyApp"]

Result: 200MB+ image

.NET AOT:

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY . .
RUN dotnet publish -c Release -r linux-x64 -p:PublishAot=true -o /app

FROM mcr.microsoft.com/dotnet/runtime-deps:8.0
COPY --from=build /app .
ENTRYPOINT ["./MyApp"]

Result: 50-80MB image (better, but still needs runtime-deps base)

Go:

FROM golang:1.22 AS build
COPY . .
RUN CGO_ENABLED=0 go build -o /app

FROM scratch
COPY --from=build /app /app
ENTRYPOINT ["/app"]

Result: 10-15MB image

Docker Compose for Development

version: '3.8'

services:
  app:
    build: .
    ports:
      - "8080:8080"
    environment:
      - DATABASE_URL=postgres://postgres:secret@db:5432/myapp
    depends_on:
      - db

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: myapp
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

The Honest Take

Go + Docker is a perfect match.

What Go does better:

  • Static binaries → scratch/distroless
  • Small images by default
  • No runtime to include
  • Fast builds in containers

What .NET does… differently:

  • Runtime-deps base required
  • Larger images even with AOT
  • More complex Dockerfiles
  • But better Windows container support

The verdict: If container size and simplicity matter, Go wins decisively. A 10MB image that starts instantly is operationally simpler than a 200MB image, especially at scale.

This is one of Go's genuine strengths. Embrace it.


Next up: AWS Lambda and Go. Serverless without the cold start tax.