Skip to content
This repository was archived by the owner on Jan 11, 2025. It is now read-only.

Commit da1003b

Browse files
fabiocicerchiaweb-flow
authored andcommitted
added multistage build to reduce size (#4)
improved image size: alpine -4%, amazonlinux -38%, centos -2%, debian -49%, fedora -12%, ubuntu -40%
1 parent 0213c08 commit da1003b

File tree

20 files changed

+2854
-2079
lines changed

20 files changed

+2854
-2079
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ Lua is a lightweight, high-level, multi-paradigm programming language designed p
6363
- [nginx](https://nginx.org/en/download.html)
6464
- Supported OS
6565
- [Alpine Linux](https://hub.docker.com/_/alpine) (~30MB)
66-
- [Amazon Linux](https://hub.docker.com/_/amazonlinux) (~300MB)
66+
- [Amazon Linux](https://hub.docker.com/_/amazonlinux) (~200MB)
6767
- [CentOS](https://hub.docker.com/_/centos) (~250MB)
68-
- [Debian](https://hub.docker.com/_/debian) (~250MB)
68+
- [Debian](https://hub.docker.com/_/debian) (~150MB)
6969
- [Fedora](https://hub.docker.com/_/fedora) (~250MB)
70-
- [Ubuntu](https://hub.docker.com/_/ubuntu) (~350MB)
70+
- [Ubuntu](https://hub.docker.com/_/ubuntu) (~200MB)
7171
- [OpenResty's Branch of LuaJIT 2](https://github.com/openresty/luajit2)
7272
- [Embed the Power of Lua into NGINX HTTP servers](https://github.com/openresty/lua-nginx-module)
7373
- [New FFI-based API for lua-nginx-module](https://github.com/openresty/lua-resty-core)

bin/docker-build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function build() {
4747
TAGS="$TAGS -t fabiocicerchia/nginx-lua:$MINOR-$OS$OS_VER"
4848
TAGS="$TAGS -t fabiocicerchia/nginx-lua:$MAJOR-$OS$OS_VER"
4949

50-
BUILD_DATE=$(date +%Y%m%d%H%M%S)
50+
BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
5151
BUILD_VERSION=$(date +%s)
5252
VCS_REF=$(git rev-parse --short HEAD)
5353
time docker build \

nginx/1.19.0/alpine/3.11.6/Dockerfile

Lines changed: 165 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,39 @@
1-
FROM alpine:3.11.6
1+
ARG DOCKER_IMAGE=fabiocicerchia/nginx-lua
2+
ARG DOCKER_IMAGE_OS=alpine
3+
ARG DOCKER_IMAGE_TAG=3.11.6
24

3-
LABEL maintainer="[email protected]"
5+
####################################
6+
# Build Nginx with support for LUA #
7+
####################################
8+
FROM $DOCKER_IMAGE_OS:$DOCKER_IMAGE_TAG AS builder
49

510
ARG BUILD_DATE
611
ARG BUILD_VERSION
712
ARG VCS_REF
813

9-
LABEL org.label-schema.schema-version="1.0"
10-
LABEL org.label-schema.build-date=$BUILD_DATE
11-
LABEL org.label-schema.name="fabiocicerchia/nginx-lua"
12-
LABEL org.label-schema.description="Nginx 1.19.0 with LUA support based on alpine 3.11.6."
13-
LABEL org.label-schema.url="https://github.com/fabiocicerchia/nginx-lua"
14-
LABEL org.label-schema.vcs-url="https://github.com/fabiocicerchia/nginx-lua"
15-
LABEL org.label-schema.vcs-ref=$VCS_REF
16-
LABEL org.label-schema.version=$BUILD_VERSION
17-
LABEL org.label-schema.docker.cmd="docker run -p 80:80 -d fabiocicerchia/nginx-lua:1.19.0-alpine3.11.6"
18-
1914
# https://github.com/openresty/luajit2
20-
ENV VER_LUAJIT 2.1-20200102
15+
ARG VER_LUAJIT=2.1-20200102
2116

2217
# https://github.com/openresty/lua-nginx-module
2318
# Production ready.
24-
ENV VER_LUA_NGINX_MODULE 0.10.15
19+
ARG VER_LUA_NGINX_MODULE=0.10.15
2520

2621
# https://github.com/openresty/lua-resty-core
2722
# This library is production ready.
28-
ENV VER_LUA_RESTY_CORE 0.1.17
29-
ENV LUA_LIB_DIR /usr/local/share/lua/5.1
23+
ARG VER_LUA_RESTY_CORE=0.1.17
24+
ARG LUA_LIB_DIR=/usr/local/share/lua/5.1
3025

3126
# https://github.com/openresty/lua-resty-lrucache
3227
# This library is considered production ready.
33-
ENV VER_LUA_RESTY_LRUCACHE 0.09
34-
35-
# https://github.com/nginx/nginx
36-
ENV VER_NGINX 1.19.0
28+
ARG VER_LUA_RESTY_LRUCACHE=0.09
3729

3830
# https://github.com/vision5/ngx_devel_kit
3931
# The NDK is now considered to be stable.
40-
ENV VER_NGX_DEVEL_KIT 0.3.1
41-
42-
# https://github.com/Yelp/dumb-init
43-
ENV VER_DUMBINIT 1.2.2
44-
45-
ENV LUAJIT_LIB /usr/local/lib
46-
ENV LUAJIT_INC /usr/local/include/luajit-2.1
47-
ENV LD_LIBRARY_PATH /usr/local/lib/:$LD_LIBRARY_PATH
32+
ARG VER_NGX_DEVEL_KIT=0.3.1
4833

49-
RUN set -x \
50-
&& apk update \
51-
&& apk add --no-cache \
52-
geoip-dev \
53-
openssl-dev \
54-
pcre-dev \
55-
zlib-dev \
56-
&& apk add --no-cache --virtual .build-deps \
57-
curl \
58-
g++ \
59-
gzip \
60-
make \
61-
tar \
62-
# OpenResty LUAJIT2
63-
# ##############################################################################
64-
&& curl -Lo /luajit.tar.gz https://github.com/openresty/luajit2/archive/v${VER_LUAJIT}.tar.gz \
65-
&& tar xvzf /luajit.tar.gz && rm /luajit.tar.gz \
66-
&& cd /luajit2-${VER_LUAJIT} \
67-
&& make -j "$(nproc)" \
68-
&& make install \
69-
&& cd / \
70-
# LUA Resty Core
71-
# ##############################################################################
72-
&& curl -Lo /lua-resty-core.tar.gz https://github.com/openresty/lua-resty-core/archive/v${VER_LUA_RESTY_CORE}.tar.gz \
73-
&& tar xvzf /lua-resty-core.tar.gz && rm /lua-resty-core.tar.gz \
74-
&& cd /lua-resty-core-${VER_LUA_RESTY_CORE} \
75-
&& make -j "$(nproc)" \
76-
&& make install \
77-
&& cd / \
78-
# LUA Resty LRUCache
79-
# ##############################################################################
80-
&& curl -Lo /lua-resty-lrucache.tar.gz https://github.com/openresty/lua-resty-lrucache/archive/v${VER_LUA_RESTY_LRUCACHE}.tar.gz \
81-
&& tar xvzf /lua-resty-lrucache.tar.gz && rm /lua-resty-lrucache.tar.gz \
82-
&& cd /lua-resty-lrucache-${VER_LUA_RESTY_LRUCACHE} \
83-
&& make -j "$(nproc)" \
84-
&& make install \
85-
&& cd / \
86-
# NGX Devel Kit
87-
# ##############################################################################
88-
&& curl -Lo /ngx_devel_kit.tar.gz https://github.com/vision5/ngx_devel_kit/archive/v${VER_NGX_DEVEL_KIT}.tar.gz \
89-
&& tar xvzf /ngx_devel_kit.tar.gz && rm /ngx_devel_kit.tar.gz \
90-
# Lua Nginx Module
91-
# ##############################################################################
92-
&& curl -Lo /lua-nginx.tar.gz https://github.com/openresty/lua-nginx-module/archive/v${VER_LUA_NGINX_MODULE}.tar.gz \
93-
&& tar xvzf /lua-nginx.tar.gz && rm /lua-nginx.tar.gz \
94-
# NGINX
95-
# ##############################################################################
96-
# create nginx user/group first, to be consistent throughout docker variants
97-
&& addgroup -g 32548 -S nginx \
98-
&& adduser -S -D -H -u 32548 -h /var/cache/nginx -s /sbin/nologin -G nginx -g nginx nginx \
99-
# we're on an architecture upstream doesn't officially build for
100-
# let's build binaries from the published packaging sources
101-
&& apk add --no-cache --virtual .nginx-build-deps \
102-
alpine-sdk \
103-
bash \
104-
findutils \
105-
gcc \
106-
gd-dev \
107-
geoip-dev \
108-
libc-dev \
109-
libedit-dev \
110-
libxslt-dev \
111-
linux-headers \
112-
make \
113-
mercurial \
114-
openssl-dev \
115-
pcre-dev \
116-
perl-dev \
117-
zlib-dev \
118-
&& curl -Lo /nginx.tar.gz https://nginx.org/download/nginx-${VER_NGINX}.tar.gz \
119-
&& tar xvzf /nginx.tar.gz && rm /nginx.tar.gz \
120-
&& cd /nginx-${VER_NGINX} \
121-
&& mkdir -p /var/cache/nginx/client_temp \
122-
/var/cache/nginx/proxy_temp \
123-
/var/cache/nginx/fastcgi_temp \
124-
/var/cache/nginx/uwsgi_temp \
125-
/var/cache/nginx/scgi_temp \
126-
&& ./configure \
34+
# https://github.com/nginx/nginx
35+
ARG VER_NGINX=1.19.0
36+
ARG NGINX_BUILD_CONFIG="\
12737
--prefix=/etc/nginx \
12838
--sbin-path=/usr/sbin/nginx \
12939
--modules-path=/usr/lib/nginx/modules \
@@ -133,19 +43,22 @@ RUN set -x \
13343
--pid-path=/var/run/nginx.pid \
13444
--lock-path=/var/run/nginx.lock \
13545
--http-client-body-temp-path=/var/cache/nginx/client_temp \
136-
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
13746
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
138-
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
47+
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
13948
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
49+
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
14050
--user=nginx \
14151
--group=nginx \
52+
--add-module=/lua-nginx-module-${VER_LUA_NGINX_MODULE} \
53+
--add-module=/ngx_devel_kit-${VER_NGX_DEVEL_KIT} \
14254
--with-compat \
14355
--with-file-aio \
144-
--with-threads \
14556
--with-http_addition_module \
14657
--with-http_auth_request_module \
14758
--with-http_dav_module \
59+
--with-http_dav_module \
14860
--with-http_flv_module \
61+
--with-http_geoip_module \
14962
--with-http_gunzip_module \
15063
--with-http_gzip_static_module \
15164
--with-http_mp4_module \
@@ -163,34 +76,155 @@ RUN set -x \
16376
--with-stream_realip_module \
16477
--with-stream_ssl_module \
16578
--with-stream_ssl_preread_module \
166-
--with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' \
167-
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' \
168-
--add-module=/lua-nginx-module-${VER_LUA_NGINX_MODULE} \
169-
--add-module=/ngx_devel_kit-${VER_NGX_DEVEL_KIT} \
170-
--with-http_dav_module \
171-
--with-http_geoip_module \
172-
&& make -j "$(nproc)" build \
173-
&& make install \
79+
--with-threads \
80+
"
81+
82+
ARG LUAJIT_LIB=/usr/local/lib
83+
ARG LUAJIT_INC=/usr/local/include/luajit-2.1
84+
ARG LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
85+
86+
ARG BUILD_DEPS="\
87+
curl \
88+
g++ \
89+
geoip-dev \
90+
gzip \
91+
make \
92+
openssl-dev \
93+
pcre-dev \
94+
tar \
95+
zlib-dev"
96+
97+
ARG NGINX_BUILD_DEPS="\
98+
alpine-sdk \
99+
bash \
100+
findutils \
101+
gcc \
102+
gd-dev \
103+
geoip-dev \
104+
libc-dev \
105+
libedit-dev \
106+
libxslt-dev \
107+
linux-headers \
108+
make \
109+
mercurial \
110+
openssl-dev \
111+
pcre-dev \
112+
perl-dev \
113+
zlib-dev"
114+
115+
RUN set -eux \
116+
&& apk update \
117+
&& apk add --no-cache \
118+
$BUILD_DEPS \
119+
$NGINX_BUILD_DEPS
120+
121+
RUN set -eux \
122+
# OpenResty LUAJIT2
123+
# ##############################################################################
124+
&& curl -Lo /luajit.tar.gz https://github.com/openresty/luajit2/archive/v${VER_LUAJIT}.tar.gz \
125+
&& tar -C / -xvzf /luajit.tar.gz && rm /luajit.tar.gz \
126+
&& cd /luajit2-${VER_LUAJIT} \
127+
&& make -j "$(nproc)" \
128+
&& make install \
129+
\
130+
# LUA Resty Core
131+
# ##############################################################################
132+
&& curl -Lo /lua-resty-core.tar.gz https://github.com/openresty/lua-resty-core/archive/v${VER_LUA_RESTY_CORE}.tar.gz \
133+
&& tar -C / -xvzf /lua-resty-core.tar.gz && rm /lua-resty-core.tar.gz \
134+
&& cd /lua-resty-core-${VER_LUA_RESTY_CORE} \
135+
&& make -j "$(nproc)" \
136+
&& make install \
137+
\
138+
# LUA Resty LRUCache
139+
# ##############################################################################
140+
&& curl -Lo /lua-resty-lrucache.tar.gz https://github.com/openresty/lua-resty-lrucache/archive/v${VER_LUA_RESTY_LRUCACHE}.tar.gz \
141+
&& tar -C / -xvzf /lua-resty-lrucache.tar.gz && rm /lua-resty-lrucache.tar.gz \
142+
&& cd /lua-resty-lrucache-${VER_LUA_RESTY_LRUCACHE} \
143+
&& make -j "$(nproc)" \
144+
&& make install \
145+
\
146+
# NGX Devel Kit
147+
# ##############################################################################
148+
&& curl -Lo /ngx_devel_kit.tar.gz https://github.com/vision5/ngx_devel_kit/archive/v${VER_NGX_DEVEL_KIT}.tar.gz \
149+
&& tar -C / -xvzf /ngx_devel_kit.tar.gz && rm /ngx_devel_kit.tar.gz \
150+
\
151+
# Lua Nginx Module
152+
# ##############################################################################
153+
&& curl -Lo /lua-nginx.tar.gz https://github.com/openresty/lua-nginx-module/archive/v${VER_LUA_NGINX_MODULE}.tar.gz \
154+
&& tar -C / -xvzf /lua-nginx.tar.gz && rm /lua-nginx.tar.gz
155+
156+
RUN set -eux \
157+
# NGINX
158+
# ##############################################################################
159+
# we're on an architecture upstream doesn't officially build for
160+
# let's build binaries from the published packaging sources
161+
&& curl -Lo /nginx.tar.gz https://nginx.org/download/nginx-${VER_NGINX}.tar.gz \
162+
&& tar -C / -xvzf /nginx.tar.gz && rm /nginx.tar.gz \
163+
&& cd /nginx-${VER_NGINX} \
164+
&& mkdir -p /var/cache/nginx/client_temp \
165+
/var/cache/nginx/proxy_temp \
166+
/var/cache/nginx/fastcgi_temp \
167+
/var/cache/nginx/uwsgi_temp \
168+
/var/cache/nginx/scgi_temp \
169+
&& ./configure \
170+
${NGINX_BUILD_CONFIG} \
171+
--with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' \
172+
--with-ld-opt='-Wl,-rpath,/usr/local/lib -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' \
173+
&& make -j "$(nproc)" build \
174+
&& make install
175+
176+
##########################################
177+
# Combine everything with minimal layers #
178+
##########################################
179+
FROM $DOCKER_IMAGE_OS:$DOCKER_IMAGE_TAG
180+
181+
# http://label-schema.org/rc1/
182+
LABEL maintainer="Fabio Cicerchia <[email protected]>" \
183+
org.label-schema.build-date=$BUILD_DATE \
184+
org.label-schema.description="Nginx $VER_NGINX with LUA support based on $DOCKER_IMAGE_OS $DOCKER_IMAGE_TAG." \
185+
org.label-schema.docker.cmd="docker run -p 80:80 -d $DOCKER_IMAGE:$VER_NGINX-$DOCKER_IMAGE_OS$DOCKER_IMAGE_TAG" \
186+
org.label-schema.name="$DOCKER_IMAGE" \
187+
org.label-schema.schema-version="1.0" \
188+
org.label-schema.url="https://github.com/$DOCKER_IMAGE" \
189+
org.label-schema.vcs-ref=$VCS_REF \
190+
org.label-schema.vcs-url="https://github.com/$DOCKER_IMAGE" \
191+
org.label-schema.version="$VER_NGINX-$DOCKER_IMAGE_OS$DOCKER_IMAGE_TAG"
192+
193+
# https://github.com/Yelp/dumb-init
194+
ARG VER_DUMBINIT=1.2.2
195+
196+
ARG PKG_DEPS="\
197+
geoip-dev \
198+
openssl-dev \
199+
pcre-dev \
200+
zlib-dev"
201+
202+
COPY --from=builder /etc/nginx /etc/nginx
203+
COPY --from=builder /usr/local/lib /usr/local/lib
204+
COPY --from=builder /usr/local/share/lua /usr/local/share/lua
205+
COPY --from=builder /usr/sbin/nginx /usr/sbin/nginx
206+
COPY --from=builder /var/cache/nginx /var/cache/nginx
207+
208+
RUN set -eux \
209+
&& apk update \
210+
&& apk add --no-cache \
211+
$PKG_DEPS \
174212
# Bring in tzdata so users could set the timezones through the environment
175213
# variables
176-
&& apk add --no-cache tzdata \
214+
&& apk add --no-cache tzdata \
177215
# Bring in curl and ca-certificates to make registering on DNS SD easier
178-
&& apk add --no-cache curl ca-certificates \
216+
&& apk add --no-cache curl ca-certificates \
179217
# forward request and error logs to docker log collector
180-
&& ln -sf /dev/stdout /var/log/nginx/access.log \
181-
&& ln -sf /dev/stderr /var/log/nginx/error.log \
218+
&& mkdir -p /var/log/nginx \
219+
&& ln -sf /dev/stdout /var/log/nginx/access.log \
220+
&& ln -sf /dev/stderr /var/log/nginx/error.log \
182221
# dumb-init
183222
# ##############################################################################
184-
&& apk add --no-cache dumb-init \
185-
# Cleanup
186-
# ##############################################################################
187-
&& rm -rf /lua-nginx-module-${VER_LUA_NGINX_MODULE} \
188-
&& rm -rf /lua-resty-core-${VER_LUA_RESTY_CORE} \
189-
&& rm -rf /lua-resty-lrucache-${VER_LUA_RESTY_LRUCACHE} \
190-
&& rm -rf /luajit2-${VER_LUAJIT} \
191-
&& rm -rf /nginx-${VER_NGINX} \
192-
&& rm -rf /ngx_devel_kit-${VER_NGX_DEVEL_KIT} \
193-
&& apk del .build-deps .nginx-build-deps
223+
&& curl -Lo /usr/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v${VER_DUMBINIT}/dumb-init_${VER_DUMBINIT}_x86_64 \
224+
&& chmod +x /usr/bin/dumb-init \
225+
# create nginx user/group first, to be consistent throughout docker variants
226+
&& addgroup -g 32548 -S nginx \
227+
&& adduser -S -D -H -u 32548 -h /var/cache/nginx -s /sbin/nologin -G nginx -g nginx nginx
194228

195229
HEALTHCHECK --interval=30s --timeout=3s CMD curl --fail http://localhost/ || exit 1
196230

0 commit comments

Comments
 (0)