forked from danbooru/danbooru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerfile
321 lines (239 loc) · 10.8 KB
/
Dockerfile
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
# syntax=docker/dockerfile:1
# Build:
#
# git archive HEAD | docker buildx build - --tag danbooru --load --build-arg SOURCE_COMMIT=$(git rev-parse HEAD)
#
# Run:
#
# # Run the Danbooru webserver. Run Postgres first because a database is necessary for Danbooru to function.
# docker run --rm -it --network host -e POSTGRES_USER=danbooru -e POSTGRES_HOST_AUTH_METHOD=trust -v $PWD/danbooru-postgres:/var/lib/postgresql/data ghcr.io/danbooru/postgres:14.1 -c listen_addresses=localhost
# docker run --rm -it --network host -v $PWD:/danbooru danbooru
#
# # Run a Bash or Ruby shell inside the Danbooru container (for development or debugging).
# docker run --rm -it --network host -v $PWD:/danbooru danbooru bash
# docker run --rm -it --network host -v $PWD:/danbooru danbooru bin/rails console
#
# See https://github.com/danbooru/danbooru/wiki/Docker-Guide for more details.
ARG MOZJPEG_URL="https://github.com/mozilla/mozjpeg/archive/refs/tags/v4.1.1.tar.gz"
ARG VIPS_URL="https://github.com/libvips/libvips/releases/download/v8.14.2/vips-8.14.2.tar.xz"
ARG FFMPEG_URL="https://github.com/FFmpeg/FFmpeg/archive/refs/tags/n6.0.tar.gz"
ARG EXIFTOOL_URL="https://github.com/exiftool/exiftool/archive/refs/tags/12.56.tar.gz"
ARG OPENRESTY_URL="https://openresty.org/download/openresty-1.21.4.1.tar.gz"
ARG RUBY_URL="https://cache.ruby-lang.org/pub/ruby/3.2/ruby-3.2.1.tar.gz"
ARG POSTGRESQL_CLIENT_VERSION="14"
ARG NODE_VERSION="18.x"
# The base layer for everything.
FROM ubuntu:22.10 AS base
SHELL ["/bin/bash", "-xeuo", "pipefail", "-O", "globstar", "-O", "dotglob", "-c"]
ENV DEBIAN_FRONTEND="noninteractive"
RUN <<EOS
apt-get update
rm -rf /usr/local/*
EOS
# The base layer for building dependencies. All builds take place inside /build.
FROM base AS build-base
WORKDIR /build
ARG COMMON_BUILD_DEPS="curl ca-certificates build-essential pkg-config git"
RUN apt-get install -y --no-install-recommends $COMMON_BUILD_DEPS
# Build Ruby. Output is in /usr/local.
FROM build-base AS build-ruby
ARG RUBY_BUILD_DEPS="rustc libssl-dev libgmp-dev libyaml-dev libffi-dev libreadline-dev zlib1g-dev"
ARG RUBY_URL
RUN <<EOS
apt-get install -y --no-install-recommends $RUBY_BUILD_DEPS
curl -L $RUBY_URL | tar --strip-components=1 -xzvf -
./configure --enable-yjit --enable-shared --disable-install-doc
make -j install
find /usr/local -type f -executable -exec strip --strip-unneeded {} \;
rm -rf *
ruby --version
EOS
# Build MozJPEG. Output is in /usr/local.
FROM build-base AS build-mozjpeg
ARG MOZJPEG_BUILD_DEPS="cmake nasm libpng-dev zlib1g-dev"
ARG MOZJPEG_URL
RUN <<EOS
apt-get install -y --no-install-recommends $MOZJPEG_BUILD_DEPS
curl -L $MOZJPEG_URL | tar --strip-components=1 -xzvf -
cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DENABLE_STATIC=0 -DWITH_ARITH_ENC=1 -DWITH_ARITH_DEC=1 .
make -j install/strip
rm -rf * /usr/local/share /usr/local/man
cjpeg -version
EOS
# Build libvips. Output is in /usr/local.
FROM build-mozjpeg AS build-vips
ARG VIPS_BUILD_DEPS="meson libgirepository1.0-dev libfftw3-dev libwebp-dev liborc-dev liblcms2-dev libpng-dev libexpat1-dev libglib2.0-dev libgif-dev libexif-dev libheif-dev"
ARG VIPS_URL
RUN <<EOS
apt-get install -y --no-install-recommends $VIPS_BUILD_DEPS
curl -L $VIPS_URL | tar --strip-components=1 -xJvf -
meson build --prefix /usr/local --buildtype release --strip -Dcplusplus=false
meson compile -C build
meson install -C build
rm -rf * /usr/local/share /usr/local/man
ldconfig
vips --version
EOS
# Build FFmpeg. Output is in /usr/local.
FROM build-base AS build-ffmpeg
ARG FFMPEG_URL
ARG FFMPEG_BUILD_DEPS="nasm libvpx-dev libdav1d-dev zlib1g-dev"
ARG FFMPEG_BUILD_OPTIONS="\
--disable-ffplay --disable-network --disable-doc --disable-static --enable-shared \
--enable-libvpx --enable-libdav1d --enable-zlib \
--disable-muxers \
--enable-muxer=mp4 --enable-muxer=webm --enable-muxer=image2 --enable-muxer=null \
--disable-demuxers \
--enable-demuxer=mov,mp4,m4a,3gp,3g2,mj2 --enable-demuxer=matroska,webm --enable-demuxer=image2 \
--enable-demuxer=apng --enable-demuxer=gif \
--disable-filters \
--enable-filter=scale --enable-filter=thumbnail --enable-filter=silencedetect --enable-filter=ebur128 \
--enable-filter=aresample --enable-filter=anull --enable-filter=null --enable-filter=copy \
--disable-encoders \
--enable-encoder=libvpx_vp8 --enable-encoder=libvpx_vp9 --enable-encoder=png --enable-encoder=null \
--enable-encoder=wrapped_avframe --enable-encoder=pcm_s16le \
--disable-decoders \
--enable-decoder=vp8 --enable-decoder=vp9 --enable-decoder=h264 --enable-decoder=hevc --enable-decoder=libdav1d \
--enable-decoder=mpeg4 --enable-decoder=mjpeg --enable-decoder=png --enable-decoder=apng --enable-decoder=gif \
--enable-decoder=webp --enable-decoder=aac --enable-decoder=mp3 --enable-decoder=mp2 --enable-decoder=opus \
--enable-decoder=vorbis --enable-decoder=ac3 \
--disable-protocols \
--enable-protocol=file \
--disable-bsfs \
"
RUN <<EOS
apt-get install -y --no-install-recommends $FFMPEG_BUILD_DEPS
curl -L $FFMPEG_URL | tar --strip-components=1 -xzvf -
./configure $FFMPEG_BUILD_OPTIONS
make -j install
rm -rf * /usr/local/include /usr/local/share
ldconfig
ffmpeg -version
ffprobe -version
EOS
# Build ExifTool. Output is in /usr/local.
FROM build-base AS build-exiftool
ARG EXIFTOOL_BUILD_DEPS="perl perl-modules libarchive-zip-perl"
ARG EXIFTOOL_URL
RUN <<EOS
apt-get install -y --no-install-recommends $EXIFTOOL_BUILD_DEPS
curl -L $EXIFTOOL_URL | tar --strip-components=1 -xzvf -
perl Makefile.PL
make -j install
rm -rf * /usr/local/man /usr/local/share/**/*.pod
exiftool -ver
perl -e 'require Compress::Zlib'
perl -e 'require Archive::Zip'
perl -e 'require Digest::MD5'
EOS
# Build OpenResty. Output is in /usr/local.
FROM build-base AS build-openresty
ARG OPENRESTY_URL
ARG OPENRESTY_BUILD_DEPS="libssl-dev libpcre++-dev zlib1g-dev"
ARG OPENRESTY_BUILD_OPTIONS="\
--with-threads --with-compat --with-pcre-jit --with-file-aio \
--with-http_gunzip_module --with-http_gzip_static_module \
--with-http_realip_module --with-http_ssl_module \
--with-http_stub_status_module --with-http_v2_module \
"
RUN <<EOS
apt-get install -y --no-install-recommends $OPENRESTY_BUILD_DEPS
curl -L $OPENRESTY_URL | tar --strip-components=1 -xzvf -
./configure -j$(nproc) --prefix=/usr/local $OPENRESTY_BUILD_OPTIONS
make -j install
find /usr/local -type f -executable -exec strip --strip-unneeded {} \;
rm -rf * /usr/local/{site,pod,COPYRIGHT}
openresty -version
EOS
# Install NodeJS. Output is in /usr/local.
FROM build-base AS build-node
ARG NODE_VERSION
RUN <<EOS
apt-get install -y --no-install-recommends gnupg
. /etc/lsb-release
curl https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor > /usr/share/keyrings/nodesource.gpg
echo "deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_VERSION $DISTRIB_CODENAME main" > /etc/apt/sources.list.d/nodesource.list
apt-get update
apt-get download nodejs
dpkg --instdir=/build --force-all --install ./nodejs*.deb
mv -i usr/bin usr/lib /usr/local
find /usr/local -type f -executable -exec strip --strip-unneeded {} \;
rm -rf *
node --version
EOS
# Build Ruby gems. Output is in /usr/local.
FROM build-ruby AS build-gems
RUN apt-get install -y --no-install-recommends libpq-dev libglib2.0-dev
COPY --link Gemfile Gemfile.lock ./
RUN <<EOS
bundle install --system --no-cache --jobs $(nproc)
find /usr/local/lib/ruby/gems -regextype egrep -regex '.*\.(o|a|c|h|hh|hpp|exe|java|md|po|log|out|gem)$' -delete
find /usr/local/lib/ruby/gems -regextype egrep -regex '^.*/(Change|CHANGE|NEWS|LICENSE|COPYING|LEGAL|AUTHORS|CONTRIBUTORS|THANK|README|INSTALL|NOTICE|TODO).*$' -delete
find /usr/local/lib/ruby/gems -type f -executable -exec strip --strip-unneeded {} \;
rm -rf *
EOS
# Build the base Danbooru image. Pull in dependencies from previous layers and install runtime dependencies from apt-get.
FROM base AS danbooru-base
WORKDIR /danbooru
ARG POSTGRESQL_CLIENT_VERSION
ARG NODE_VERSION
COPY --link --from=build-vips /usr/local /usr/local
COPY --link --from=build-ruby /usr/local /usr/local
COPY --link --from=build-node /usr/local /usr/local
RUN <<EOS
apt-get install -y --no-install-recommends \
postgresql-client-${POSTGRESQL_CLIENT_VERSION} ca-certificates mkvtoolnix rclone openssl perl perl-modules libpq5 \
libgmpxx4ldbl zlib1g libfftw3-3 libwebp7 libwebpmux3 libwebpdemux2 liborc-0.4.0 liblcms2-2 libpng16-16 libexpat1 \
libglib2.0 libgif7 libexif12 libheif1 libvpx7 libdav1d6 libseccomp-dev libjemalloc2 libarchive13 libyaml-0-2 libffi8 \
libreadline8 libarchive-zip-perl tini busybox less ncdu curl
npm install -g yarn
gem install --no-document foreman
apt-get purge -y --allow-remove-essential pkg-config e2fsprogs libglib2.0-bin libglib2.0-doc mount procps python3 tzdata
apt-get autoremove -y
rm -rf /var/{lib,cache,log} /usr/share/{doc,info}/* /build
busybox --install -s
EOS
# Build Javascript and CSS assets. Output is in /danbooru/public/packs.
FROM danbooru-base AS build-assets
COPY --link .yarnrc.yml package.json yarn.lock ./
COPY --link .yarn/ ./.yarn/
RUN yarn install
COPY --link postcss.config.js babel.config.json Rakefile ./
COPY --link bin/rails bin/webpacker ./bin/
COPY --link config/application.rb config/boot.rb config/danbooru_default_config.rb config/webpacker.yml ./config/
COPY --link config/webpack/ ./config/webpack/
COPY --link public/images ./public/images
COPY --link public/fonts ./public/fonts
COPY --link app/components/ ./app/components/
COPY --link app/javascript/ ./app/javascript/
COPY --link Gemfile Gemfile.lock ./
COPY --link --from=build-gems /usr/local /usr/local
RUN bin/rails assets:precompile
# Build the final layer. Pull in the compiled assets and gems on top of the base Danbooru layer.
FROM danbooru-base AS production
COPY --link --from=build-ffmpeg /usr/local /usr/local
COPY --link --from=build-exiftool /usr/local /usr/local
COPY --link --from=build-openresty /usr/local /usr/local
COPY --link --from=build-assets /danbooru/public/packs /danbooru/public/packs
COPY --link --from=build-gems /usr/local /usr/local
COPY --link . /danbooru
# http://jemalloc.net/jemalloc.3.html#tuning
ENV LD_PRELOAD=libjemalloc.so.2
ENV MALLOC_CONF=background_thread:true,narenas:2,dirty_decay_ms:1000,muzzy_decay_ms:0,tcache:false
# https://github.com/ruby/ruby/blob/master/doc/yjit/yjit.md
ENV RUBY_YJIT_ENABLE=1
# Disable libvips warning messages
ENV VIPS_WARNING=0
ARG SOURCE_COMMIT=""
RUN <<EOS
echo $SOURCE_COMMIT > REVISION
ln -s /tmp tmp
ln -s packs public/packs-test
useradd --create-home --user-group danbooru
ldconfig
EOS
USER danbooru
ENTRYPOINT ["tini", "--"]
CMD ["bin/rails", "server"]
# https://github.com/opencontainers/image-spec/blob/main/annotations.md
LABEL org.opencontainers.image.source https://github.com/danbooru/danbooru