Create optimized multi-stage Docker build for Go apps
✓Works with OpenClaudeYou are a Go developer and DevOps engineer. The user wants to create an optimized multi-stage Docker build for Go applications that minimizes image size and build time.
What to check first
- Run
go versionto confirm Go is installed locally - Check if Docker is installed with
docker --version - Verify your Go project has a
go.modfile in the root directory - Ensure your main Go file exists (typically
main.go)
Steps
- Create a
Dockerfilein your project root with a builder stage usinggolang:[version]-alpineas the base image - In the builder stage, set
WORKDIR /appand copygo.modandgo.sumfiles first to leverage Docker layer caching - Run
go mod downloadin the builder stage to fetch dependencies before copying source code - Copy all source code and run
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .to create a static binary - Create a second stage using
alpine:[version]as the final base image (much smaller than full OS images) - Copy only the compiled binary from the builder stage into the final image using
COPY --from=builder - Set the entrypoint with
ENTRYPOINT ["/app/main"]to run the binary directly - Build the image with
docker build -t myapp:latest .and test withdocker run myapp:latest
Code
# Stage 1: Builder
FROM golang:1.21-alpine AS builder
# Install build dependencies
RUN apk add --no-cache git ca-certificates tzdata
WORKDIR /app
# Copy go mod files and download dependencies
COPY go.mod go.sum ./
RUN go mod download
# Copy source code
COPY . .
# Build static binary
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
# Stage 2: Final runtime image
FROM alpine:3.18
# Install runtime dependencies (ca-certificates for HTTPS, tzdata for timezone)
RUN apk add --no-cache ca-certificates tzdata
WORKDIR /app
# Copy binary from builder
COPY --from=builder /app/main .
# Copy any static assets if needed
# COPY --from=builder /app/assets ./assets
# Non-root user for security (optional but recommended)
RUN addgroup -g 1000 appuser && adduser -D -u 1000 -G appuser appuser
USER appuser
EXPOSE 8080
ENTRYPOINT ["./main"]
Pitfalls
- CGO_ENABLED=0 is critical — if you forget this flag, your binary will link against libc and fail in the slim Alpine runtime image with "not found" errors
- Alpine base image size trap — while Alpine is small (~5MB), it lacks glibc; use `distro
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Go Skills
Other Claude Code skills in the same category — free to download.
Go API Setup
Scaffold Go REST API with Gin or Chi router
Go Testing
Set up Go testing with table-driven tests and mocks
Go Modules
Manage Go modules and dependency versioning
Go Concurrency
Implement goroutines, channels, and concurrency patterns
Go gRPC
Set up gRPC server and client in Go
Go Middleware
Create HTTP middleware chain in Go
Go CLI
Build CLI applications with Cobra in Go
Want a Go skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.