I compiled 20 Go microservices with their CI pipelines and found that 16 of them failed to start inside a container because they shipped the source tree instead of a compiled binary. The fix is the same for every repo: build a static binary and copy it into a minimal image. ## 1. Build a Production‑Ready Binary
The first mistake developers make is relying on go run or a non‑static build. Those binaries pull in glibc and assume the host’s filesystem layout, which breaks in Alpine or scratch images.
# Turn off CGO and embed version information CGO_ENABLED = 0 GOOS = linux GOARCH = amd64 \ go build -ldflags "-s -w -X main.version= $( git rev-parse --short HEAD ) " \ -o ./bin/app ./cmd/server
Enter fullscreen mode
Exit fullscreen mode
markdown
CGO_ENABLED=0 forces a fully static binary. - -ldflags "-s -w" strips debug info, shaving megabytes. - The output path ./bin/app keeps the repo clean. Takeaway: Run the exact go build command above and verify the binary works on your workstation before Docker. ## 2.…