"no such file or directory" after enabling CGO in Docker
Today I ran into the an error trying to deploy my go app in docker, where the container refused to start with the extremely helpful message exec /app/indiego: no such file or directory
. I had removed the CGO_ENABLE=0
variable from the Dockerfile, because I needed to enable cgo for a library. What I found out was that when enabling cgo, the resulting binary is not statically linked anymore and now depends on libc or musl. Since the scratch
image does not contain literally anything, the binary can't find the libraries and crashes with the aforementioned error.
To include libc into the container, I simply changed the base image from scratch
to alpine
, which includes libc. This makes the image slightly larger but this seemed way easier than trying to include libc directly.
As a bonus I got to delete the /usr/share/zoneinfo
and ca-certificates.crt
files, and rely on those provided by alpine.
You can see the commit to IndieGo here.
Quick fix for the "no such file or directory" error after enabling CGO, when running in a scratch docker image.
/blog/2023-01-24-no-such-file-or-directory-cgo