"no such file or directory" after enabling CGO in Docker
Tim Bachmann

Tim Bachmann @tiim

About: I'm a swimmer, swim coach and CS master student in Switzerland

Location:
Switzerland
Joined:
Jan 17, 2019

"no such file or directory" after enabling CGO in Docker

Publish Date: Feb 20 '23
0 0

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.

Comments 0 total

    Add comment