abergmeier
abergmeier

Reputation: 14071

cannot find runtime/cgo in Alpine

In an alpine:edge container I installed go via

RUN apk add --no-cache musl-dev go

I try to run go get github.com/golang/protobuf/protoc-gen-go then.

This results in the error message:

go: finding github.com/golang/protobuf/protoc-gen-go latest
go: finding github.com/golang/protobuf v1.3.1
go: downloading github.com/golang/protobuf v1.3.1
go: extracting github.com/golang/protobuf v1.3.1
# github.com/golang/protobuf/protoc-gen-go
loadinternal: cannot find runtime/cgo
protoc-gen-go: program not found or is not executable

Now looking at the code, it fails on ctxt.PackageFile[name].

I double checked though, that both /usr/lib/go/pkg/tool/linux_amd64/cgo and /usr/lib/go/pkg/linux_amd64/runtime/cgo.a are present.

Which should all be in order according to go env:

GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/lib/go"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build521273293=/tmp/go-build -gno-record-gcc-switches"

Anyone any hints where to look next or what is wrong?

Upvotes: 9

Views: 13347

Answers (3)

James Mills
James Mills

Reputation: 19050

Rather than work-around(s) the correct solution is to install what the go compiler is actually looking for.

The error:

cannot find runtime/cgo

basically at a high level means it cannot find the necessary libraries and headers on your system to link against any C code or C libraires.

Just install what it is looking for libc-dev in this case with:

$ apk --no-cache -U add libc-dev

Or if preferred just install the meta package build-base:

$ apk --no-cache -U add build-base

Upvotes: 1

Auyer
Auyer

Reputation: 2971

One way to avoid this issue is to force a static build disabling cgo. Try building your binary with the following command:

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o yourBinaryName

If you actually need cgo in your app, you might want to install gcc, musl-dev and even build-base like @abergmeier pointed out. You can do this with this command:

RUN apk update && apk add --no-cache musl-dev gcc build-base

If none of that works I would check out this gist claiming it creates a Dockerfile capable of compiling protobuffs: "Alpine (3.6) based docker image with Protocol Buffers compiler supporting Go"

Upvotes: 4

abergmeier
abergmeier

Reputation: 14071

For now as a workaround, I copy necessary Go binaries from golang:1.12-alpine into my current container.

With that, go get works as intended.

Upvotes: 0

Related Questions