By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A Go binary is self-contained: no runtime installed on the host, no class path, no node_modules. Cross-compilation is a pair of environment variables, GOOS and GOARCH, so a laptop can produce the Linux artifact the server runs. CGO_ENABLED=0 produces a static binary that runs in a scratch or distroless image, which is the smallest and safest runtime you can ship.
A release is more than a binary. Use -trimpath so paths inside the binary are reproducible, -s -w to strip symbols for size, and -X to inject the version and commit at link time. Keep the build command in a script or a Makefile so CI and developers produce byte-identical flags.
# Static, reproducible server binaries for the two Linux targets.
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -trimpath -ldflags="-s -w" -o dist/relayd-linux-amd64 ./cmd/relayd
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 \
go build -trimpath -ldflags="-s -w" -o dist/relayd-linux-arm64 ./cmd/relayd
# Inject release metadata at link time (version and commit come from git).
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath \
-ldflags "-s -w -X main.version=$(git describe --tags --always) -X main.commit=$(git rev-parse --short HEAD)" \
-o dist/relayd ./cmd/relayd
# Sanity-check the artifact target and its recorded module graph.
file dist/relayd-linux-amd64
go version -m dist/relayd-linux-amd64The Dockerfile keeps the compiler out of the runtime image. Stage one uses a pinned golang image to download modules and build the static binary. Stage two copies only that binary into a minimal base, runs as a non-root user and sets the entry point. The result is tens of megabytes instead of hundreds, with a much smaller attack surface.
cat > Dockerfile <<'EOF'
FROM golang:1.23 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath \
-ldflags="-s -w" -o /out/relayd ./cmd/relayd
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/relayd /relayd
USER nonroot:nonroot
EXPOSE 8080
ENTRYPOINT ["/relayd"]
EOF
# Build and inspect the runtime identity (user should not be root).
docker build -t relay:dev .
docker image inspect relay:dev --format '{{.Config.User}}'| Flag or variable | Effect | Why it matters |
|---|---|---|
CGO_ENABLED=0 | Static binary | Runs on scratch/distroless images |
GOOS / GOARCH | Target platform | Build Linux artifacts from any laptop |
-trimpath | Removes local paths | Reproducible builds and cleaner binaries |
-ldflags "-s -w" | Strips symbols | Smaller image; keep debug symbols for local profiling |
-X main.version=... | Injects a string | The running binary can report its release |
| Pinned base images | Reproducible environments | Supply-chain and drift control |
Key takeaways
Go cross-compiles with GOOS and GOARCH; CGO_ENABLED=0 yields a static binary.
-trimpath, stripped symbols and injected version metadata make releases reproducible and diagnosable.
Use a two-stage Docker build and copy only the binary into a minimal runtime image.
Run containers as a non-root user and pin base image versions.
Keep build flags in one script or Makefile so CI and local builds agree.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.