Sub topic 4
Building a static binary
This chapter assumes you can write a basic Dockerfile and build a Go program. It also assumes you know that a Linux program may load shared libraries (.so files) at startup. The previous chapter covered static and dynamic linking. Here you produce a static binary and confirm that it is one.
The example program
Every Dockerfile in this tutorial builds the same small Go HTTP server. It answers /healthz with status 200.
package main
import (
"log"
"net/http"
)
func main() {
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
log.Fatal(http.ListenAndServe(":8080", nil))
}You also need a go.mod file. Create one with go mod init example.com/app.
Why Go needs a switch
Go compiles most code into the binary itself. Some standard library packages, such as net and os/user, can instead call the system C library through cgo. cgo is Go's bridge to C code. When cgo is on and a package uses it, the linker produces a dynamically linked binary that needs glibc at runtime.
The CGO_ENABLED environment variable controls this. Setting it to 0 makes Go use its pure Go implementations. The result has no shared library dependencies.
Build and inspect
Build once with cgo off and once with cgo on. Then inspect both.
CGO_ENABLED=0 go build -o app .
file app
# expected to report a statically linked executable
CGO_ENABLED=1 go build -o app-cgo . && ldd app-cgoThe file command reports the executable type. For app, the output should say "statically linked". The ldd command lists the shared libraries a binary loads. For app-cgo, you should see entries such as libc.so.6. This only happens if a package in the build actually uses cgo and a C compiler is installed. Otherwise Go may still produce a static binary, and ldd prints "not a dynamic executable".
Running ldd on a static binary prints "not a dynamic executable". That message is the result you want.
Useful build flags
The Dockerfiles later in this tutorial use these flags:
| Flag | Effect |
|---|---|
| `-trimpath` | Removes local file system paths from the binary. Builds become more reproducible. |
| `-ldflags="-s -w"` | Strips the symbol table and debug information. The binary gets smaller. |
Stripping makes stack traces from the Go runtime still readable, but it removes data that debuggers such as dlv and gdb use.
Common errors
- A binary built on one machine fails in a container with "exec format error". The architecture differs, for example an arm64 laptop and an amd64 server. Set
GOOSandGOARCH, or usedocker buildxwith--platform. filereports "dynamically linked". A dependency uses cgo. Check withgo list -f '{{.CgoFiles}}' ./..., or setCGO_ENABLED=0and see whether the build still succeeds.- The build fails with
CGO_ENABLED=0. Some libraries need C code, such as certain SQLite drivers. You then need a dynamic binary and a runtime image with glibc. A later chapter covers that case.
Rust gets a static binary by targeting musl, for example x86_64-unknown-linux-musl. C and C++ use -static with a suitable libc. The language notes chapter covers both.
Exercise
You run file app on a Go binary and it reports "dynamically linked". Name the setting you would change to get a static binary, and the command that would show which libraries the binary loads.
Show answer
Set CGO_ENABLED=0 when running go build, so Go uses its pure Go implementations instead of calling C. Run ldd app to list the shared libraries the binary loads. A static binary prints "not a dynamic executable".