static binary
A program file that carries all the code it needs inside it and loads no shared libraries from the machine it runs on.
In more detail
Because it needs nothing from the machine it runs on except the kernel, a static binary can be copied onto any compatible system, including an empty container image. The trade-off is a larger file, and security fixes in bundled libraries require a rebuild. The file command reports statically linked for such programs.
Examples
CGO_ENABLED=0 go build -o app .
file app # ... statically linkedGo: CGO_ENABLED=0 go build -o app . produces a static binaryRelated terms
- cgo — The Go feature that lets Go code call C code, which links the program to C libraries on the build and target machines.
- scratch — Docker's special empty base image, with no files at all, used to ship a single static binary and nothing else.
- shared library — Ready-made code that many programs load at run time instead of each carrying its own copy.
- C library — The basic library that almost every program uses to talk to the operating system kernel, such as glibc on Debian Linux or musl on Alpine Linux.
- file (command) — A Unix command that inspects a file's contents and reports what kind of file it is, such as a statically linked executable for a given processor.
Taught in
- What Go Is and Why It Matters — Concurrent Systems Programming with Go
- Building Static Binaries — Concurrent Systems Programming with Go
- Shipping in Scratch and Distroless Images — Concurrent Systems Programming with Go
- Containerizing and Testing the Final Build — Concurrent Systems Programming with Go