ERROR: dockerfile parse error invalid field ' ' - go

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

Related

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

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

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

How to deploy GoLang Binaries to CF

Hi we are trying to deploy golang binaries to cf.
for example main.exe which is in /deploy/ folder along with mainfest file
Note: It is also observed that if we push the whole project app then it works. but if we try to push only binaries then we get the below error
And Manifest file contains following info
applications:
- name: test-app
command: test-app
env:
GO_INSTALL_PACKAGE_SPEC: ./
**ERROR** To use go native vendoring set the $GOPACKAGENAME
environment variable to your app's package name
**ERROR** Unable to determine import path: GOPACKAGENAME unset
After adding GOPACKAGENAME: main since main.exe is our binary name we get following error below
Failed to compile droplet: Failed to run finalize script: exit status 12
Cell 507b6e9c-c5c5-4685-9a71-d7cc1c876f5a stopping instance 6a92ff73-76ec-4baf-8a3e-54b54cfa307e
BuildpackCompileFailed - App staging failed in the buildpack compile phase
thanks #Volker for pointing out
for above issue:
first we have to build with following command (used make file)
GOOS="linux" go build main.go
then copy that main binary to /deploy folder
then manifest file in the deploy folder
applications:
- name: test-app
command: ./main
stack: cflinuxfs3
buildpacks:
- https://github.com/cloudfoundry/binary-buildpack.git
Then to push to cf
cf push -f ./manifest-template.yml

Gitlab CI and Go modules

I have a Go project and I want write a Gitlab CI file my project but I see error loading module requirements. This is my gitlab CI file:
stages:
- compile
build:
stage: compile
image: golang:1.12
script:
- go build -o binary
error :
Running with gitlab-runner 11.8.0 (4745a6f3)
on Runner #6 e0e0c446
Using Docker executor with image golang:1.12 ...
Pulling docker image golang:1.12 ...
Using docker image sha256:be63d15101cb68aea1841b45201b08a1a486c1a60a029bbf0ddb284ab646b0b7 for golang:1.12 ...
Running on runner-e0e0c446-project-753-concurrent-0 via runner-01.tool.afra.snapp.infra...
Cloning repository...
Cloning into '/builds/Alsopa/echo-training'...
Checking out ab3994cc as develop...
Skipping Git submodules setup
$ go build -o binary
go: finding github.com/labstack/gommon v0.2.9
go: finding github.com/labstack/echo v3.3.10+incompatible
go: finding github.com/go-sql-driver/mysql v1.4.1
go: google.golang.org/appengine#v1.6.1: unrecognized import path "google.golang.org/appengine" (parse https://google.golang.org/appengine?go-get=1: no go-import meta tags ())
go: error loading module requirements
ERROR: Job failed: exit code 1
Be sure you upload the last go.sum file. Just before uploading to master for the ci execution run go get, upload to master and run the ci.
The primary error seems to be:
go: google.golang.org/appengine#v1.6.1: unrecognized import path
"google.golang.org/appengine" (parse
https://google.golang.org/appengine?go-get=1: no go-import meta tags ())
I would try running go get -v google.golang.org/appengine and curl -v https://google.golang.org/appengine?go-get=1 from within your CI environment to see what they show.
This is what I get for go get -v google.golang.org/appengine:
$ go get -v google.golang.org/appengine
Fetching https://google.golang.org/appengine?go-get=1
Parsing meta tags from https://google.golang.org/appengine?go-get=1 (status code 200)
get "google.golang.org/appengine": found meta tag
get.metaImport{Prefix:"google.golang.org/appengine",
VCS:"git", RepoRoot:"https://github.com/golang/appengine"}
at https://google.golang.org/appengine?go-get=1
Perhaps you have some network connectivity, firewall, or HTTP proxy issue.
You should download modules before compilation. Assuming your project already has go modules files go.mod and go.sum try to update .gitlab-ci.yml this way
build:
stage: compile
image: golang:1.13
script:
- go get -d -v
- go build -o binary

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

Resources