问题
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?
回答1:
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"
回答2:
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.
来源:https://stackoverflow.com/questions/55932233/cannot-find-runtime-cgo-in-alpine