Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: build apisix-openresty base on alpine #97

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ image_tag="7"
iteration=0
local_code_path=0
openresty="openresty"
openresty_version="1.19.3.2.0"
artifact="0"
apisix_repo="https://github.com/apache/apisix"
dashboard_repo="https://github.com/apache/apisix-dashboard"
Expand Down Expand Up @@ -67,6 +68,34 @@ define build
endef
endif

### function for building image
### $(1) is name
### $(2) is dockerfile filename
### $(3) is package type
### $(4) is openresty image name
### $(5) is openresty image version
### $(6) is code path
ifneq ($(buildx), True)
define build-image
docker build -t apache/$(1)-$(3):$(version) \
--build-arg OPENRESTY_NAME=$(4) \
--build-arg OPENRESTY_VERSION=$(5) \
--build-arg CODE_PATH=$(6) \
-f ./dockerfiles/Dockerfile.$(2).$(3) .
endef
else
define build-image
docker buildx build -t apache/$(1)-$(3):$(version) \
--build-arg OPENRESTY_NAME=$(4) \
--build-arg OPENRESTY_VERSION=$(5) \
--build-arg CODE_PATH=$(6) \
--load \
--cache-from=$(cache_from) \
--cache-to=$(cache_to) \
-f ./dockerfiles/Dockerfile.$(2).$(3) .
endef
endif

### function for packing
### $(1) is name
### $(2) is package type
Expand Down Expand Up @@ -115,6 +144,14 @@ package-apisix-rpm:
package-apisix-deb:
$(call package,apisix,deb)


### build apisix image base alpine:
.PHONY: build-apisix-alpine-image
build-apisix-alpine-image:
$(call build,apisix-openresty,apisix-openresty,apk,$(local_code_path))
$(call build-image,apisix,apisix,alpine,apisix-openresty,$(openresty_version),$(local_code_path))


### build dashboard:
.PHONY: build-dashboard-rpm
build-dashboard-rpm:
Expand Down Expand Up @@ -155,6 +192,10 @@ build-apisix-openresty-rpm:
build-apisix-openresty-deb:
$(call build,apisix-openresty,apisix-openresty,deb,$(local_code_path))

.PHONY: build-apisix-openresty-apk
build-apisix-openresty-apk:
$(call build,apisix-openresty,apisix-openresty,apk,$(local_code_path))

### build rpm for apisix-openresty:
.PHONY: package-apisix-openresty-rpm
package-apisix-openresty-rpm:
Expand Down Expand Up @@ -221,4 +262,9 @@ package: build-fpm
package: build-dashboard-deb
package: package-dashboard-deb

else ifeq ($(app)_$(type),apisix_)
package: build-fpm
package: build-dashboard-deb
package: package-dashboard-deb

endif
130 changes: 130 additions & 0 deletions dockerfiles/Dockerfile.apisix-openresty.apk
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
ARG IMAGE_BASE="alpine"
ARG IMAGE_TAG="3.12"

FROM ${IMAGE_BASE}:${IMAGE_TAG}

COPY ./utils/build-common.sh /tmp/build-common.sh
COPY build-apisix-openresty.sh /tmp/build-apisix-openresty.sh
COPY ./utils/determine-dist.sh /tmp/determine-dist.sh

ARG RESTY_VERSION="1.19.3.1"
ARG RESTY_OPENSSL_VERSION="1.1.1g"
ARG RESTY_OPENSSL_PATCH_VERSION="1.1.1f"
ARG RESTY_OPENSSL_URL_BASE="https://www.openssl.org/source"
ARG RESTY_PCRE_VERSION="8.44"
ARG RESTY_J="1"
ARG RESTY_CONFIG_OPTIONS="\
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove unused ARG/labels

--with-compat \
--with-file-aio \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_geoip_module=dynamic \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_image_filter_module=dynamic \
--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_xslt_module=dynamic \
--with-ipv6 \
--with-mail \
--with-mail_ssl_module \
--with-md5-asm \
--with-pcre-jit \
--with-sha1-asm \
--with-stream \
--with-stream_ssl_module \
--with-threads \
"
ARG RESTY_CONFIG_OPTIONS_MORE=""
ARG RESTY_LUAJIT_OPTIONS="--with-luajit-xcflags='-DLUAJIT_NUMMODE=2 -DLUAJIT_ENABLE_LUA52COMPAT'"

