Golang Dockerfile Failing - go

I have a Golang web application that I am looking to run in Docker container. I am able to run it fine outside of the container, so I know it works, but when I build it from Dockerfile and run it, it gives me an error.
The Makefile looks like the following
GOCMD = go
GOBUILD = $(GOCMD) build
GOGET = $(GOCMD) get -v
GOCLEAN = $(GOCMD) clean
GOINSTALL = $(GOCMD) install
GOTEST = $(GOCMD) test
.PHONY: all
all: build
test:
$(GOTEST) -v -cover ./...
build:
$(GOGET); $(GOBUILD) -v -o engine
clean:
$(GOCLEAN) -n -i -x
rm -f $(GOPATH)/bin/engine
rm -rf bin/engine
install:
$(GOINSTALL)
And the Dockerfile looks like the following
FROM golang
ADD engine /go/bin/engine
EXPOSE 7777
ENTRYPOINT /go/bin/engine
I am building the image and running it using the following
docker build -t engine .
docker run -d --name engine -p 7777:7777 engine
and its giving me the following error
/go/bin/engine: 1: /go/bin/engine: Syntax error: "(" unexpected

When you build a binary, go build assumes that you are trying to build for your current computer, it chooses values for GOOS and GOARCH (described here) for you.
If you are not building on a linux machine then you will need to cross compile the binary for linux, as this is what the OS inside the docker container will be running. Explanation here
You need something like:
GOOS=linux
build:
$(GOGET); GOOS=$(GOOS) $(GOBUILD) -v -o engine

Related

What's wrong about kuberent client demo error

