building go binaries for small docker images

building docker images with go should be super easy - picking a minimal image (in my case I build from alpine), build the binary, ADD to the image, run…

the problem

your app is not starting, instead you see something like this

standard_init_linux.go:195: exec user process caused "no such file or directory"

cause: several modules are calling C code via cgo, and cgo depends on libc. Go found libc on the system it was build on, so it linked to it. But libc is missing on alpine…

the fix

build a really static linked binary!

CGO_ENABLED=0 go build -a -o main .
  • CGO_ENABLED=0 - disable cgo, this is the important part!
  • -a - rebuild all, including imported depdenencies

now, your app should start without a problem, even on a scratch image :)


for Disqus comments please visit the permalink...