CGO in Docker

illustrations illustrations illustrations illustrations illustrations illustrations illustrations

CGO in Docker

Published on Mar 15, 2021 by Mark Smith

post-thumb

When I was working on dockerising a Go server this morning I came across this lovely error when starting the container:

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

I hadn’t came across this one before and turns out it’s related to CGO.

The server I’m working on requires CGO as I’m using the https://github.com/mattn/go-sqlite3 sqlite library which has a dependency on it.

After some googling of the error it seemed people were seeing a very similar issue with carriage returns within the Dockerfile, this threw me for a while as I was then checking the file, re-creating the file (I’m using a mac) and then of course being presented with the same fun error.

I’m using a 2 stage build, the build stage uses a golang image which builds the CGOENABLED binary and then the production image used scratch. This was the source of the error, scratch doesn’t have support for the CGO enabled binary so I changed it to use a different image which does. The image I chose was bitnami_/_minideb:buster, which though not as small as a scratch image, gives a pretty small production image as well.

Hopefully this helps someone else who’s loosing time to this error as it’s not immediately obvious what the source of the issue is when building a cgoenabled go image.