Richard
Richard

Reputation: 10648

how to create a statically linked golang executable with go 1.5+

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.

Upvotes: 10

Views: 11722

Answers (3)

Vivian K. Scott
Vivian K. Scott

Reputation: 892

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.

Upvotes: 2

Bryan
Bryan

Reputation: 12200

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" ...

Upvotes: 10

Richard
Richard

Reputation: 10648

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.

Upvotes: 11

Related Questions