Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 327
abergmeier Avatar asked May 01 '19 06:05

abergmeier


1 Answers

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"

like image 118
Auyer Avatar answered Nov 01 '22 19:11

Auyer