The command '/bin/sh -c go build -o image_srv .' returned a non-zero code: 2 - go

I am trying to build a docker file using docker-compose. Part of the build is related to go language. When I build the code I get the following error. What could be the issue? How do I fix this error ? Could you please advise? I am new to go language
---> Running in 68f94f330d0b
Removing intermediate container 68f94f330d0b
---> 7a9ca17f7bd7
Step 15/16 : RUN go build -o image_srv .
---> Running in 38fa96ef75d6
# golang.org/x/sys/unix
/go/src/golang.org/x/sys/unix/syscall.go:83:16: undefined: unsafe.Slice
/go/src/golang.org/x/sys/unix/syscall_unix.go:118:7: undefined: unsafe.Slice
/go/src/golang.org/x/sys/unix/sysvshm_unix.go:33:7: undefined: unsafe.Slice
The command '/bin/sh -c go build -o image_srv .' returned a non-zero code: 2

You can find appropriate 1.17 tags by going to https://hub.docker.com/_/golang/tags?page=1&name=1.17
Probably golang:1.17.13-alpine will suite your use case
Take note that you may need to delete any existing docker images so that the image can rebuild
# list docker images
$ docker image ls
# delete image foobar
$ docker rmi foobar

Related

go application build with bazel can't link when running inside container

I am trying to containerize my application build, though, when running the build that uses bazel with bazel-gazelle inside a container I will get this error:
$ bazel run --spawn_strategy=local //:gazelle --verbose_failures
INFO: Analyzed target //:gazelle (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /home/workstation/.cache/bazel/_bazel_workstation/fb227af4c7b6aa39cc5b15d7fd9b737a/external/go_sdk/BUILD.bazel:43:15: GoToolchainBinary external/go_sdk/builder [for host] failed: (Exit 1): go failed: error executing command
(cd /home/workstation/.cache/bazel/_bazel_workstation/fb227af4c7b6aa39cc5b15d7fd9b737a/execroot/__main__ && \
exec env - \
GOROOT_FINAL=GOROOT \
external/go_sdk/bin/go tool link -o bazel-out/host/bin/external/go_sdk/builder bazel-out/host/bin/external/go_sdk/builder.a)
# Configuration: e0f1106e28100863b4221c55fca6feb935acec078da5376e291cf644e275dae5
# Execution platform: #local_config_platform//:host
/opt/go/pkg/tool/linux_amd64/link: mapping output file failed: invalid argument
Target //:gazelle failed to build
INFO: Elapsed time: 2.302s, Critical Path: 0.35s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully
FAILED: Build did NOT complete successfully
I tried to run it standalone:
$ /home/workstation/.cache/bazel/_bazel_workstation/fb227af4c7b6aa39cc5b15d7fd9b737a/external/go_sdk/bin/go tool link -o bazel-out/host/bin/external/go_sdk/builder bazel-out/host/bin/external/go_sdk/builder.a
/opt/go/pkg/tool/linux_amd64/link: mapping output file failed: invalid argument
and still got no success.
Never had this kind of link problem and the linker don't provide much more information. Tried to install all packages I could think of and still no luck.
For context:
Running Ubuntu 20.04 LTS
Docker 20.10.9
Bazel 4.2.2
Rules GO v0.31.0
Bazel Gazelle v0.25.0
Also tried to run it with the strace, though I don't think I am skilled enough to find meaningful information from the tool output.
#edit
For more context:
e$ /home/workstation/.cache/bazel/_bazel_workstation/fb227af4c7b6aa39cc5b15d7fd9b737a/external/go_sdk/bin/go tool link -v -o bazel-out/host/bin/external/go_sdk/builder bazel-out/host/bin/external/go_sdk/builder.a
HEADER = -H5 -T0x401000 -R0x1000
searching for runtime.a in /opt/go/pkg/linux_amd64/runtime.a
/opt/go/pkg/tool/linux_amd64/link: mapping output file failed: invalid argument

ERROR: dockerfile parse error invalid field ' '

