CenturyLinkLabs / golang-builder

Containerized build environment for compiling an executable Golang package and packaging it in a light-weight Docker container.
Apache License 2.0
398 stars 84 forks source link

Golang packages are dynamically linked #9

Open utrack opened 9 years ago

utrack commented 9 years ago

Because of root Docker's golang image, static builds are not available.

Dynamically linked gotools from Docker's golang image are unable to produce static builds. Example ldd output from freshly created binary:

oldie% ldd bin/server
        linux-vdso.so.1 (0x00007ffc0f4f9000)
        libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f10b9e54000)
        libc.so.6 => /usr/lib/libc.so.6 (0x00007f10b9ab2000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f10ba071000)
bdehamer commented 9 years ago

Not sure what might be going on here, we don't seem to have any problems creating statically linked binaries. Compare this:

bdehamer:watchtower $ go build
bdehamer:watchtower $ ldd watchtower 
        linux-vdso.so.1 =>  (0x00007fff325ce000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fcfad358000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcfacf93000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fcfad57f000)

with this:

bdehamer:watchtower $ docker run --rm -v $(pwd):/src centurylink/golang-builder
Building github.com/CenturyLinkLabs/watchtower
bdehamer:watchtower $ ldd watchtower 
        not a dynamic executable
utrack commented 9 years ago

@bdehamer I read some stuff on the matter... It looks like dynamic builds are produced when the binary uses net package or something from os. http://grokbase.com/t/gg/golang-nuts/137jbj44wt/go-nuts-re-go-program-isnt-compiled-to-static-binary-if-uses-net-package Build fails if I pass -d ldflag (which discards dynamic links). I guess I'll just fork golang container and build go tools in it statically for now.