10

golang version < 1.5 - there are plenty of static linking examples, posts and recipes. What about >= 1.5? (google search has returned no useful results for my search terms.) Anyone have any recommendations on how to produce a statically linked binary that can be executed inside a basic rkt (from CoreOS) container?

my go:

$go version
go version go1.5 linux/amd64

when I try to run 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

suggesting that the executable in the container is depending on this lib and hence not static.

my manifest looks like:

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:

go build ${myapp}.go

This article has a few examples golang < 1.5. And then there is this getting started article on the CoreOS site.

Richard
  • 10,122
  • 10
  • 42
  • 61
  • Can you show us how you're building your app? The process hasn't really changed. – JimB Oct 13 '15 at 21:47
  • and I added some links to articles. – Richard Oct 13 '15 at 22:00
  • 4
    have you set `CGO_ENABLED=0`? – JimB Oct 13 '15 at 22:04
  • Not required but could you possibly provide some context and/or links to information about `rkt`? Yours is the first use of the tag and I at least am not familiar with it. – evanmcdonnal Oct 13 '15 at 22:31
  • 1
    JimB's comment seems right. 1.5 doesn't dynamically link Go code by default, so your problem is probably with Go linking to the system's C library for DNS reoslution, which was also what older versions did. `CGO_ENABLED=0` turns that off. – twotwotwo Oct 13 '15 at 23:19
  • @evanmcdonnal: rkt: https://github.com/coreos/rkt – peterSO Oct 13 '15 at 23:36
  • While I still like to create static binaries from my go projects I have also started using rkt-builder to build my projects inside a rkt container. This has a number of cool side effects. – Richard Aug 15 '16 at 04:05

3 Answers3

11

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

While it was not part of the original question, once the program started executing in the rkt container it could not perform a proper DNS request. So there must be something else going on too.

Richard
  • 10,122
  • 10
  • 42
  • 61
  • Is this running on Mac OS X (darwin)? I thought this was the only platform where DNS lookups are not done by Go itself. – Volker Oct 14 '15 at 04:15
  • 1
    @Volker: dns lookups are done in go when nsswitch.conf permits, but the cgo resolver is still linked so it can be used when needed (there's also a ` GODEBUG=netdns=` option for switching back and forth at runtime, which needs both available) – JimB Oct 14 '15 at 12:52
  • 1
    @Richard you have to add your own /etc/hosts to your container, like they do [here](https://github.com/coreos/etcd/blob/master/scripts/build-aci#L64), in the etcd project. – David Castillo Mar 22 '16 at 19:47
10

Static linking:

Go 1.5:

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

With Go 1.6 I had to use:

go build -ldflags "-linkmode external -extldflags -static" ...
Bryan
  • 11,398
  • 3
  • 53
  • 78
1

try to build static link version :

go build -ldflags '-extldflags "-static"' -tags netgo,osusergo .

use -tags osusergo,netgo to force static build without glibc dependency library.

Vivian K. Scott
  • 515
  • 2
  • 5
  • 13