ARG RESTY_EVAL_PRE_CONFIGURE=""
ARG RESTY_EVAL_POST_MAKE=""

# These are not intended to be user-specified
ARG _RESTY_CONFIG_DEPS="--with-pcre \
--with-cc-opt='-DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl111/include' \
--with-ld-opt='-L/usr/local/openresty/pcre/lib -L/usr/local/openresty/openssl111/lib -Wl,-rpath,/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl111/lib' \
"

LABEL resty_image_base="${RESTY_IMAGE_BASE}"
LABEL resty_image_tag="${IMAGE_TAG}"
LABEL resty_version="${RESTY_VERSION}"
LABEL resty_openssl_version="${RESTY_OPENSSL_VERSION}"
LABEL resty_openssl_patch_version="${RESTY_OPENSSL_PATCH_VERSION}"
LABEL resty_openssl_url_base="${RESTY_OPENSSL_URL_BASE}"
LABEL resty_pcre_version="${RESTY_PCRE_VERSION}"
LABEL resty_config_options="${RESTY_CONFIG_OPTIONS}"
LABEL resty_config_options_more="${RESTY_CONFIG_OPTIONS_MORE}"
LABEL resty_config_deps="${_RESTY_CONFIG_DEPS}"
LABEL resty_eval_pre_configure="${RESTY_EVAL_PRE_CONFIGURE}"
LABEL resty_eval_post_make="${RESTY_EVAL_POST_MAKE}"


RUN apk add --no-cache --virtual .build-deps \
build-base \
coreutils \
curl \
gd-dev \
geoip-dev \
libxslt-dev \
linux-headers \
make \
perl-dev \
readline-dev \
zlib-dev \
&& apk add --no-cache \
gd \
geoip \
libgcc \
libxslt \
zlib \
bash \
git \
sudo \
curl

RUN cd /tmp \
&& if [ -n "${RESTY_EVAL_PRE_CONFIGURE}" ]; then eval $(echo ${RESTY_EVAL_PRE_CONFIGURE}); fi \
&& cd /tmp \
&& curl -fSL "${RESTY_OPENSSL_URL_BASE}/openssl-${RESTY_OPENSSL_VERSION}.tar.gz" -o openssl-${RESTY_OPENSSL_VERSION}.tar.gz \
&& tar xzf openssl-${RESTY_OPENSSL_VERSION}.tar.gz \
&& cd openssl-${RESTY_OPENSSL_VERSION} \
&& echo 'patching OpenSSL 1.1.1 for OpenResty' \
&& curl -s https://raw.githubusercontent.com/openresty/openresty/master/patches/openssl-${RESTY_OPENSSL_PATCH_VERSION}-sess_set_get_cb_yield.patch | patch -p1 \
&& ./config \
no-threads shared zlib -g \
enable-ssl3 enable-ssl3-method \
--prefix=/usr/local/openresty/openssl111 \
--libdir=lib \
-Wl,-rpath,/usr/local/openresty/openssl111/lib \
&& make -j${RESTY_J} \
&& make -j${RESTY_J} install_sw

RUN cd /tmp \
&& curl -fSL https://ftp.pcre.org/pub/pcre/pcre-${RESTY_PCRE_VERSION}.tar.gz -o pcre-${RESTY_PCRE_VERSION}.tar.gz \
&& tar xzf pcre-${RESTY_PCRE_VERSION}.tar.gz \
&& cd /tmp/pcre-${RESTY_PCRE_VERSION} \
&& ./configure \
--prefix=/usr/local/openresty/pcre \
--disable-cpp \
--enable-jit \
--enable-utf \
--enable-unicode-properties \
&& make -j${RESTY_J} \
&& make -j${RESTY_J} install

RUN cd /tmp \
&& curl --version \
&& ./build-common.sh build_apisix_openresty_apk

CMD /bin/sh
55 changes: 55 additions & 0 deletions dockerfiles/Dockerfile.apisix.alpine
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
ARG ENABLE_PROXY=false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file can be removed?

