- 主镜像 Dockerfile:多阶段构建,复用 rust-nginx-base 镜像(nginx 1.30.4 + rust 1.89 + 已 configure 的 pcre/openssl/编译链),冷构建从 ~17min 压到 ~5min - deploy/nginx.conf:容器内 nginx 配置(端口 18098 / /usr/local/nginx/... 路径) - docker-compose.yml:本机一键起服务(端口 8881→18098)+ S10 健康探测 - scripts/container-entrypoint.sh:容器入口脚本(启动前 healthcheck 等) - .dockerignore:避免把 build/target/data/refs 误打进镜像 - deploy/Dockerfile.base:rust-nginx-base 镜像定义(apt 链 + nginx/pcre 源码 + 已 ./configure 过的源码树) - deploy/build-base-image.sh:构建并推送 base 镜像到阿里云 CR (crpi-yscq976uwza88qap.cn-shenzhen.personal.cr.aliyuncs.com/ssw188/nginx)
110 lines
5.0 KiB
Docker
110 lines
5.0 KiB
Docker
# ============================================================================
|
||
# rust-nginx-base —— iPass / SSO / 任何 ngx-rust 项目共用的编译基础镜像
|
||
#
|
||
# 包含:
|
||
# - rust:1.89-slim-bookworm(rust 工具链)
|
||
# - nginx 编译链:build-essential / libpcre2-dev / zlib1g-dev / libssl-dev
|
||
# - bindgen 依赖:clang / libclang-dev / pkg-config
|
||
# - 已下载 + ./configure 过的 nginx 1.30.4 源码树(/src/nginx-1.30.4/)
|
||
# objs/ 含 bindgen 所需头文件
|
||
# - 已 configure 过的 pcre 8.45 源码树(/src/pcre-8.45/)
|
||
#
|
||
# 设计:
|
||
# - tag 版本号:1.30.4-rust-1.89(nginx 与 rust 任一升级都推新 tag)
|
||
# - 推到阿里云 CR:crpi-yscq976uwza88qap.cn-shenzhen.personal.cr.aliyuncs.com/ssw188/nginx
|
||
# - 复用方式:FROM crpi-.../nginx:1.30.4-rust-1.89 AS builder → 直接 cargo build
|
||
#
|
||
# 构建命令:
|
||
# docker build \
|
||
# --build-context ngxbuild=../ipass/build/ngxbuild \
|
||
# -f deploy/Dockerfile.base \
|
||
# -t crpi-yscq976uwza88qap.cn-shenzhen.personal.cr.aliyuncs.com/ssw188/nginx:1.30.4-rust-1.89 \
|
||
# .
|
||
#
|
||
# ============================================================================
|
||
|
||
# syntax=docker/dockerfile:1.7
|
||
|
||
ARG NGINX_VERSION=1.30.4
|
||
ARG PCRE_VERSION=8.45
|
||
ARG RUST_VERSION=1.89
|
||
|
||
FROM rust:${RUST_VERSION}-slim-bookworm
|
||
|
||
ARG NGINX_VERSION
|
||
ARG PCRE_VERSION
|
||
|
||
# nginx 编译链 + bindgen 依赖 + 下载工具(兜底用)
|
||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||
build-essential \
|
||
libpcre2-dev \
|
||
zlib1g-dev \
|
||
libssl-dev \
|
||
clang \
|
||
libclang-dev \
|
||
pkg-config \
|
||
curl \
|
||
ca-certificates \
|
||
xz-utils \
|
||
git \
|
||
&& rm -rf /var/lib/apt/lists/*
|
||
|
||
# --- nginx + pcre 源码:从 ngxbuild 上下文拷入 ---
|
||
# 约定:ngxbuild 上下文必须含 nginx-${NGINX_VERSION}.tar.gz 和 pcre-${PCRE_VERSION}.tar.gz
|
||
# 升级 nginx 版本时(NGINX_VERSION=1.30.5 等):
|
||
# 先 curl 到 ngxbuild 目录:
|
||
# curl -fsSL https://nginx.org/download/nginx-1.30.5.tar.gz -o ../ipass/build/ngxbuild/nginx-1.30.5.tar.gz
|
||
# curl -fsSL https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz/download -o ../ipass/build/ngxbuild/pcre-8.45.tar.gz
|
||
WORKDIR /src
|
||
COPY --from=ngxbuild nginx-${NGINX_VERSION}.tar.gz /tmp/nginx-${NGINX_VERSION}.tar.gz
|
||
COPY --from=ngxbuild pcre-${PCRE_VERSION}.tar.gz /tmp/pcre-${PCRE_VERSION}.tar.gz
|
||
|
||
RUN tar -xzf /tmp/nginx-${NGINX_VERSION}.tar.gz -C /src/ \
|
||
&& tar -xzf /tmp/pcre-${PCRE_VERSION}.tar.gz -C /src/ \
|
||
&& rm -f /tmp/nginx-${NGINX_VERSION}.tar.gz /tmp/pcre-${PCRE_VERSION}.tar.gz
|
||
|
||
# --- nginx + pcre:configure + 触发 pcre 子构建(生成 pcre.h),但不编 nginx 二进制 ----
|
||
# 为什么必须 make 一次:
|
||
# 1. ./configure 生成 objs/Makefile + ngx_auto_config.h(bindgen 头文件)
|
||
# 2. nginx 的 objs/Makefile 依赖 pcre.h(pcre-8.45/pcre.h)—— **不跑 make 就没 pcre.h**
|
||
# 3. cargo nginx-sys build.rs 直接读 pcre.h + ngx_*.h,缺一个就 fatal error
|
||
# 4. nginx make 的第一步是触发 pcre 子目录的 configure,生成 pcre.h
|
||
# 5. 但 nginx make 后续会真编 nginx 二进制(耗时)。这里只跑 make 的 pcre 子步骤:
|
||
# `make objs/Makefile` 触发 pcre 子 configure,但不全量编译
|
||
# 等等——实测 nginx 1.30.4 的 objs/Makefile 不会因单纯 touch objs/Makefile 触发 pcre 子构建。
|
||
# 必须实际跑 `make`(包括 nginx 二进制)。代价:base 多 30s,但下游 make 是纯增量(秒过)。
|
||
WORKDIR /src/nginx-${NGINX_VERSION}
|
||
RUN ./configure \
|
||
--prefix=/usr/local/nginx \
|
||
--with-pcre=/src/pcre-${PCRE_VERSION} \
|
||
--with-http_realip_module \
|
||
--with-http_ssl_module \
|
||
--without-http_ssi_module \
|
||
--without-http_userid_module \
|
||
--without-http_autoindex_module \
|
||
--without-http_geo_module \
|
||
--without-http_split_clients_module \
|
||
--without-http_referer_module \
|
||
--without-http_uwsgi_module \
|
||
--without-http_scgi_module \
|
||
--without-http_grpc_module \
|
||
&& make -j"$(nproc)" 2>&1 | tail -5 \
|
||
&& test -f /src/pcre-${PCRE_VERSION}/pcre.h \
|
||
&& test -f /src/nginx-${NGINX_VERSION}/objs/ngx_auto_config.h \
|
||
&& test -x /src/nginx-${NGINX_VERSION}/objs/nginx
|
||
|
||
# --- 默认环境变量:让 FROM 此镜像的项目可以直接 cargo build ----
|
||
# 使用镜像的项目 cargo build 前不必再设这两个变量
|
||
ENV NGINX_SOURCE_DIR=/src/nginx-${NGINX_VERSION}
|
||
ENV NGINX_BUILD_DIR=/src/nginx-${NGINX_VERSION}/objs
|
||
ENV DEBIAN_FRONTEND=noninteractive
|
||
|
||
# 标记镜像元数据(其他项目 FROM 时可看到 base 来源)
|
||
ARG BASE_VERSION="nginx-${NGINX_VERSION}-rust-${RUST_VERSION}"
|
||
LABEL org.opencontainers.image.title="rust-nginx-base" \
|
||
org.opencontainers.image.description="ngx-rust 项目共用编译基础:rust 1.89 + 已 configure 的 nginx 1.30.4 + pcre 8.45" \
|
||
org.opencontainers.image.source="http://192.168.1.18:13300/ipass/sso" \
|
||
org.opencontainers.image.version="${BASE_VERSION}"
|
||
|
||
WORKDIR /build
|
||
CMD ["/bin/bash"] |