How do I compile the go compiler statically with CGO support?
Compiling the sources with CGO_ENABLED=0 and GO_LDFLAGS='-extldflags "-static"' results in a statically linked binary:
../bin/go: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
However, using the linker flags and CGO_ENABLED=1 does not produced a statically linked binary.
This is what I have done so far:
FROM debian:stretch-slim AS builder
ENV CGO_ENABLED=1
ENV GO_LDFLAGS='-extldflags "-static"'
ENV GOROOT_BOOTSTRAP=/usr/local/go
RUN apt update && apt upgrade -y && apt install -y curl git build-essential make cmake
RUN curl -fLOSs "https://dl.google.com/go/go1.9.3.linux-amd64.tar.gz" \
&& tar -C /usr/local -xzf "go1.9.3.linux-amd64.tar.gz"
RUN git clone https://go.googlesource.com/go \
&& cd go \
&& git checkout go1.9.3 \
&& cd src \
&& ./make.bash
RUN apt install -y file && /usr/bin/file /go/bin/go
FROM scratch
ENV GOPATH=/code
COPY --from=builder /go/bin /usr/local/go/bin
CMD ["/usr/local/go/bin/go"]