ARG OPENRESTY_VERSION
ARG OPENRESTY_NAME

FROM apache/${OPENRESTY_NAME}:${OPENRESTY_VERSION} AS production-stage

ARG APISIX_VERSION=2.9
LABEL apisix_version="${APISIX_VERSION}"

ARG ENABLE_PROXY
RUN set -x \
&& (test "${ENABLE_PROXY}" != "true" || /bin/sed -i 's,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g' /etc/apk/repositories) \
&& apk add --no-cache --virtual .builddeps \
automake \
autoconf \
libtool \
pkgconfig \
cmake \
git \
pcre \
pcre-dev \
&& mkdir ~/.luarocks \
&& luarocks config variables.OPENSSL_LIBDIR /usr/local/openresty/openssl/lib \
&& luarocks config variables.OPENSSL_INCDIR /usr/local/openresty/openssl/include \
&& luarocks install https://github.com/apache/apisix/raw/master/rockspec/apisix-${APISIX_VERSION}-0.rockspec --tree=/usr/local/apisix/deps \
&& cp -v /usr/local/apisix/deps/lib/luarocks/rocks-5.1/apisix/${APISIX_VERSION}-0/bin/apisix /usr/bin/ \
&& (if [ "$APISIX_VERSION" = "master" ] || [ "$APISIX_VERSION" \> "2.2" ]; then echo 'use shell ';else bin='#! /usr/local/openresty/luajit/bin/luajit\npackage.path = "/usr/local/apisix/?.lua;" .. package.path'; sed -i "1s@.*@$bin@" /usr/bin/apisix ; fi;) \
&& mv /usr/local/apisix/deps/share/lua/5.1/apisix /usr/local/apisix \
&& apk del .builddeps build-base make unzip

FROM alpine:3.13 AS last-stage

ARG ENABLE_PROXY
# add runtime for Apache APISIX
RUN set -x \
&& (test "${ENABLE_PROXY}" != "true" || /bin/sed -i 's,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g' /etc/apk/repositories) \
&& apk add --no-cache bash libstdc++ curl tzdata

WORKDIR /usr/local/apisix

COPY --from=production-stage /usr/local/openresty/ /usr/local/openresty/
COPY --from=production-stage /usr/local/apisix/ /usr/local/apisix/
COPY --from=production-stage /usr/bin/apisix /usr/bin/apisix

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /usr/local/apisix/logs/access.log \
&& ln -sf /dev/stderr /usr/local/apisix/logs/error.log

ENV PATH=$PATH:/usr/local/openresty/luajit/bin:/usr/local/openresty/nginx/sbin:/usr/local/openresty/bin

EXPOSE 9080 9443

CMD ["sh", "-c", "/usr/bin/apisix init && /usr/bin/apisix init_etcd && /usr/local/openresty/bin/openresty -p /usr/local/apisix -g 'daemon off;'"]

STOPSIGNAL SIGQUIT
9 changes: 9 additions & 0 deletions utils/build-common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ build_apisix_openresty_deb() {
./build-apisix-openresty.sh
}

build_apisix_openresty_apk() {
export_openresty_variables
./build-apisix-openresty.sh
}

export_openresty_variables() {
export openssl_prefix=/usr/local/openresty/openssl111
export zlib_prefix=/usr/local/openresty/zlib
export pcre_prefix=/usr/local/openresty/pcre
export OR_PREFIX=/usr/local/openresty

export cc_opt="-DNGX_LUA_ABORT_AT_PANIC -I${zlib_prefix}/include -I${pcre_prefix}/include -I${openssl_prefix}/include"
export ld_opt="-L${zlib_prefix}/lib -L${pcre_prefix}/lib -L${openssl_prefix}/lib -Wl,-rpath,${zlib_prefix}/lib:${pcre_prefix}/lib:${openssl_prefix}/lib"
Expand All @@ -42,4 +48,7 @@ build_apisix_openresty_rpm)
build_apisix_openresty_deb)
build_apisix_openresty_deb
;;
build_apisix_openresty_apk)
build_apisix_openresty_apk
;;
esac