Skip to content

Instantly share code, notes, and snippets.

@richard24se
Last active May 31, 2021 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richard24se/80f67461078ba26eb8abd044dfbeac76 to your computer and use it in GitHub Desktop.
Save richard24se/80f67461078ba26eb8abd044dfbeac76 to your computer and use it in GitHub Desktop.
#source https://github.com/docker-library/golang/blob/ee2d52a7ad3e077af02313cd4cd87fd39837412c/1.15/alpine3.12/Dockerfile
#custom
rm -rf /usr/local/go/src/*
apk add build-base
apk add gnupg go bash
apk add --no-cache \
ca-certificates
# set up nsswitch.conf for Go's "netgo" implementation
# - https://github.com/golang/go/blob/go1.9.1/src/net/conf.go#L194-L275
# - docker run --rm debian:stretch grep '^hosts:' /etc/nsswitch.conf
[ ! -e /etc/nsswitch.conf ] && echo 'hosts: files dns' > /etc/nsswitch.conf
set -eux; apk add --no-cache --virtual .build-deps
apk add --no-cache --virtual gcc gnupg go musl-dev openssl
export \
# set GOROOT_BOOTSTRAP such that we can actually build Go
GOROOT_BOOTSTRAP="$(go env GOROOT)" \
# ... and set "cross-building" related vars to the installed system's values so that we create a build targeting the proper arch
# (for example, if our build host is GOARCH=amd64, but our build env/image is GOARCH=386, our build needs GOARCH=386)
GOOS="$(go env GOOS)" \
GOARCH="$(go env GOARCH)" \
GOHOSTOS="$(go env GOHOSTOS)" \
GOHOSTARCH="$(go env GOHOSTARCH)" \
; \
# also explicitly set GO386 and GOARM if appropriate
# https://github.com/docker-library/golang/issues/184
apkArch="$(apk --print-arch)"; \
case "$apkArch" in \
x86_64) export GOARCH='amd64' GOOS='linux';; \
armhf) export GOARM='6' ;; \
armv7) export GOARM='7' ;; \
x86) export GO386='387' ;;
esac; \
\
# https://github.com/golang/go/issues/38536#issuecomment-616897960
url='https://dl.google.com/go/go1.16.4.src.tar.gz'; \
sha256='ae4f6b6e2a1677d31817984655a762074b5356da50fb58722b99104870d43503'; \
\
wget -O go.tgz.asc "$url.asc"; \
wget -O go.tgz "$url"; \
echo "$sha256 *go.tgz" | sha256sum -c -; \
\
# https://github.com/golang/go/issues/14739#issuecomment-324767697
export GNUPGHOME="$(mktemp -d)"; \
# https://www.google.com/linuxrepositories/
gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys 'EB4C 1BFD 4F04 2F6D DDCC EC91 7721 F63B D38B 4796'; \
gpg --batch --verify go.tgz.asc go.tgz; \
gpgconf --kill all; \
rm -rf "$GNUPGHOME" go.tgz.asc; \
\
tar -C /usr/local -xzf go.tgz; \
rm go.tgz; \
\
goEnv="$(go env | sed -rn -e '/^GO(OS|ARCH|ARM|386)=/s//export \0/p')"; \
eval "$goEnv"; \
[ -n "$GOOS" ]; \
[ -n "$GOARCH" ]; \
( \
cd /usr/local/go/src; \
./make.bash; \
); \
\
apk del --no-network .build-deps; \
\
# pre-compile the standard library, just like the official binary release tarballs do
go install std; \
# go install: -race is only supported on linux/amd64, linux/ppc64le, linux/arm64, freebsd/amd64, netbsd/amd64, darwin/amd64 and windows/amd64
# go install -race std; \
\
# remove a few intermediate / bootstrapping files the official binary release tarballs do not contain
rm -rf \
/usr/local/go/pkg/*/cmd \
/usr/local/go/pkg/bootstrap \
/usr/local/go/pkg/obj \
/usr/local/go/pkg/tool/*/api \
/usr/local/go/pkg/tool/*/go_bootstrap \
/usr/local/go/src/cmd/dist/dist \
; \
\
go version
echo 'export PATH=$PATH:/usr/local/go/bin'>>~/.profile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment