-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstatic-nginx.unprivileged-nginxuser.Dockerfile
More file actions
411 lines (352 loc) · 12.6 KB
/
static-nginx.unprivileged-nginxuser.Dockerfile
File metadata and controls
411 lines (352 loc) · 12.6 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# build: docker build --no-cache --progress=plain --target binary --build-arg NGINX_VERSION=1.29.1 -t tobi312/tools:static-nginx-unprivileged-nginxuser -f static-nginx.unprivileged-nginxuser.Dockerfile .
FROM alpine:latest AS builder
ARG PCRE2_VERSION=10.47
ARG ZLIB_VERSION=1.3.1
ARG OPENSSL_VERSION=3.5.4
ARG NGINX_VERSION=1.29.4
ARG VCS_REF
ENV BUILD_DIR=/usr/src
ENV OUTPUT_DIR=/nginx
LABEL org.opencontainers.image.title="Static NGINX"\
org.opencontainers.image.revision="${VCS_REF}" \
org.opencontainers.image.description="Static NGINX${NGINX_VERSION:+ ${NGINX_VERSION}} (unprivileged/nginxuser) build with pcre2${PCRE2_VERSION:+-${PCRE2_VERSION}}, zlib${ZLIB_VERSION:+-${ZLIB_VERSION}} and openssl${OPENSSL_VERSION:+-${OPENSSL_VERSION}}" \
org.opencontainers.image.source="https://github.com/Tob1as/docker-tools/"
SHELL ["/bin/ash", "-euxo", "pipefail", "-c"]
WORKDIR /usr/src
RUN echo ">> Install build packages ..." && \
apk add --no-cache --virtual .build-deps \
linux-headers \
musl-dev \
\
ca-certificates \
wget \
g++ \
make \
perl \
file \
tree \
\
autoconf \
automake \
libtool \
\
#geoip-dev geoip-static \
&& \
mkdir -p ${OUTPUT_DIR}
# https://github.com/maxmind/geoip-api-c
RUN GEOIP1_VERSION=1.6.12 && \
echo ">> Download and compile: GeoIP V1 ${GEOIP1_VERSION} ..." && \
wget https://github.com/maxmind/geoip-api-c/archive/refs/tags/v${GEOIP1_VERSION}.tar.gz && \
tar xf v${GEOIP1_VERSION}.tar.gz && \
rm v${GEOIP1_VERSION}.tar.gz && \
cd geoip-api-c-${GEOIP1_VERSION} && \
./bootstrap && \
./configure --enable-static --disable-shared && \
make -j$(nproc) && \
make install && \
cd ..
# https://github.com/PCRE2Project/pcre2
RUN echo ">> Download: pcre2-${PCRE2_VERSION} ..." && \
wget https://github.com/PCRE2Project/pcre2/releases/download/pcre2-${PCRE2_VERSION}/pcre2-${PCRE2_VERSION}.tar.gz && \
tar xf pcre2-${PCRE2_VERSION}.tar.gz
# https://github.com/madler/zlib
RUN echo ">> Download: zlib-${ZLIB_VERSION} ..." && \
wget https://zlib.net/zlib-${ZLIB_VERSION}.tar.gz && \
tar xf zlib-${ZLIB_VERSION}.tar.gz
# https://github.com/openssl/openssl
RUN echo ">> Download: openssl-${OPENSSL_VERSION} ..." && \
wget https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz && \
tar xf openssl-${OPENSSL_VERSION}.tar.gz
# nginx user
RUN echo 'nginx:x:101:101:nginx:/var/cache/nginx:/sbin/nologin' >> /etc/passwd ; \
echo 'nginx:x:101:nginx' >> /etc/group
# === Build NGINX static ===
# https://github.com/nginx/nginx && https://nginx.org/
# https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/#sources
# https://nginx.org/en/docs/configure.html
# configured build like: "docker run --rm --name nginx-info --entrypoint=nginx -it nginx:alpine-slim -V"
RUN echo ">> Download and BUILD: nginx-${NGINX_VERSION} ..." && \
wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz && \
tar xf nginx-${NGINX_VERSION}.tar.gz && \
cd nginx-${NGINX_VERSION} && \
./configure \
--with-pcre=../pcre2-${PCRE2_VERSION} \
--with-zlib=../zlib-${ZLIB_VERSION} \
--with-openssl=../openssl-${OPENSSL_VERSION} \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
#--error-log-path=/var/log/nginx/error.log \
--error-log-path=/dev/stderr \
#--http-log-path=/var/log/nginx/access.log \
--http-log-path=/dev/stdout \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-perl_modules_path=/usr/lib/perl5/vendor_perl \
--user=nginx \
--group=nginx \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-http_v3_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-cc-opt='-static -Os -fstack-clash-protection -Wformat -Werror=format-security -fno-plt -g' \
--with-ld-opt='-static -Wl,--as-needed,-O1,--sort-common' \
# others:
--with-http_geoip_module \
&& \
make -j$(nproc) && \
strip objs/nginx && \
cd ..
RUN echo ">> do something after builds ..." && \
mkdir -p ${OUTPUT_DIR}/etc/nginx/ ${OUTPUT_DIR}/var/cache/nginx/ ${OUTPUT_DIR}/usr/sbin ${OUTPUT_DIR}/usr/lib/nginx/modules ${OUTPUT_DIR}/var/run ${OUTPUT_DIR}/var/log/nginx ${OUTPUT_DIR}/usr/share/nginx/html ${OUTPUT_DIR}/etc/ssl/ && \
cp nginx-${NGINX_VERSION}/objs/nginx ${OUTPUT_DIR}/usr/sbin/ && \
cp -r nginx-${NGINX_VERSION}/conf/. ${OUTPUT_DIR}/etc/nginx/ && \
#cp -r nginx-${NGINX_VERSION}/html/. ${OUTPUT_DIR}/usr/share/nginx/html && \
mv ${OUTPUT_DIR}/etc/nginx/nginx.conf ${OUTPUT_DIR}/etc/nginx/nginx.conf.bak && \
file ${OUTPUT_DIR}/usr/sbin/nginx && \
#ldd ${OUTPUT_DIR}/usr/sbin/nginx && \
#tree ${OUTPUT_DIR} && \
${OUTPUT_DIR}/usr/sbin/nginx -V && \
${OUTPUT_DIR}/usr/sbin/nginx -help
COPY <<EOF /nginx/etc/nginx/nginx.conf
#user nginx;
#user nginx nginx;
worker_processes auto;
#error_log /var/log/nginx/error.log notice;
error_log /dev/stderr notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '
'\$status \$body_bytes_sent "\$http_referer" '
'"\$http_user_agent" "\$http_x_forwarded_for"';
#access_log /var/log/nginx/access.log main;
access_log /dev/stdout main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
#server_tokens off;
include /etc/nginx/conf.d/*.conf;
}
EOF
COPY <<EOF /nginx/etc/nginx/conf.d/default.conf
# HTTP server
server {
listen 80;
listen [::]:80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \\.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \\.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts\$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\\.ht {
# deny all;
#}
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 10.0.0.0/8;
allow 172.16.0.0/12;
allow 192.168.0.0/16;
allow ::1;
allow fc00::/7;
deny all;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; }
}
# HTTPS server
#server {
# listen 443 ssl;
# listen [::]:443 ssl;
# server_name localhost;
#
# ssl_certificate /etc/ssl/ssl.crt;
# ssl_certificate_key /etc/ssl/ssl.key;
#
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
#
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# #charset koi8-r;
#
# #access_log /var/log/nginx/access.log main;
#
# location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
# }
#
# #error_page 404 /404.html;
#
# # redirect server error pages to the static page /50x.html
# #
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# root /usr/share/nginx/html;
# }
#
# # proxy the PHP scripts to Apache listening on 127.0.0.1:80
# #
# #location ~ \\.php$ {
# # proxy_pass http://127.0.0.1;
# #}
#
# # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
# #
# #location ~ \\.php$ {
# # root html;
# # fastcgi_pass 127.0.0.1:9000;
# # fastcgi_index index.php;
# # fastcgi_param SCRIPT_FILENAME /scripts\$fastcgi_script_name;
# # include fastcgi_params;
# #}
#
# # deny access to .htaccess files, if Apache's document root
# # concurs with nginx's one
# #
# #location ~ /\\.ht {
# # deny all;
# #}
#
# location /nginx_status {
# stub_status on;
# access_log off;
# allow 127.0.0.1;
# allow 10.0.0.0/8;
# allow 172.16.0.0/12;
# allow 192.168.0.0/16;
# allow ::1;
# allow fc00::/7;
# deny all;
# }
#
# location = /favicon.ico { log_not_found off; access_log off; }
# location = /robots.txt { log_not_found off; }
#}
EOF
COPY <<EOF /nginx/usr/share/nginx/html/index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is working.
Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
This is a static nginx build, for more details see:
<a href="https://github.com/Tob1as/docker-tools/blob/main/static-nginx.unprivileged-nginxuser.Dockerfile">https://github.com/Tob1as/docker-tools</a>.</p>
<p><em>Have Fun!</em></p>
</body>
</html>
EOF
RUN tree ${OUTPUT_DIR}
# unprivileged / non-root user (patch)
RUN sed -i -E 's/^(\s*#?\s*listen\s+)(\[::\]:)?80(\b[^0-9])/\1\28080\3/' ${OUTPUT_DIR}/etc/nginx/conf.d/default.conf && \
sed -i -E 's/^(\s*#?\s*listen\s+)(\[::\]:)?443(\b[^0-9])/\1\28443\3/' ${OUTPUT_DIR}/etc/nginx/conf.d/default.conf && \
chown -R 101:101 /nginx/
FROM scratch AS binary
ARG VCS_REF
ARG BUILD_DATE
ARG PCRE2_VERSION
ARG ZLIB_VERSION
ARG OPENSSL_VERSION
ARG NGINX_VERSION
LABEL org.opencontainers.image.title="Static NGINX" \
#org.opencontainers.image.vendor="" \
org.opencontainers.image.authors="Tobias Hargesheimer <docker@ison.ws>" \
org.opencontainers.image.version="${NGINX_VERSION}" \
org.opencontainers.image.created="${BUILD_DATE}" \
org.opencontainers.image.revision="${VCS_REF}" \
org.opencontainers.image.description="Static NGINX${NGINX_VERSION:+ ${NGINX_VERSION}} (unprivileged/nginxuser) build with pcre2${PCRE2_VERSION:+-${PCRE2_VERSION}}, zlib${ZLIB_VERSION:+-${ZLIB_VERSION}} and openssl${OPENSSL_VERSION:+-${OPENSSL_VERSION}}" \
org.opencontainers.image.documentation="https://github.com/Tob1as/docker-tools/" \
org.opencontainers.image.base.name="scratch" \
org.opencontainers.image.licenses="BSD-2-Clause license" \
org.opencontainers.image.url="https://hub.docker.com/r/tobi312/tools" \
org.opencontainers.image.source="https://github.com/Tob1as/docker-tools/"
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /nginx /
COPY <<EOF /etc/passwd
#root:x:0:0:root:/root:/sbin/nologin
nginx:x:101:101:nginx:/var/cache/nginx:/sbin/nologin
EOF
COPY <<EOF /etc/group
#root:x:0:root
nginx:x:101:nginx
EOF
STOPSIGNAL SIGQUIT
# root user
#EXPOSE 80
# unprivileged / non-root user
EXPOSE 8080
USER 101
CMD ["nginx", "-g", "daemon off;"]