-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile.template
366 lines (357 loc) · 10.9 KB
/
Dockerfile.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
{{
def is_alpine:
env.variant | startswith("alpine")
;
def is_slim:
env.variant | startswith("slim-")
-}}
{{ if is_alpine then ( -}}
FROM alpine:{{ env.variant | ltrimstr("alpine") }}
{{ ) elif is_slim then ( -}}
FROM debian:{{ env.variant | ltrimstr("slim-") }}-slim
{{ ) else ( -}}
FROM buildpack-deps:{{ env.variant }}
{{ ) end -}}
{{ if is_alpine then ( -}}
RUN set -eux; \
apk add --no-cache \
bzip2 \
ca-certificates \
gmp-dev \
libffi-dev \
procps \
yaml-dev \
zlib-dev \
;
{{ ) elif is_slim then ( -}}
RUN set -eux; \
apt-get update; \
apt-get install -y --no-install-recommends \
bzip2 \
ca-certificates \
libffi-dev \
libgmp-dev \
libssl-dev \
libyaml-dev \
procps \
zlib1g-dev \
; \
rm -rf /var/lib/apt/lists/*
{{ ) else "" end -}}
# skip installing gem documentation
RUN set -eux; \
mkdir -p /usr/local/etc; \
{ \
echo 'install: --no-document'; \
echo 'update: --no-document'; \
} >> /usr/local/etc/gemrc
ENV LANG C.UTF-8
ENV RUBY_MAJOR {{ env.version }}
ENV RUBY_VERSION {{ .version }}
ENV RUBY_DOWNLOAD_SHA256 {{ .sha256 }}
ENV jemalloc_pkgver 3.6.0
ENV jemalloc_sha256 e16c2159dd3c81ca2dc3b5c9ef0d43e1f2f45b04548f42db12e7c12d7bdf84fe
# some of ruby's build scripts are written in ruby
# we purge system ruby later to make sure our final image uses what we just built
RUN set -eux; \
\
{{ if is_alpine then ( -}}
# readline-dev vs libedit-dev: https://bugs.ruby-lang.org/issues/11869 and https://github.com/docker-library/ruby/issues/75
apk add --no-cache --virtual .ruby-builddeps \
autoconf \
bison \
bzip2 \
bzip2-dev \
ca-certificates \
coreutils \
dpkg-dev dpkg \
g++ \
gcc \
gdbm-dev \
glib-dev \
libc-dev \
libffi-dev \
libxml2-dev \
libxslt-dev \
linux-headers \
make \
ncurses-dev \
openssl \
openssl-dev \
patch \
procps \
{{ if [ "3.0", "3.1", "3.2" ] | index(env.version | rtrimstr("-rc")) then ( -}}
readline-dev \
{{ ) else "" end -}}
ruby \
tar \
xz \
yaml-dev \
zlib-dev \
; \
{{ ) else ( -}}
savedAptMark="$(apt-mark showmanual)"; \
apt-get update; \
apt-get install -y --no-install-recommends \
bison \
dpkg-dev \
libgdbm-dev \
ruby \
{{ if is_slim then ( -}}
autoconf \
g++ \
gcc \
libbz2-dev \
libgdbm-compat-dev \
libglib2.0-dev \
libncurses-dev \
{{ if [ "3.0", "3.1", "3.2" ] | index(env.version | rtrimstr("-rc")) then ( -}}
libreadline-dev \
{{ ) else "" end -}}
libxml2-dev \
libxslt-dev \
make \
wget \
xz-utils \
{{ ) else "" end -}}
; \
rm -rf /var/lib/apt/lists/*; \
{{ ) end -}}
{{ if .rust.version then ( -}}
\
rustArch=; \
{{ def archVar: if is_alpine then "apkArch" else "dpkgArch" end -}}
{{ archVar }}="$({{ if is_alpine then "apk --print-arch" else "dpkg --print-architecture" end }})"; \
case "${{ archVar }}" in \
{{
(
.rustup.arches
| (if is_alpine then "musl" else "glibc" end) as $libcKey
| to_entries
| map(
.key as $bashbrewArch
| (
if is_alpine then
{
amd64: "x86_64",
arm32v6: "armhf",
arm32v7: "armv7",
arm64v8: "aarch64",
i386: "x86",
ppc64le: "ppc64le",
riscv64: "riscv64",
s390x: "s390x",
}
else
{
amd64: "amd64",
arm32v5: "armel",
arm32v7: "armhf",
arm64v8: "arm64",
i386: "i386",
mips64le: "mips64el",
ppc64le: "ppc64el",
riscv64: "riscv64",
s390x: "s390x",
}
end
)[$bashbrewArch] as $distroArch
| select($distroArch)
| .value
| select(has($libcKey))
| .[$libcKey]
| (
-}}
{{ $distroArch | @sh }}) rustArch={{ .arch | @sh }}; rustupUrl={{ .url | @sh }}; rustupSha256={{ .sha256 | @sh }} ;; \
{{
)
)
| join("")
)
-}}
esac; \
\
if [ -n "$rustArch" ]; then \
mkdir -p /tmp/rust; \
\
wget -O /tmp/rust/rustup-init "$rustupUrl"; \
echo "$rustupSha256 */tmp/rust/rustup-init" | sha256sum --check --strict; \
chmod +x /tmp/rust/rustup-init; \
\
export RUSTUP_HOME='/tmp/rust/rustup' CARGO_HOME='/tmp/rust/cargo'; \
export PATH="$CARGO_HOME/bin:$PATH"; \
/tmp/rust/rustup-init -y --no-modify-path --profile minimal --default-toolchain {{ .rust.version | @sh }} --default-host "$rustArch"; \
\
rustc --version; \
cargo --version; \
fi; \
{{ ) else "" end -}}
\
mkdir /jemalloc-stable && cd /jemalloc-stable && \
wget "https://github.com/jemalloc/jemalloc/releases/download/$jemalloc_pkgver/jemalloc-$jemalloc_pkgver.tar.bz2" && \
echo "$jemalloc_sha256 *jemalloc-$jemalloc_pkgver.tar.bz2" | sha256sum -c - && \
tar -xjf "jemalloc-$jemalloc_pkgver.tar.bz2" && cd "jemalloc-$jemalloc_pkgver"; \
wget https://raw.githubusercontent.com/fullstaq-labs/fullstaq-ruby-server-edition/main/resources/jemalloc_cxx_fix.patch; \
patch -t -p1 < jemalloc_cxx_fix.patch && rm jemalloc_cxx_fix.patch; \
./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var && \
make -j "$(nproc)" && make install && \
strip --strip-debug "/usr/lib/libjemalloc.so" && rm -rf "/usr/lib/pkgconfig"; \
cd / && rm -rf /jemalloc-stable; \
\
wget -O ruby.tar.xz "https://cache.ruby-lang.org/pub/ruby/${RUBY_MAJOR%-rc}/ruby-$RUBY_VERSION.tar.xz"; \
echo "$RUBY_DOWNLOAD_SHA256 *ruby.tar.xz" | sha256sum --check --strict; \
\
mkdir -p /usr/src/ruby; \
tar -xJf ruby.tar.xz -C /usr/src/ruby --strip-components=1; \
rm ruby.tar.xz; \
\
cd /usr/src/ruby; \
\
{{ if is_alpine then ( -}}
# https://github.com/docker-library/ruby/issues/196
# https://bugs.ruby-lang.org/issues/14387#note-13 (patch source)
# https://bugs.ruby-lang.org/issues/14387#note-16 ("Therefore ncopa's patch looks good for me in general." -- only breaks glibc which doesn't matter here)
wget -O 'thread-stack-fix.patch' 'https://bugs.ruby-lang.org/attachments/download/7081/0001-thread_pthread.c-make-get_main_stack-portable-on-lin.patch'; \
echo '3ab628a51d92fdf0d2b5835e93564857aea73e0c1de00313864a94a6255cb645 *thread-stack-fix.patch' | sha256sum --check --strict; \
patch -p1 -i thread-stack-fix.patch; \
rm thread-stack-fix.patch; \
\
{{
{
"2.6": {
url: "https://github.com/ruby/ruby/pull/3773",
commit: "fcc88da5eb162043adcba552646677d2ab5adf55",
sha256: "95565a8a8f0eddd39d76fe8af63fc34b6429c956c8f8fe0aa17282d37adced64",
},
}[env.version] | if . and env.variant != "alpine3.13" then (
-}}
# https://bugs.ruby-lang.org/issues/17723 (building with autoconf 2.70+ fails)
# {{ .url }}
wget -O 'autoconf-2.70.patch' 'https://github.com/ruby/ruby/commit/{{ .commit }}.patch'; \
echo '{{ .sha256 }} *autoconf-2.70.patch' | sha256sum --check --strict; \
patch -p1 -i autoconf-2.70.patch; \
rm autoconf-2.70.patch; \
\
{{ ) else "" end -}}
# the configure script does not detect isnan/isinf as macros
export ac_cv_func_isnan=yes ac_cv_func_isinf=yes; \
\
{{ ) else "" end -}}
# hack in "ENABLE_PATH_CHECK" disabling to suppress:
# warning: Insecure world writable dir
{ \
echo '#define ENABLE_PATH_CHECK 0'; \
echo; \
cat file.c; \
} > file.c.new; \
mv file.c.new file.c; \
\
autoconf; \
{{ if is_alpine and "3.0" == (env.version | rtrimstr("-rc")) then ( -}}
# fix builds on arm32v6/7 and s390x: https://github.com/docker-library/ruby/issues/308
# and don't break the other arches: https://github.com/docker-library/ruby/issues/365
{{ if .rust.version then "" else ( -}}
{{ archVar }}="$(apk --print-arch)"; \
{{ ) end -}}
case "${{ archVar }}" in \
s390x | armhf | armv7) \
apk add --no-cache libucontext-dev; \
export LIBS='-lucontext'; \
;; \
esac; \
{{ ) else "" end -}}
gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \
{{ if ( [ "3.1" ] | index(env.version) ) then ( -}}
# add ruby 3.1 jemalloc patch
wget -O 'ruby_31_jemalloc.patch' 'https://raw.githubusercontent.com/fullstaq-ruby/server-edition/main/resources/ruby_31_jemalloc.patch'; \
patch -p1 -ti ruby_31_jemalloc.patch; \
rm ruby_31_jemalloc.patch; \
{{ ) else "" end -}}
{{ if ( [ "3.3-rc" ] | index(env.version) ) then ( -}}
# add ruby 3.3-rc(preview3) jemalloc patch
wget -O 'ruby_33_preview3_jemalloc.patch' 'https://gist.githubusercontent.com/jitingcn/16c492b36575112febe1c82d3e45fe2a/raw/751ad215e3e2f12a3a00a55f649355826aab4ce4/ruby_33_preview3_jemalloc.patch'; \
patch -p1 -ti ruby_33_preview3_jemalloc.patch; \
rm ruby_33_preview3_jemalloc.patch; \
{{ ) else "" end -}}
{{ if ( [ "3.2" ] | index(env.version) ) then ( -}}
# add ruby 3.2 jemalloc patch
wget -O 'ruby_32_jemalloc.patch' 'https://gist.githubusercontent.com/jitingcn/4a518dbd1d0112bdb5789a65e6456033/raw/1e378904e1f3766f4faa1c5cb87e045a9bd1f6f2/ruby_32_jemalloc.patch'; \
patch -p1 -ti ruby_32_jemalloc.patch; \
rm ruby_32_jemalloc.patch; \
{{ ) else "" end -}}
./configure \
--build="$gnuArch" \
--disable-install-doc \
--enable-shared \
--with-jemalloc \
{{ if .rust.version then ( -}}
${rustArch:+--enable-yjit} \
{{ ) else "" end -}}
; \
make -j "$(nproc)"; \
make install; \
strip --strip-all /usr/local/bin/ruby; \
\
{{ if .rust.version then ( -}}
rm -rf /tmp/rust; \
{{ ) else "" end -}}
{{ if is_alpine then ( -}}
runDeps="$( \
scanelf --needed --nobanner --format '%n#p' --recursive /usr/local \
| tr ',' '\n' \
| sort -u \
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
| sed s/"so:libjemalloc.so.1"// \
)"; \
apk add --no-network --virtual .ruby-rundeps $runDeps; \
apk del --no-network .ruby-builddeps; \
{{ ) else ( -}}
apt-mark auto '.*' > /dev/null; \
apt-mark manual $savedAptMark > /dev/null; \
find /usr/local -type f -executable -not \( -name '*tkinter*' \) -exec ldd '{}' ';' \
| awk '/=>/ { so = $(NF-1); if (index(so, "/usr/local/") == 1) { next }; gsub("^/(usr/)?", "", so); print so }' \
| sort -u \
| xargs -r dpkg-query --search \
| cut -d: -f1 \
| sort -u \
| xargs -r apt-mark manual \
; \
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
{{ ) end -}}
\
cd /; \
rm -r /usr/src/ruby; \
rm /usr/lib/libjemalloc.a; \
rm /usr/lib/libjemalloc_pic.a; \
# verify we have no "ruby" packages installed
{{ if is_alpine then ( -}}
if \
apk --no-network list --installed \
| grep -v '^[.]ruby-rundeps' \
| grep -i ruby \
; then \
exit 1; \
fi; \
{{ ) else ( -}}
if dpkg -l | grep -i ruby; then exit 1; fi; \
{{ ) end -}}
[ "$(command -v ruby)" = '/usr/local/bin/ruby' ]; \
# rough smoke test
ruby --version; \
gem --version; \
bundle --version
# Sanity check for jemalloc
RUN ruby -r rbconfig -e "abort 'jemalloc not enabled' unless RbConfig::CONFIG['LIBS'].include?('jemalloc') || RbConfig::CONFIG['MAINLIBS'].include?('jemalloc')"; \
ruby -e 'abort "jemalloc not enabled" if `MALLOC_CONF=stats_print:true ruby -e "exit" 2>&1` == ""'
RUN MALLOC_CONF="stats_print:true" \
LD_PRELOAD="/usr/lib/libjemalloc.so" \
ruby -e "exit" 2>&1 | grep "3.6.0-0"
ENV LD_PRELOAD=/usr/lib/libjemalloc.so
# don't create ".bundle" in all our apps
ENV GEM_HOME /usr/local/bundle
ENV BUNDLE_SILENCE_ROOT_WARNING=1 \
BUNDLE_APP_CONFIG="$GEM_HOME"
ENV PATH $GEM_HOME/bin:$PATH
# adjust permissions of a few directories for running "gem install" as an arbitrary user
RUN mkdir -p "$GEM_HOME" && chmod 1777 "$GEM_HOME"
CMD [ "irb" ]