download kubernetes sdk source code
git clone https://github.com/kubernetes/client-go.git kubernet-client
and then
cd kubernet-client/examples/in-cluster-client-configuration/
and
type go run main.go , has error
➜ in-cluster-client-configuration git:(master) go run main.go
# net
/usr/local/Cellar/go/1.18.2/libexec/src/net/dial.go:351:11: d.DialContext undefined (type *Dialer has no field or method DialContext)
/usr/local/Cellar/go/1.18.2/libexec/src/net/dial.go:372:18: invalid character U+2013 '–' in identifier
/usr/local/Cellar/go/1.18.2/libexec/src/net/lookup_unix.go:70:14: d.DialContext undefined (type Dialer has no field or method DialContext)
➜ in-cluster-client-configuration git:(master) pwd
go_sourcecode/kubernet-client/examples/in-cluster-client-configuration
the go version
➜ in-cluster-client-configuration git:(master) go version
go version go1.18.2 darwin/amd64
what's wrong?
The step-by-step process has been written in the markdown file (https://github.com/kubernetes/client-go/tree/master/examples/in-cluster-client-configuration), why you didn't follow that ?
As this is an in-cluster example, this has to be run from inside of a kubernetes cluster. go run main.go will not work. To run this,
make a binary : go build -o ./app .
build an image : docker build -t YOUR_DOCKER_ID/controller:in-cluster .
push it on docker hub: docker push IMAGE
OR load this image into cluster. For kind cluster : kind load docker-image IMAGE
give permission to list pods : kubectl create clusterrolebinding default-view --clusterrole=view --serviceaccount=default:default
Then run kubectl run --rm -i demo --image=IMAGE

How to make a linux executable file using gitlab (go env)?

I am trying to make a Linux executable file of my Go project. I have the following configuration in my .config-ci.yml in my gitlab project.
demo_job_1:
tags:
- cpf
- cpf-test
- testing
- unit-testing
script:
- go run test/main.go
- GOOS=linux GOARCH=amd64 go build
- go env
- cd test
- ./test
- echo Successfully run and Built
After running this pipeline, I still get the GOOS=windows when I check in env file. How can I build my project so that the output after building is of Linux executable file. Right now, I am getting .exe file which runs on Windows only.
You can see the project details in the following gitlab:
https://gitlab.com/smilekison/test
This is the error that is shown by Pipeline Job:
$ go run test/main.go
Hello world
$ GOOS=linux GOARCH=amd64 go build
GOOS=linux : The term 'GOOS=linux' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\WINDOWS\TEMP\build_script487326907\script.ps1:207 char:1
+ GOOS=linux GOARCH=amd64 go build
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (GOOS=linux:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
First to address your actual error: it seems you are on a windows based runner. That means you have to use windows CMD commands. It does not know ENV, etc.
You can do go env -w GOOS="linux" instead. Same with GOARCH. Then just run go build ..
You can also use a variables section to overwrite go env with environment variables:
variables:
GOOS: "linux"
GOARCH: "amd64"
It goes at the top of the gitlab file somewhere.
Here my typical build pipeline for Go projects using docker containers:
build_App:
image: golang:1.15.3
stage: build
allow_failure: false
tags:
- unix
script:
- go mod download
- mkdir $CI_PROJECT_DIR/release
- cd cmd/app
- GOOS=linux GOARCH=amd64 go build -o $CI_PROJECT_DIR/release/app .
artifacts:
paths:
- $CI_PROJECT_DIR/release
And test pipeline
go_test:
image: golang:1.15.3
stage: verify
allow_failure: false
tags:
- unix
script:
- go mod download
- go test -race -cover ./...
This is based on a runner that uses docker images to build in.
I needed to write go env -w GOOS="linux" GOARCH="amd64" to make the executable file for linux and if I want to make executable for windows I have to just rename linux to windows and I can make go language install by using image : golang:1.15.7 here. This way my .gitlab-ci.yml file can get GO Lang install and can run any go command.
demo_job_1:
stages:
-build
build:
stage: build
image : golang:1.15.7
tags:
- cpf
- cpf-test
- testing
- unit-testing
script:
- go run test/main.go
- go env -w GOOS=linux GOARCH=amd64
- go env
- cd test
- ./test
- echo Successfully run and Built

Error while running make command using Fabric 1.0.6 after all the 15 steps

I am installing fabric in a custom path $HOME//gopath/src/github.com/hyperledger/fabric using the make command but after installing all the 15 steps, I am getting the following error
Step 15/15 : LABEL org.hyperledger.fabric.version=1.0.2 org.hyperledger.fabric.base.version=0.3.2
---> Running in ed05a67810db
Removing intermediate container ed05a67810db
---> 99b4b0b28957
Successfully built 99b4b0b28957
Successfully tagged hyperledger/fabric-javaenv:latest
docker tag hyperledger/fabric-javaenv hyperledger/fabric-javaenv:x86_64-1.0.2
build/bin/peer
CGO_CFLAGS=" " GOBIN=/root/gopath/src/github.com/hyperledger/fabric/build/bin go install -tags "" -ldflags "-X github.com/hyperledger/fabric/common/metadata.Version=1.0.2 -X github.com/hyperledger/fabric/common/metadata.BaseVersion=0.3.2 -X github.com/hyperledger/fabric/common/metadata.BaseDockerLabel=org.hyperledger.fabric -X github.com/hyperledger/fabric/common/metadata.DockerNamespace=hyperledger -X github.com/hyperledger/fabric/common/metadata.BaseDockerNamespace=hyperledger" github.com/hyperledger/fabric/peer
go build github.com/hyperledger/fabric/vendor/github.com/miekg/pkcs11: invalid flag in #cgo LDFLAGS: -Wl,--no-as-needed
Makefile:227: recipe for target 'build/bin/peer' failed
make: *** [build/bin/peer] Error 1
Docker version 17.12.0-ce, build c97c6d6
docker-compose version 1.18.0, build 8dd22a9
go version go1.9.4 linux/amd64
OS : Ubuntu 16.04
I have set the following path also
$ mkdir $HOME/gopath
$ export GOPATH=$HOME/gopath
$ export GOROOT=$HOME/go
$ export PATH=$PATH:$GOROOT/bin
Jira Link: https://jira.hyperledger.org/browse/FAB-2218 which I have already done
It is a known issue in go 1.9.4:
https://github.com/golang/go/issues/23739
While go people are fixing it, you can manually whitelist the flags before starting make:
$ export CGO_LDFLAGS_ALLOW='-Wl,--no-as-needed'
$ make
Alternatively you can just downgrade to go 1.9.3
as far as I know you don't need to build the fabric itself.
You just need to have sources in the gopath, as your chaincode will need this, when you are building your chaincode for the blockchain itself.
In addition you will probably also need to install the fabric-ca for your chaincode to build successfully, if you use the certificate-stuff.
This is sufficient to check if you installed fabric correctly:
cd $GOPATH/src/github.com/hyperledger/fabric
make unit-test
Keep going if this works.
I found this confusing at first as well ;)
I'd assume you switch to golang 1.9.4 or greater hence I belive this actually related to following issue, to solve it you need to update vendor dependencies:
govendor fetch github.com/hyperledger/fabric/vendor/github.com/miekg/pkcs11
Next continue to build binaries and run tests.

How to freeze micro version with dependencies?

I want to build a docker image with a fixed version of micro and go dependencies. I plan to do it with dep:
git checkout git#github.com:micro/micro.git
dep ensure
git add Gopkg.toml
git add Gopkg.lock
# Build micro
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o micro ./main.go
# Build docker image
...
So, my question is does it the best solution to build consistent micro docker image?
An example of a Dockerfile can be:
FROM golang:1.9-alpine3.6 as builder
# Install package manager
RUN apk add --no-cache --virtual .go-dependencies git curl \
&& curl https://glide.sh/get | sh
# Copy files from context
WORKDIR /go/src/github.com/foo/bar
COPY . .
# Install project dependencies, test and build
RUN glide install \
&& go test ./... \
&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o ./entry ./main.go ./plugins.go
# Build final image with binary
FROM alpine:3.6
RUN apk add --update ca-certificates && \
rm -rf /var/cache/apk/* /tmp/*
WORKDIR /
COPY --from=builder /go/src/github.com/foo/bar/entry .
ENTRYPOINT [ "/entry" ]
And the glide.yaml would look like this:
package: .
import:
- package: github.com/micro/go-micro
version: ^0.3.0
subpackages:
- client
- server
- package: github.com/micro/go-plugins
version: ^0.6.1
subpackages:
- wrapper/trace/opentracing
- broker/nats
- transport/nats
- package: github.com/opentracing/opentracing-go
version: ^1
- package: github.com/openzipkin/zipkin-go-opentracing
version: ^0.3
testImport:
- package: github.com/golang/mock
subpackages:
- gomock
- package: github.com/smartystreets/goconvey
subpackages:
- convey
In my case, dep looks great and fast enough, moreover, it's official dependency manager in go so I think it's a right choice.

How can I fix the following Google Cloud Platform deployment error? [Error 2]

I'm trying to deploy my web-app to Google Cloud Platform, and I got an error while deploying. I understand that last package (go-sqlite3) needs gcc compiler, and Google Cloud WM has it:
$which gcc
/usr/bin/gcc
but it won't work
Step 3 : RUN go-wrapper install -tags appenginevm
---> Running in b0f03024342d
+ exec go install -v -tags appenginevm
github.com/mattn/go-colorable
github.com/mattn/go-isatty
github.com/labstack/gommon/color
github.com/valyala/fasttemplate/vendor/github.com/valyala/bytebufferpool
github.com/valyala/fasttemplate
github.com/labstack/gommon/log
golang.org/x/crypto/acme
golang.org/x/crypto/acme/autocert
github.com/labstack/echo
golang.org/x/net/context
github.com/mattn/go-sqlite3
# github.com/mattn/go-sqlite3
exec: "gcc": executable file not found in $PATH
The command '/bin/sh -c go-wrapper install -tags appenginevm' returned a non-zero code: 2
ERROR
ERROR: build step "gcr.io/cloud-builders/docker#sha256:926dc1a14e6f7eb5b3462b5c1d491aa6c73090291167ac2bf181c026b05f19da" failed: exit status 2
You have to make your path correct by following command on your shell.
PATH=/usr/bin:$PATH

Resources