Optimizing Docker Containers for Production
January 15, 2026Container bloat is a real issue. When I first started writing Dockerfiles, I routinely ended up with 1GB+ images for simple microservices. By utilizing multi-stage builds and minimal base images, I managed to reduce my deployment payloads by over 70%.
Here is the standard multi-stage Dockerfile template I use for all my Go projects:
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY . .
RUN go build -o main .
FROM alpine:latest
WORKDIR /root/
COPY --from=builder /app/main .
CMD ["./main"]
This approach compiles the binary in a heavy environment, but only copies the compiled binary into the final, lightweight Alpine container. It drastically reduces the attack surface and deployment times.