How to create statically linked golang executable with go 1.5+

golang version <1.5 - there are many static links, messages, and recipes. What about> = 1.5? (google search did not return any useful results for my search terms.) Anyone have recommendations on creating a statically linked binary that can be executed inside the rkt base container (from CoreOS)?

my move:

$go version go version go1.5 linux/amd64 

when i try to start my container:

 sudo rkt --insecure-skip-verify run /tmp/FastBonusReport.aci 

I get:

 [38049.477658] FastBonusReport[4]: Error: Unable to open "/lib64/ld-linux-x86-64.so.2": No such file or directory 

assuming the executable in the container is dependent on this library and therefore not static.

My manifest looks like this:

 cat <<EOF > /tmp/${myapp}/manifest { "acKind": "ImageManifest", "acVersion": "0.9.0", "name": "${lowermyapp}", "labels": [ {"name": "os", "value": "linux"}, {"name": "arch", "value": "amd64"} ], "app": { "exec": [ "/bin/${myapp}" ], "user": "0", "group": "0" } } EOF 

my command line to build the binary looks like this:

 go build ${myapp}.go 

There are several examples of golang <1.5 in this article . And then there is this start of work on the CoreOS website.

+7
go rkt
source share
2 answers

I hate answering my question. The comments were correct. CGO_ENABLED=0 go build ./... seems to have done the trick.

While it was not part of the original question, as soon as the program started in the rkt container, it could not make a proper DNS query. So there must be something else going on too.

+8
source share

Static connection:

Go 1.5:

 go build -ldflags "-extldflags -static" ... 

With Go 1.6, I had to use:

 go build -ldflags "-linkmode external -extldflags -static" ... 
+5
source share

All Articles