forked from docker-library/golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile-linux.template
203 lines (196 loc) · 5.59 KB
/
Dockerfile-linux.template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
{{
def is_alpine:
env.variant | startswith("alpine")
;
def alpine_version:
env.variant | ltrimstr("alpine")
-}}
{{ if is_alpine then ( -}}
FROM alpine:{{ alpine_version }}
RUN 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
RUN [ ! -e /etc/nsswitch.conf ] && echo 'hosts: files dns' > /etc/nsswitch.conf
{{ ) else ( -}}
FROM buildpack-deps:{{ env.variant }}-scm
# install cgo-related dependencies
RUN set -eux; \
apt-get update; \
apt-get install -y --no-install-recommends \
g++ \
gcc \
libc6-dev \
make \
pkg-config \
; \
rm -rf /var/lib/apt/lists/*
{{ ) end -}}
ENV PATH /usr/local/go/bin:$PATH
ENV GOLANG_VERSION {{ .version }}
{{
def os_arches:
if is_alpine then
{
amd64: "x86_64",
arm32v6: "armhf",
arm32v7: "armv7",
arm64v8: "aarch64",
i386: "x86",
ppc64le: "ppc64le",
s390x: "s390x",
}
else
{
amd64: "amd64",
arm32v5: "armel",
arm32v7: "armhf",
arm64v8: "arm64",
i386: "i386",
mips64le: "mips64el",
ppc64le: "ppc64el",
s390x: "s390x",
}
end
-}}
RUN set -eux; \
{{ if is_alpine then ( -}}
apk add --no-cache --virtual .fetch-deps gnupg; \
arch="$(apk --print-arch)"; \
{{ ) else ( -}}
arch="$(dpkg --print-architecture)"; arch="${arch##*-}"; \
{{ ) end -}}
url=; \
case "$arch" in \
{{
[
.arches | to_entries[]
| select(.value.supported)
| .key as $bashbrewArch
| (
os_arches
| .[$bashbrewArch] // empty
) as $osArch
| .value
| (
-}}
{{ $osArch | @sh }}) \
{{ if .url and (is_alpine | not) then ( -}}
url={{ .url | @sh }}; \
sha256={{ .sha256 | @sh }}; \
{{ ) else ( -}}
export {{ .env | to_entries | map(.key + "=" + (.value | @sh)) | join(" ") }}; \
{{ ) end -}}
;; \
{{
)
] | add
-}}
*) echo >&2 "error: unsupported architecture '$arch' (likely packaging update needed)"; exit 1 ;; \
esac; \
build=; \
if [ -z "$url" ]; then \
# https://github.com/golang/go/issues/38536#issuecomment-616897960
build=1; \
url={{ .arches.src.url | @sh }}; \
sha256={{ .arches.src.sha256 | @sh }}; \
{{ if is_alpine then ( -}}
# the precompiled binaries published by Go upstream are not compatible with Alpine, so we always build from source here 😅
{{ ) else ( -}}
echo >&2; \
echo >&2 "warning: current architecture ($arch) does not have a compatible Go binary release; will be building from source"; \
echo >&2; \
{{ ) end -}}
fi; \
\
wget -O go.tgz.asc "$url.asc"; \
wget -O go.tgz "$url"{{ if is_alpine then "" else " --progress=dot:giga" end }}; \
echo "$sha256 *go.tgz" | sha256sum -c -; \
\
# https://github.com/golang/go/issues/14739#issuecomment-324767697
GNUPGHOME="$(mktemp -d)"; export GNUPGHOME; \
# https://www.google.com/linuxrepositories/
gpg --batch --keyserver keyserver.ubuntu.com --recv-keys 'EB4C 1BFD 4F04 2F6D DDCC EC91 7721 F63B D38B 4796'; \
# let's also fetch the specific subkey of that key explicitly that we expect "go.tgz.asc" to be signed by, just to make sure we definitely have it
gpg --batch --keyserver keyserver.ubuntu.com --recv-keys '2F52 8D36 D67B 69ED F998 D857 78BD 6547 3CB3 BD13'; \
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; \
\
if [ -n "$build" ]; then \
{{ if is_alpine then ( -}}
apk add --no-cache --virtual .build-deps \
bash \
gcc \
go \
musl-dev \
; \
{{ ) else ( -}}
savedAptMark="$(apt-mark showmanual)"; \
apt-get update; \
apt-get install -y --no-install-recommends golang-go; \
{{ ) end -}}
\
( \
cd /usr/local/go/src; \
# set GOROOT_BOOTSTRAP + GOHOST* such that we can build Go successfully
export GOROOT_BOOTSTRAP="$(go env GOROOT)" GOHOSTOS="$GOOS" GOHOSTARCH="$GOARCH"; \
{{
def need_386_hack:
is_alpine and alpine_version == "3.13"
-}}
{{ if need_386_hack then ( -}}
if [ -n "${GO386:-}" ]; then \
# https://github.com/docker-library/golang/issues/359 -> https://github.com/golang/go/issues/44500
# Go 1.16 + Alpine 3.13 == Go 1.15 bootstrap
# (once Alpine 3.13 goes away, we can remove this)
GO386= ./bootstrap.bash; \
export GOROOT_BOOTSTRAP="/usr/local/go-$GOOS-$GOARCH-bootstrap"; \
"$GOROOT_BOOTSTRAP/bin/go" version; \
fi; \
{{ ) else "" end -}}
./make.bash; \
{{ if need_386_hack then ( -}}
if [ -n "${GO386:-}" ]; then \
rm -rf "$GOROOT_BOOTSTRAP"; \
fi; \
{{ ) else "" end -}}
); \
\
{{ if is_alpine then ( -}}
apk del --no-network .build-deps; \
{{ ) else ( -}}
apt-mark auto '.*' > /dev/null; \
apt-mark manual $savedAptMark > /dev/null; \
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
rm -rf /var/lib/apt/lists/*; \
{{ ) end -}}
\
# 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 \
; \
fi; \
\
{{ if is_alpine then ( -}}
apk del --no-network .fetch-deps; \
\
{{ ) else "" end -}}
go version
ENV GOPATH /go
ENV PATH $GOPATH/bin:$PATH
RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"
WORKDIR $GOPATH