This repository was archived by the owner on Nov 8, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathDockerfile.template
More file actions
258 lines (243 loc) · 12.4 KB
/
Copy pathDockerfile.template
File metadata and controls
258 lines (243 loc) · 12.4 KB
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
# Build arguments for version pinning and reproducibility
ARG BASE_VERSION=3.24.3
ARG BASE_HASH=be2656061b536c1883c4034790e21d0a09c5aaaf51968b89368a3c0371ea2c9a
FROM alpine:${BASE_VERSION}@sha256:${BASE_HASH} AS builder
ARG OPENSSL_VERSION
ARG APP_VERSION
SHELL ["/bin/ash", "-euo", "pipefail", "-c"]
ENV CC=clang \
CXX=clang++ \
LD=lld \
AR=llvm-ar \
NM=llvm-nm \
RANLIB=llvm-ranlib \
STRIP=llvm-strip
WORKDIR /tmp
# System setup and dependency installation
RUN addgroup --system --gid "10001" freenginx && adduser --disabled-password --shell /bin/false --ingroup freenginx --uid "10001" --no-create-home freenginx \
# Install build dependencies
&& apk add -q --no-cache \
clang22=22.1.3-r2 \
lld22=22.1.3-r0 \
llvm=22-r0 \
make=4.4.1-r4 \
git=2.54.0-r0 \
mimalloc2-dev=2.2.7-r0 \
build-base=0.5-r4 \
linux-headers=7.0.0-r1 \
upx=5.2.0-r0 \
perl=5.42.2-r1 \
cmake=4.2.3-r0 \
zstd-dev=1.5.7-r2 \
zstd-static=1.5.7-r2 \
brotli-dev=1.2.1-r0 \
brotli-static=1.2.1-r0 \
autoconf=2.73-r0 \
automake=1.18.1-r1 \
libtool=2.6.0-r1 \
python3-dev=3.14.10-r1 \
&& git config --global advice.detachedHead false \
&& PCRE_VERSION=$(git ls-remote --tags --refs https://github.com/PCRE2Project/pcre2 'pcre2-*' | grep -v -- '-RC' | sed 's|.*/||' | tail -1) \
&& ZLIB_VERSION=$(git ls-remote --tags --refs https://github.com/zlib-ng/zlib-ng | sed 's|.*/||' | sed 's/^v//' | sort -V | tail -1) \
&& ZSTD_VERSION=$(git ls-remote --tags --refs https://github.com/tokers/zstd-nginx-module | sed 's|.*/||' | sort -V | tail -1) \
&& NJS_VERSION=$(git ls-remote --tags --refs https://github.com/nginx/njs '[0-9]*' | sed 's|.*/||' | sort -V | tail -1) \
&& QJS_VERSION=$(git ls-remote --tags --refs https://github.com/quickjs-ng/quickjs 'v*' | sed 's|.*/||' | sort -V | tail -1) \
&& LIBXML2_VERSION=$(git ls-remote --tags --refs https://github.com/GNOME/libxml2 'v*' | sed 's|.*/||' | sort -V | tail -1) \
&& LIBXSLT_VERSION=$(git ls-remote --tags --refs https://github.com/GNOME/libxslt 'v*' | sed 's|.*/||' | sort -V | tail -1) \
# Clone FreeNGINX webserver and remove documentation
&& git clone -q --depth 1 --recursive -j8 --single-branch -b "release-${APP_VERSION}" https://github.com/freenginx/nginx freenginx && rm -rf /tmp/freenginx/docs/html/* \
# Clone OpenSSL with HTTP/3 and QUIC support
&& git clone -q --depth 1 --recursive -j8 --single-branch -b "openssl-${OPENSSL_VERSION}" https://github.com/openssl/openssl \
# Clone PCRE2 for regex with JIT support
&& git clone -q --depth 1 --recursive -j8 --single-branch -b "${PCRE_VERSION}" https://github.com/PCRE2Project/pcre2 \
# Clone zlib-ng for modern compression performance
&& git clone -q --depth 1 --recursive -j8 --single-branch -b "${ZLIB_VERSION}" https://github.com/zlib-ng/zlib-ng \
# Clone Brotli compression module
&& git clone -q --depth 1 --recurse-submodules -j8 https://github.com/google/ngx_brotli \
# Clone ZSTD compression module
&& git clone -q --depth 1 --recursive -j8 --single-branch -b "${ZSTD_VERSION}" https://github.com/tokers/zstd-nginx-module \
# Clone NJS module
&& git clone -q --depth 1 --recursive -j8 --single-branch -b "${NJS_VERSION}" https://github.com/nginx/njs \
# Clone QuickJS-NG engine
&& git clone -q --depth 1 --recursive -j8 --single-branch -b "${QJS_VERSION}" https://github.com/quickjs-ng/quickjs.git \
# Clone libxml2 for NJS
&& git clone -q --depth 1 --recursive -j8 --single-branch -b "${LIBXML2_VERSION}" https://github.com/GNOME/libxml2 \
# Clone libxslt for NJS
&& git clone -q --depth 1 --recursive -j8 --single-branch -b "${LIBXSLT_VERSION}" https://github.com/GNOME/libxslt \
# Remove Server header from HTTP responses (HTTP/1.1, HTTP/2, HTTP/3)
&& sed -ie 's@r->headers_out.server == NULL@0@g' \
/tmp/freenginx/src/http/ngx_http_header_filter_module.c \
/tmp/freenginx/src/http/v2/ngx_http_v2_filter_module.c \
/tmp/freenginx/src/http/v3/ngx_http_v3_filter_module.c \
# Remove default footer from error pages
&& sed -ie 's@<hr><center>freenginx</center>@@g' /tmp/freenginx/src/http/ngx_http_special_response.c
# Build QuickJS-NG library
WORKDIR /tmp/quickjs
RUN make -s -j "$(nproc)" \
&& cp ./build/libqjs.a ./libquickjs.a
# Build libxml2 library
WORKDIR /tmp/libxml2
RUN ./autogen.sh \
&& CFLAGS="-fPIC -O3 -march=x86-64 -fstack-protector-strong -fcf-protection=full \
-fvisibility=hidden -flto=full" \
./configure \
--disable-shared \
--enable-static \
&& make -s -j "$(nproc)" && make -s install && make -s clean
# Build libxslt library
WORKDIR /tmp/libxslt
RUN ./autogen.sh \
&& CFLAGS="-fPIC -O3 -march=x86-64 -fstack-protector-strong -fcf-protection=full \
-fvisibility=hidden -flto=full" \
./configure \
--disable-shared \
--enable-static \
--with-libxml-src=/tmp/libxml2 \
&& make -s -j "$(nproc)" && make -s install && make -s clean
# Build zlib-ng and Brotli compression libraries with optimizations
WORKDIR /tmp/zlib-ng
RUN mkdir -p /tmp/ngx_brotli/deps/brotli/out \
&& cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF \
-DCMAKE_C_FLAGS="-m64 -march=x86-64 -mtune=generic -funroll-loops -ffunction-sections -fdata-sections" \
-DCMAKE_CXX_FLAGS="-m64 -march=x86-64 -mtune=generic -funroll-loops -ffunction-sections -fdata-sections" \
-DCMAKE_INSTALL_PREFIX=./installed -B /tmp/ngx_brotli/deps/brotli/out -S /tmp/ngx_brotli/deps/brotli \
# Build Brotli encoder
&& cmake --build /tmp/ngx_brotli/deps/brotli/out --config Release --target brotlienc \
# Configure zlib-ng for compatibility mode
&& sed -i "s/compat=0/compat=1/" /tmp/zlib-ng/configure && ./configure --zlib-compat \
# Build and install zlib-ng
&& make -s -j "$(nproc)" && make -s install && make -s clean
# Configure and build FreeNGINX with security hardening and optimizations
WORKDIR /tmp/freenginx
RUN ./auto/configure \
--with-debug \
# Installation paths
--prefix="/etc/freenginx" \
--sbin-path="/usr/sbin/freenginx" \
--user="freenginx" \
--group="freenginx" \
--http-log-path="/dev/stdout" \
--error-log-path="/dev/stderr" \
--conf-path="/etc/freenginx/freenginx.conf" \
--pid-path="/tmp/freenginx.pid" \
--lock-path="/tmp/freenginx.lock" \
--http-client-body-temp-path="/tmp/client_temp" \
--http-proxy-temp-path="/tmp/proxy_temp" \
--http-fastcgi-temp-path="/tmp/fastcgi_temp" \
# SSL/TLS configuration with modern protocols
--with-openssl="/tmp/openssl" \
--with-openssl-opt="enable-ec_nistp_64_gcc_128 no-tls1 no-tls1_1 \
no-shared no-weak-ssl-ciphers enable-quic enable-ktls zlib" \
# External libraries
--with-pcre="/tmp/pcre2" \
--with-zlib="/tmp/zlib-ng" \
# Compilation flags
--with-cc-opt="-fPIE -O3 -march=x86-64 -flto=full \
-I/tmp/quickjs -I/usr/local/include \
-Wall -Wextra -Wformat=2 -Wimplicit-fallthrough \
-Werror=format-security -Werror=return-type -Wno-deprecated-declarations \
-Wno-unused-parameter -Wno-implicit-fallthrough -Wno-sign-compare \
-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -D_GLIBCXX_ASSERTIONS \
-fstrict-flex-arrays=3 -fstack-clash-protection -fstack-protector-strong \
-fcf-protection=full -ftrivial-auto-var-init=pattern \
-fno-delete-null-pointer-checks -fno-strict-overflow \
-fasynchronous-unwind-tables -fomit-frame-pointer -DTCP_FASTOPEN=23" \
--with-ld-opt="-fuse-ld=lld -flto=full -L/tmp/quickjs -L/usr/local/lib \
-lmimalloc -lxml2 -lxslt -lexslt -lz -lm -static-pie \
-Wl,-z,nodlopen -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now \
-Wl,-z,separate-code -Wl,-z,shstk -Wl,-z,force-ibt -Wl,--no-warnings \
-Wl,--as-needed -Wl,--no-copy-dt-needed-entries" \
# Core features
--with-compat \
--with-pcre-jit \
--with-threads \
# HTTP modules
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_v3_module \
--with-http_gzip_static_module \
# Stream modules
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
# Remove unnecessary modules to reduce attack surface
--without-stream_split_clients_module \
--without-stream_set_module \
--without-http_geo_module \
--without-http_scgi_module \
--without-http_uwsgi_module \
--without-http_autoindex_module \
--without-http_split_clients_module \
--without-http_memcached_module \
--without-http_ssi_module \
--without-http_empty_gif_module \
--without-http_browser_module \
--without-http_userid_module \
--without-http_mirror_module \
--without-http_referer_module \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
# Third-party modules
--add-module="/tmp/ngx_brotli" \
--add-module="/tmp/zstd-nginx-module" \
--add-module="/tmp/njs/nginx" \
# Build and install FreeNGINX
&& make -s -j "$(nproc)" && make -s install \
&& file objs/src/http/ngx_http.o objs/src/core/nginx.o | grep -i 'LLVM IR bitcode' && make -s clean \
# Security hardening: strip debug symbols and compress binary
&& strings /usr/sbin/freenginx | grep -iE "zlib-ng/zlib.h|mimalloc_version|quickjs/quickjs.h" \
&& llvm-strip --strip-all /usr/sbin/freenginx \
&& upx -qq --best --lzma /usr/sbin/freenginx \
&& ls -lash /usr/sbin/freenginx \
&& file /usr/sbin/freenginx | grep 'statically linked' \
&& readelf -dn /usr/sbin/freenginx | grep -E 'Properties|no dynamic' \
# Set proper permissions
&& chown -R "10001:10001" /etc/freenginx && rm -rf /tmp/* \
&& grep -E "(root|freenginx)" /etc/passwd > /tmp/passwd \
&& grep -E "(root|freenginx)" /etc/group > /tmp/group \
&& grep -E "(root|freenginx)" /etc/shadow > /tmp/shadow \
&& find /etc/freenginx -type d -exec chmod 550 {} \; \
&& find /etc/freenginx -type f -name "*.conf" -exec chmod 440 {} \; \
&& find /etc/freenginx -type f -name "*.types" -exec chmod 440 {} \; \
&& chmod 644 /tmp/passwd /tmp/group && chmod 640 /tmp/shadow
# Final distroless image
FROM scratch
ARG APP_VERSION
ARG BUILD_DATE
ARG VCS_REF
# Copy minimal runtime dependencies from builder
WORKDIR /tmp
COPY --from=builder --chown="0:0" /tmp/passwd /tmp/group /tmp/shadow /etc/
COPY --from=builder --chown="10001:10001" --chmod="550" /usr/sbin/freenginx /usr/sbin/freenginx
COPY --from=builder --chown="10001:10001" /etc/freenginx /etc/freenginx
# Copy configuration files
COPY --chown="10001:10001" ./freenginx.conf /etc/freenginx/freenginx.conf
COPY --chown="10001:10001" ./default.conf /etc/freenginx/conf.d/default.conf
# Expose HTTP, HTTPS, and QUIC ports
EXPOSE 8080/tcp 8443/tcp 8443/udp
# OCI labels for image metadata
LABEL description="Distroless FreeNGINX with HTTP/3, QUIC, ECH and PQC support🚀" \
maintainer="ammnt" \
org.opencontainers.image.description="Distroless FreeNGINX with HTTP/3, QUIC, ECH and PQC support🚀" \
org.opencontainers.image.authors="ammnt" \
org.opencontainers.image.title="Distroless FreeNGINX with HTTP/3, QUIC, ECH and PQC support🚀" \
org.opencontainers.image.source="https://github.com/ammnt/freenginx/" \
org.opencontainers.image.created=${BUILD_DATE} \
org.opencontainers.image.documentation="https://github.com/ammnt/freenginx/blob/main/README.md" \
org.opencontainers.image.licenses="BSD-2-Clause License" \
org.opencontainers.image.url="https://ammnt.fun" \
org.opencontainers.image.version=${APP_VERSION} \
org.opencontainers.image.revision=${VCS_REF}
# Health check for container orchestration
HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \
CMD ["/usr/sbin/freenginx", "-qt"]
# Use SIGQUIT for graceful shutdown with connection draining
STOPSIGNAL SIGQUIT
# Run as non-root user for security
USER 10001:10001
# Start FreeNGINX in foreground mode
ENTRYPOINT ["/usr/sbin/freenginx", "-g", "daemon off;"]