197 lines
6.5 KiB
Docker
197 lines
6.5 KiB
Docker
# Ver: 1.9 by Endial Fang (endial@126.com)
|
||
#
|
||
|
||
# 默认变量 ========================================================================
|
||
# 该部分变量为系统根据编译命令默认设置
|
||
|
||
# `TARGETPLATFORM`:构建后的目标平台信息。如 `linux/amd64`,`linux/arm/v7`,`windows/amd64`
|
||
# `TARGETOS`:目标平台信息(TARGETPLATFORM)中的操作系统部分,如:`linux`、`windows`
|
||
# `TARGETARCH`:目标平台信息(TARGETPLATFORM)中的平台架构部分,如:`amd64`、`arm`
|
||
# `TARGETVARIANT`:目标平台信息(TARGETPLATFORM)中的版本变体部分,如:`v7`
|
||
# `BUILDPLATFORM`:用于构建的节点平台信息
|
||
# `BUILDOS`:用于构建的节点平台信息(BUILDPLATFORM)中的操作系统部分
|
||
# `BUILDARCH`用于构建的节点平台信息(BUILDPLATFORM)中的平台架构部分
|
||
# `BUILDVARIANT`用于构建的节点平台信息(BUILDPLATFORM)中的版本变体部分
|
||
|
||
# 可变参数 ========================================================================
|
||
# 该部分变量,在编译命令中通过 `--build-arg` 传入;如果未设置,则使用下面对应的默认值
|
||
|
||
# 设置当前应用名称及版本
|
||
ARG APP_NAME=nginx
|
||
ARG APP_VER=1.24.0
|
||
|
||
# 设置默认仓库地址,默认为本地仓库;定义时需要包含末尾的`/`
|
||
ARG REGISTRY_URL="docker.colovu.com/"
|
||
|
||
# 设置 apt-get 源:default / ustc / aliyun
|
||
ARG APT_SOURCE=aliyun
|
||
|
||
# 编译镜像时指定用于加速的本地软件包存储服务器地址
|
||
ARG LOCAL_URL=""
|
||
|
||
# 0. 预处理 ======================================================================
|
||
FROM --platform=${TARGETPLATFORM:-linux/amd64} ${REGISTRY_URL}colovu/dbuilder:11 as builder
|
||
|
||
# 声明需要使用的全局可变参数
|
||
ARG APP_NAME
|
||
ARG APP_VER
|
||
ARG REGISTRY_URL
|
||
ARG APT_SOURCE
|
||
ARG LOCAL_URL
|
||
|
||
# 选择软件包源(Optional),以加速后续软件包安装
|
||
RUN select_source ${APT_SOURCE};
|
||
|
||
# 安装依赖的软件包及库(Optional)
|
||
RUN install_pkg libpcre3 libpcre3-dev \
|
||
zlib1g zlib1g-dev \
|
||
libxslt1.1 libxslt1-dev \
|
||
libgd3 libgd-dev \
|
||
libxml2 libxml2-dev \
|
||
geoip-bin geoip-database libgeoip-dev
|
||
|
||
# 设置工作目录
|
||
WORKDIR /tmp
|
||
|
||
# 下载并解压软件包 nginx
|
||
RUN set -eux; \
|
||
appName="${APP_NAME}-${APP_VER}.tar.gz"; \
|
||
[ ! -z ${LOCAL_URL} ] && localURL=${LOCAL_URL}/nginx; \
|
||
appUrls="${localURL:-} \
|
||
http://nginx.org/download \
|
||
"; \
|
||
download_pkg unpack ${appName} "${appUrls}";
|
||
|
||
# 源码编译: 编译后将配置文件模板拷贝至 /usr/local/${app_name}/share/${app_name} 中
|
||
RUN set -eux; \
|
||
APP_ARCH=`arch` \
|
||
APP_SRC="/tmp/${APP_NAME}-${APP_VER}"; \
|
||
cd ${APP_SRC}; \
|
||
LDFLAGS="-L/usr/local/lib -L/usr/lib/${APP_ARCH}-linux-gnu" ./configure \
|
||
--prefix=/usr/local/nginx \
|
||
--sbin-path=/usr/local/nginx/sbin/nginx \
|
||
--conf-path=/etc/nginx/nginx.conf \
|
||
--pid-path=/var/run/nginx/nginx.pid \
|
||
--lock-path=/var/run/nginx/nginx.lock \
|
||
--http-log-path=/var/log/nginx/access.log \
|
||
--error-log-path=/var/log/nginx/error.log \
|
||
--modules-path=/usr/local/nginx/modules \
|
||
--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-pcre \
|
||
--with-pcre-jit \
|
||
--with-http_ssl_module \
|
||
--with-http_v2_module \
|
||
--with-http_realip_module \
|
||
--with-http_xslt_module \
|
||
--with-http_image_filter_module \
|
||
--with-http_geoip_module \
|
||
--with-http_sub_module \
|
||
--with-http_dav_module \
|
||
--with-http_mp4_module \
|
||
--with-http_gunzip_module \
|
||
--with-http_auth_request_module \
|
||
--with-http_slice_module \
|
||
\
|
||
--with-stream \
|
||
--with-stream_geoip_module \
|
||
--with-stream_realip_module \
|
||
--with-stream_ssl_module \
|
||
--with-threads \
|
||
--with-poll_module \
|
||
--with-mail \
|
||
; \
|
||
make -j "$(nproc)" && make install; \
|
||
strip /usr/local/nginx/sbin/nginx;
|
||
|
||
# 生成默认 PHP 首页文件
|
||
RUN set -eux; \
|
||
echo "<?php" >/usr/local/nginx/html/index.php; \
|
||
echo "phpinfo();" >>/usr/local/nginx/html/index.php; \
|
||
echo "?>" >>/usr/local/nginx/html/index.php;
|
||
|
||
# 检测并生成依赖文件记录
|
||
RUN set -eux; \
|
||
find /usr/local/${APP_NAME} -type f -executable -exec ldd '{}' ';' | \
|
||
awk '/=>/ { print $(NF-1) }' | \
|
||
sort -u | \
|
||
xargs -r dpkg-query --search 2>/dev/null | \
|
||
cut -d: -f1 | \
|
||
sort -u >/usr/local/${APP_NAME}/runDeps;
|
||
|
||
# 1. 生成镜像 =====================================================================
|
||
FROM --platform=${TARGETPLATFORM:-linux/amd64} ${REGISTRY_URL}colovu/debian:11
|
||
|
||
# 声明需要使用的全局可变参数
|
||
ARG APP_NAME
|
||
ARG APP_VER
|
||
ARG REGISTRY_URL
|
||
ARG APT_SOURCE
|
||
ARG LOCAL_URL
|
||
|
||
# 镜像所包含应用的基础信息,定义环境变量,供后续脚本使用
|
||
ENV APP_NAME=${APP_NAME} \
|
||
APP_VER=${APP_VER} \
|
||
APP_EXEC=${APP_NAME}
|
||
|
||
ENV APP_HOME_DIR=/usr/local/${APP_NAME} \
|
||
APP_DEF_DIR=/etc/${APP_NAME}
|
||
|
||
ENV PATH="${APP_HOME_DIR}/sbin:${APP_HOME_DIR}/bin:${PATH}" \
|
||
LD_LIBRARY_PATH="${APP_HOME_DIR}/lib"
|
||
|
||
LABEL \
|
||
"Version"="v${APP_VER}" \
|
||
"Description"="Docker image for ${APP_NAME}." \
|
||
"Github"="https://github.com/colovu/docker-${APP_NAME}" \
|
||
"Vendor"="Endial Fang (endial@126.com)"
|
||
|
||
# 从预处理过程中拷贝软件包(Optional),可以使用阶段编号或阶段命名定义来源
|
||
COPY --from=0 /usr/local/${APP_NAME} /usr/local/${APP_NAME}
|
||
|
||
# 拷贝应用使用的客制化脚本,并创建对应的用户及数据存储目录
|
||
COPY customer /
|
||
RUN set -eux; \
|
||
prepare_env; \
|
||
/bin/bash -c "ln -sf /usr/local/${APP_NAME}/etc/${APP_NAME} /etc/";
|
||
|
||
# 选择软件包源(Optional),以加速后续软件包安装
|
||
RUN select_source ${APT_SOURCE}
|
||
|
||
# 安装依赖的软件包及库(Optional)
|
||
RUN install_pkg `cat /usr/local/${APP_NAME}/runDeps`;
|
||
|
||
# 执行预处理脚本,并验证安装的软件包
|
||
RUN set -eux; \
|
||
override_file="/usr/local/overrides/overrides-${APP_VER}.sh"; \
|
||
[ -e "${override_file}" ] && /bin/bash "${override_file}"; \
|
||
${APP_EXEC} -V ;
|
||
|
||
# 默认提供的数据卷
|
||
VOLUME ["/srv/conf", "/srv/data", "/srv/cert", "/var/log"]
|
||
|
||
# 默认non-root用户启动,必须保证端口在1024之上
|
||
EXPOSE 8080 8443
|
||
|
||
# 应用健康状态检查
|
||
#HEALTHCHECK --interval=30s --timeout=30s --retries=3 \
|
||
# CMD curl -fs http://localhost:8080/ || exit 1
|
||
HEALTHCHECK --interval=10s --timeout=10s --retries=3 \
|
||
CMD netstat -ltun | grep 8080
|
||
|
||
# 使用 non-root 用户运行后续的命令
|
||
USER 1001
|
||
|
||
# 设置工作目录
|
||
WORKDIR /srv/conf
|
||
|
||
# 容器初始化命令
|
||
ENTRYPOINT ["/usr/local/bin/entry.sh"]
|
||
|
||
# 应用程序的启动命令,必须使用非守护进程方式运行
|
||
CMD ["/usr/local/bin/run.sh"]
|