In a normal Go build like go build main.go the resulting binary is dynamically linked againt glibc:

$ ldd main
linux-vdso.so.1 (0x00007ffd2a980000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f29e486d000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f29e46a2000)
/lib64/ld-linux-x86-64.so.2 (0x000055f8d296f000)

This is a Problem when the executable is supposed to run in a Docker Image that does not contain these libraries like alpine or even FROM scratch.

Running the container results in an error message:

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

Because the libraries are not found in the containers file system.

The Solution is to build a staticly linked binary:

go build -ldflags '-d -s -w' -a -tags netgo -installsuffix netgo main.go

This solves the problem and results in docker image size of 4 MB compared to more than 100 MB using a complete linux distribution.