I am trying to containerize Go Developer environment.
Blogs I am referring: https://www.docker.com/blog/containerize-your-go-developer-environment-part-1/
I am getting an error:
failed to solve with frontend dockerfile.v0: failed to solve with frontend gateway.v0: rpc error: code = Unknown desc = invalid field '' must be a key=value pair
Makefile:8: recipe for target 'bin/example' failed
make: *** [bin/example] Error 1
My Dockerfile:
# syntax = docker/dockerfile:1-experimental
FROM --platform=${BUILDPLATFORM} golang:1.17.8-alpine AS base
WORKDIR /src
ENV CGO_ENABLED=0
COPY go.* .
RUN --mount=type=cache, target=/go/pkg/mod go mod download
FROM base AS build
ARG TARGETOS
ARG TARGETARCH
RUN --mount=target=. --mount=type=cache, target=/go/pkg/mod --mount=type=cache, target=/root/.cache/go-build GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o /out/example .
FROM base AS unit-test
RUN --mount=target=. --mount=type=cache, target=/go/pkg/mod --mount=type=cache, target=/root/.cache/go-build go test -v .
FROM golangci/golangci-lint:v1.43-alpine AS lint-base
FROM base AS lint
RUN --mount=target=. --mount=from=lint-base, src=/usr/bin/golangci-lint, target=/usr/bin/golangci-lint --mount=type=cache, target=/go/pkg/mod --mount=type=cache, target=/root/.cache/go-build --mount=type=cache, target=/root/.cache/golangci-lint golangci-lint run --timeout 10m0s ./...
FROM scratch AS bin-unix
COPY --from=build /out/example /
FROM bin-unix AS bin-linux
FROM bin-unix AS bin-darwin
FROM scratch AS bin-windows
COPY --from=build /out/example /example.exe
FROM bin-${TARGETOS} AS bin
Makefile is as follows:
all: bin/example
test: lint unit-test
PLATFORM=linux/amd64
.PHONY: bin/example
bin/example:
#docker build . --target bin --output bin/ --platform ${PLATFORM}
.PHONY: unit-test
unit-test:
#docker build . --target unit-test
.PHONY: lint
lint:
#docker build . --target lint
In terminal:
atin#atin-VirtualBox:~/Desktop/container-go-dev$ export DOCKER_BUILDKIT=1
atin#atin-VirtualBox:~/Desktop/container-go-dev$ make
[+] Building 23.2s (5/5) FINISHED
=> [internal] load build definition from Dockerfile 1.6s
=> => transferring dockerfile: 38B 0.4s
=> [internal] load .dockerignore 1.3s
=> => transferring context: 34B 0.3s
=> resolve image config for docker.io/docker/dockerfile:1-experimental 10.3s
=> [auth] docker/dockerfile:pull token for registry-1.docker.io 0.0s
=> CACHED docker-image://docker.io/docker/dockerfile:1-experimental#sha2 0.0s
failed to solve with frontend dockerfile.v0: failed to solve with frontend gateway.v0: rpc error: code = Unknown desc = failed to create LLB definition: dockerfile parse error line 7: invalid field '' must be a key=value pair
Makefile:8: recipe for target 'bin/example' failed
make: *** [bin/example] Error 1
This is line #7 of your docker file:
RUN --mount=type=cache, target=/go/pkg/mod go mod download
I'm certainly not an expert at dockerfiles but that syntax does not look right to me. I have no idea what program in your image the RUN command is invoking, but I question whether there should be a comma here, and should there be an equals sign between "mount" and "type", or should it be a dash? And, does it even work to pass an option here with "--"?
You have a lot of invalid whitespace throughout your file, including the syntax line up top (likely resulting in it being ignored) and in the --mount options:
RUN --mount=type=cache, target=/go/pkg/mod go mod download
Should be
RUN --mount=type=cache,target=/go/pkg/mod go mod download

monitoring esxi using prometheus : getting error undefined: debug.ReadBuildInfo

