-
Notifications
You must be signed in to change notification settings - Fork 476
/
Dockerfile-linux.template
261 lines (251 loc) · 7.5 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
{{
def is_alpine:
env.variant | startswith("alpine")
;
def alpine_version:
env.variant | ltrimstr("alpine")
-}}
{{
def is_oracle:
env.variant | startswith("oraclelinux")
;
def oracle_version:
env.variant | ltrimstr("oraclelinux")
-}}
{{
def is_debian:
is_alpine or is_oracle | not
;
def is_debian_slim:
is_debian and (env.variant | startswith("slim-"))
;
def debian_suite:
env.variant | ltrimstr("slim-")
-}}
{{
if is_alpine then (
-}}
FROM alpine:{{ alpine_version }}
RUN apk add --no-cache java-cacerts
ENV JAVA_HOME /opt/openjdk-{{ env.version }}
{{
) elif is_oracle then (
-}}
FROM oraclelinux:{{ oracle_version }}-slim
RUN set -eux; \
microdnf install \
gzip \
tar \
\
# jlink --strip-debug on 13+ needs objcopy: https://github.com/docker-library/openjdk/issues/351
# Error: java.io.IOException: Cannot run program "objcopy": error=2, No such file or directory
binutils \
# java.lang.UnsatisfiedLinkError: /usr/java/openjdk-12/lib/libfontmanager.so: libfreetype.so.6: cannot open shared object file: No such file or directory
# https://github.com/docker-library/openjdk/pull/235#issuecomment-424466077
freetype fontconfig \
; \
microdnf clean all
ENV JAVA_HOME /usr/java/openjdk-{{ env.version }}
{{
) else (
-}}
FROM {{
if is_debian_slim then
"debian:" + debian_suite + "-slim"
else
"buildpack-deps:" + debian_suite + (
if env.javaType == "jdk" then
"-scm"
else
"-curl"
end
)
end
}}
RUN set -eux; \
apt-get update; \
apt-get install -y --no-install-recommends \
{{ if is_debian_slim then "" else ( -}}
bzip2 \
unzip \
xz-utils \
\
# jlink --strip-debug on 13+ needs objcopy: https://github.com/docker-library/openjdk/issues/351
# Error: java.io.IOException: Cannot run program "objcopy": error=2, No such file or directory
binutils \
\
# java.lang.UnsatisfiedLinkError: /usr/local/openjdk-11/lib/libfontmanager.so: libfreetype.so.6: cannot open shared object file: No such file or directory
# java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11FontManager
# https://github.com/docker-library/openjdk/pull/235#issuecomment-424466077
fontconfig libfreetype6 \
\
{{ ) end -}}
# utilities for keeping Debian and OpenJDK CA certificates in sync
ca-certificates p11-kit \
; \
rm -rf /var/lib/apt/lists/*
ENV JAVA_HOME /usr/local/openjdk-{{ env.version }}
{{
) end
-}}
ENV PATH $JAVA_HOME/bin:$PATH
{{ if is_alpine then "" else ( -}}
# Default to UTF-8 file.encoding
ENV LANG C.UTF-8
{{ ) end -}}
{{ def java_version: if is_alpine then .alpine.version else .version end -}}
# https://jdk.java.net/
# >
# > Java Development Kit builds, from Oracle
# >
ENV JAVA_VERSION {{ java_version }}
{{ if is_alpine then ( -}}
# "For Alpine Linux, builds are produced on a reduced schedule and may not be in sync with the other platforms."
{{ ) else "" end -}}
{{
def arches:
if is_alpine then .alpine else . end
| .[env.javaType].arches
;
def get_arch_command:
if is_alpine then
"apk --print-arch"
elif is_oracle then
"rpm --query --queryformat='%{ARCH}' rpm"
else
"dpkg --print-architecture"
end
;
def case_arch:
# input is a bashbrew arch
# - "amd64", "arm64v8", etc
# output is a shell "case" expression for matching the output of running "get_arch_command"
# - "i[3456]86", "aarch64", "x86_64", etc
. as $bashbrewArch
| if is_alpine then {
amd64: "x86_64",
arm64v8: "aarch64",
} elif is_oracle then {
amd64: "x86_64",
arm64v8: "aarch64",
} else {
amd64: "amd64",
arm64v8: "arm64",
} end
| .[$bashbrewArch] // error("unsupported bashbrew architecture: " + $bashbrewArch)
| @sh
;
def wget_command:
if is_oracle then
"curl -fL -o"
else
[
"wget",
if is_alpine then empty else "--progress=dot:giga" end,
"-O"
] | join(" ")
end
-}}
RUN set -eux; \
\
arch="$({{ get_arch_command }})"; \
case "$arch" in \
{{
[
arches | to_entries[]
| select(.key | startswith("windows-") | not)
| .key as $bashbrewArch | .value
| (
-}}
{{ $bashbrewArch | case_arch }}) \
downloadUrl={{ .url | @sh }}; \
{{ if .sha256 then ( -}}
downloadSha256={{ .sha256 | @sh }}; \
{{ ) else "" end -}}
;; \
{{
)
] | add
-}}
*) echo >&2 "error: unsupported architecture: '$arch'"; exit 1 ;; \
esac; \
\
{{ if is_debian_slim then ( -}}
savedAptMark="$(apt-mark showmanual)"; \
apt-get update; \
apt-get install -y --no-install-recommends \
wget \
; \
rm -rf /var/lib/apt/lists/*; \
\
{{ ) else "" end -}}
{{ wget_command }} openjdk.tgz "$downloadUrl"; \
{{ if [ arches[] ] | any(has("sha256")) then ( -}}
echo "$downloadSha256 *openjdk.tgz" | sha256sum {{ if is_alpine then "-c" else "--strict --check" end }} -; \
{{ ) else "" end -}}
\
mkdir -p "$JAVA_HOME"; \
tar --extract \
--file openjdk.tgz \
--directory "$JAVA_HOME" \
--strip-components 1 \
--no-same-owner \
; \
rm openjdk.tgz*; \
\
{{ if is_debian_slim then ( -}}
apt-mark auto '.*' > /dev/null; \
[ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
\
{{ ) else "" end -}}
{{ if is_alpine then ( -}}
rm -rf "$JAVA_HOME/lib/security/cacerts"; \
# see "java-cacerts" package installed above (which maintains "/etc/ssl/certs/java/cacerts" for us)
ln -sT /etc/ssl/certs/java/cacerts "$JAVA_HOME/lib/security/cacerts"; \
{{ ) elif is_oracle then ( -}}
rm -rf "$JAVA_HOME/lib/security/cacerts"; \
# see "update-ca-trust" script which creates/maintains this cacerts bundle
ln -sT /etc/pki/ca-trust/extracted/java/cacerts "$JAVA_HOME/lib/security/cacerts"; \
\
# https://github.com/oracle/docker-images/blob/a56e0d1ed968ff669d2e2ba8a1483d0f3acc80c0/OracleJava/java-8/Dockerfile#L17-L19
ln -sfT "$JAVA_HOME" /usr/java/default; \
ln -sfT "$JAVA_HOME" /usr/java/latest; \
for bin in "$JAVA_HOME/bin/"*; do \
base="$(basename "$bin")"; \
[ ! -e "/usr/bin/$base" ]; \
alternatives --install "/usr/bin/$base" "$base" "$bin" 20000; \
done; \
{{ ) else ( -}}
# update "cacerts" bundle to use Debian's CA certificates (and make sure it stays up-to-date with changes to Debian's store)
# see https://github.com/docker-library/openjdk/issues/327
# http://rabexc.org/posts/certificates-not-working-java#comment-4099504075
# https://salsa.debian.org/java-team/ca-certificates-java/blob/3e51a84e9104823319abeb31f880580e46f45a98/debian/jks-keystore.hook.in
# https://git.alpinelinux.org/aports/tree/community/java-cacerts/APKBUILD?id=761af65f38b4570093461e6546dcf6b179d2b624#n29
{ \
echo '#!/usr/bin/env bash'; \
echo 'set -Eeuo pipefail'; \
echo 'trust extract --overwrite --format=java-cacerts --filter=ca-anchors --purpose=server-auth "$JAVA_HOME/lib/security/cacerts"'; \
} > /etc/ca-certificates/update.d/docker-openjdk; \
chmod +x /etc/ca-certificates/update.d/docker-openjdk; \
/etc/ca-certificates/update.d/docker-openjdk; \
\
# https://github.com/docker-library/openjdk/issues/331#issuecomment-498834472
find "$JAVA_HOME/lib" -name '*.so' -exec dirname '{}' ';' | sort -u > /etc/ld.so.conf.d/docker-openjdk.conf; \
ldconfig; \
{{ ) end -}}
\
# https://github.com/docker-library/openjdk/issues/212#issuecomment-420979840
# https://openjdk.java.net/jeps/341
java -Xshare:dump; \
\
# basic smoke test
{{ if env.javaType == "jdk" then ( -}}
fileEncoding="$(echo 'System.out.println(System.getProperty("file.encoding"))' | jshell -s -)"; [ "$fileEncoding" = 'UTF-8' ]; rm -rf ~/.java; \
javac --version; \
java --version
# "jshell" is an interactive REPL for Java (see https://en.wikipedia.org/wiki/JShell)
CMD ["jshell"]
{{ ) else ( -}}
java --version
{{ ) end -}}