i am trying monitor esxi via prometheus to grafana using https://github.com/devinotelecom/prometheus-vmware-exporter
when i am running
[root#admin01 prometheus-vmware-exporter]# docker build -t prometheus-vmware-exporter .
Sending build context to Docker daemon 157.7kB
Step 1/9 : FROM golang:1.11 as builder
---> 43a154fee764
Step 2/9 : WORKDIR /src/github.com/devinotelecom/prometheus-vmware-exporter
---> Using cache
---> 6b2aad4c7a43
Step 3/9 : COPY ./ /src/github.com/devinotelecom/prometheus-vmware-exporter
---> Using cache
---> 2f5f1b155f7f
Step 4/9 : RUN go get -d -v
---> Using cache
---> a48d35b3d5e2
Step 5/9 : RUN CGO_ENABLED=0 GOOS=linux go build
---> Running in 86199cee4fcb
# github.com/prometheus/client_golang/prometheus
/go/src/github.com/prometheus/client_golang/prometheus/build_info_collector.go:24:15: undefined: debug.ReadBuildInfo
The command '/bin/sh -c CGO_ENABLED=0 GOOS=linux go build' returned a non-zero code: 2
[root#admin01 prometheus-vmware-exporter]#
Thanks for your help in Advanced
The minimal golang version in prometheus is 1.15, but in the Dockerfile it says 1.11
source : https://github.com/prometheus/client_golang/blob/main/go.mod
Try changing golang:1.11 to golang:1.15++ in Dockerfile

Static Go Binaries w/ Docker - Entrypoint Not Found

I used Static Go Binaries with Docker on OSX by Nicola Paolucci to try to use static Go binary w/ Docker. I believe I followed every step correctly, but when I run the final image, I get the following error response from Docker.
NOTE The name of my service and executable are netverify
docker: Error response from daemon: Container command '/netverify' not found or does not exist..
My Dockerfile.static looks like the following...
#Create a minimal container to run a Golang static binary
FROM tianon/true
ADD netverify /
EXPOSE 8282
CMD ["/netverify"]
My Dockerfile.build looks like the following...
FROM golang
ADD Makefile /
WORKDIR /
RUN make setup
ADD . /go/src/github.com/eirwin/netverify
RUN make buildgo
CMD ["/bin/bash"]
My Makefile is the following...
GOCMD = go
GOBUILD = $(GOCMD) build
GOGET = $(GOCMD) get -v
GOCLEAN = $(GOCMD) clean
GOINSTALL = $(GOCMD) install
GOTEST = $(GOCMD) test
.PHONY: all
all: build
setup:
$(GOGET) github.com/gorilla/mux
buildgo:
GOOS=linux $(GOBUILD) -o netverify ./go/src/github.com/eirwin/netverify
builddocker:
docker build -t eirwin/netverify -f ./Dockerfile.build .
docker run -t eirwin/netverify /bin/true
docker cp `docker ps -q -n=1`:/netverify .
chmod 755 ./netverify
docker build --rm=true --tag=eirwin/netverify -f Dockerfile.static .
run: builddocker
docker run -p 8282:8282 eirwin/netverify
For the purpose of this post, lets assume I have the following as my golang application.
func main() {
router := mux.NewRouter()
router.HandleFunc("/ping", api.PingHandler).Methods("GET")
http.ListenAndServe(":8282", router)
}
When I run Make run everything seems to work except for when the image is ran.
I can see that the image builds correctly at ~8.5MB
eirwin/netverify latest eae16e146b91 3 seconds ago 8.63 MB
But when If docker run -p 8282:8282 eirwin/netverify is ran I get the following error...
docker: Error response from daemon: Container command '/netverify' not found or does not exist..
The go static build in your makefile is missing some options.
buildgo:
CGO_ENABLED=0 GOOS=linux go build -ldflags "-s" -a -installsuffix cgo -o netverify ./go/src/github.com/eirwin/netverify
The build process can capture the container ID to avoid timing issues.
Separate the tags for build and binary images.
builddocker:
docker build -t eirwin/netverify-build -f ./Dockerfile.build .
CID=$$(docker create eirwin/netverify-build); \
docker cp $$CID:/netverify .; \
docker rm $$CID
chmod 755 ./netverify
docker build --rm=true --tag=eirwin/netverify -f Dockerfile.static .
Your binary Dockerfile.static can start with the scratch blank image.
FROM scratch

Golang failes to change the name of imported module

I'm trying to bring SkyDNSv1 back to life and build it from my fork (here is Dockerfile). SkyDNS was really good and simple tool for the quick service discovery, but it wasn't updated for a long time.
There is an error in build process and it's caused by third party library. I cannot figure out why does it happen:
$ docker build --no-cache -t skydns1 .
Sending build context to Docker daemon 1.566 MB
Sending build context to Docker daemon
Step 0 : FROM golang:1.4.2
---> 3e8cb8e0c765
Step 1 : WORKDIR /go/src
---> Running in 3a06cf460ad9
---> 1dd14a099164
Removing intermediate container 3a06cf460ad9
Step 2 : RUN go get github.com/codegangsta/cli
---> Running in eabcfd6fe621
---> c9ea222f2d74
Removing intermediate container eabcfd6fe621
Step 3 : RUN go get github.com/vitalyisaev2/skydns1
---> Running in 3264582b2e7a
# github.com/rcrowley/go-metrics/influxdb
github.com/rcrowley/go-metrics/influxdb/influxdb.go:19: undefined: client.ClientConfig
github.com/rcrowley/go-metrics/influxdb/influxdb.go:38: undefined: client.Series
github.com/rcrowley/go-metrics/influxdb/influxdb.go:44: undefined: client.Series
github.com/rcrowley/go-metrics/influxdb/influxdb.go:52: undefined: client.Series
github.com/rcrowley/go-metrics/influxdb/influxdb.go:60: undefined: client.Series
github.com/rcrowley/go-metrics/influxdb/influxdb.go:70: undefined: client.Series
github.com/rcrowley/go-metrics/influxdb/influxdb.go:82: undefined: client.Series
github.com/rcrowley/go-metrics/influxdb/influxdb.go:93: undefined: client.Series
github.com/rcrowley/go-metrics/influxdb/influxdb.go:106: client.WriteSeries undefined (type *client.Client has no field or method WriteSeries)
INFO[0075] The command [/bin/sh -c go get github.com/vitalyisaev2/skydns1] returned a non-zero code:
But if you look through the file causing this error, you'll notice that Golang is confused about influxdb/client. I think that compiler doesn't replace imported name client with influxClient as it should do:
package influxdb
import (
"fmt"
influxClient "github.com/influxdb/influxdb/client"
"github.com/rcrowley/go-metrics"
"log"
"time"
)
Probably I just missing an obvious mistake. Any help will be appreciated.
The Go compiler doesn't replace or rewrite anything, the code is just wrong. The github.com/rcrowley/go-metrics/influxdb package was written with some other influxdb client code that no longer exists. (Looks like there are a couple github issues open about this already)
If you look at the current influxdb/client package, you'll see there's no Series, ClientConfig, or Client.WriteSeries at all. You'll need to drop the dependency on github.com/rcrowley/go-metrics/influxdb in order to get your project to build.

Resources