registry/git/target 三个 mount,源码不变时跳过依赖重编;cache mount 内容不进 镜像层,.so 先复制到 /out,runtime 改从 /out 取。 原写法 `cargo build 2>&1 | tail -15` 的退出码取自 tail,cargo 失败会被判成功 并缓存坏层,改为落日志 + 失败打尾部 + 非零退出。
137 lines
6.9 KiB
Docker
137 lines
6.9 KiB
Docker
# ipass-sso 网关镜像 —— 多阶段构建(复用 rust-nginx-base)
|
||
#
|
||
# 从 ipass-all 派生的精简发行版:只装 ipass-core + ipass-plugin-sso(2 个 named
|
||
# context),gateway 装配层把内核 + sso 编进单个 libipass_sso_gateway.so。
|
||
#
|
||
# 设计:
|
||
# - builder 阶段:FROM crpi-yscq976uwza88qap.cn-shenzhen.personal.cr.aliyuncs.com/ssw188/nginx:1.30.4-rust-1.89
|
||
# base 已含 rust 1.89 + 已 configure/make 的 nginx 1.30.4 + pcre 8.45 + 编译链,
|
||
# 这里只跑 cargo build
|
||
# - runtime 阶段:debian:bookworm-slim,只带 nginx 二进制 + 模块 + 静态资源
|
||
#
|
||
# 构建命令(context = 本仓根;2 个 named context 缺一不可):
|
||
# bash scripts/collect-plugin-dist.sh # 前置:收集壳 + sso 管理页 dist
|
||
# docker build \
|
||
# --build-context ipass-core=../ipass-core \
|
||
# --build-context ipass-plugin-sso=../ipass-plugin-sso \
|
||
# -t crpi-yscq976uwza88qap.cn-shenzhen.personal.cr.aliyuncs.com/ssw188/nginx:ipass-sso-v0.6.0 .
|
||
#
|
||
# named context 落位约定:COPY 到 /ipass-core、/ipass-plugin-sso —— builder 的
|
||
# WORKDIR 是 /build,rust/Cargo.toml 里 `../../ipass-core/...` 与插件自己的
|
||
# `../ipass-core/...` 等 path 依赖按宿主机「同级目录」布局原样解析。
|
||
# ⚠️ 每个插件仓只 COPY 编译所需子集(Cargo.toml/src/vendor/rules),
|
||
# 避开几百 MB 的 target/ 与 data/。
|
||
#
|
||
# 升级 nginx/rust 版本时:先跑 ipass-sso/deploy/build-base-image.sh 推新 base,
|
||
# 再 bump 此处 FROM 的 tag。
|
||
|
||
# syntax=docker/dockerfile:1.7
|
||
|
||
ARG BASE_TAG=1.30.4-rust-1.89
|
||
|
||
# ===========================================================================
|
||
# builder: 复用 rust-nginx-base + Rust 模块编译(nginx 二进制 base 里已 make 好)
|
||
# ===========================================================================
|
||
FROM crpi-yscq976uwza88qap.cn-shenzhen.personal.cr.aliyuncs.com/ssw188/nginx:${BASE_TAG} AS builder
|
||
|
||
WORKDIR /build
|
||
|
||
# 内核(含 vendored ngx-rust 与 pcre 路径依赖的源码树约定)
|
||
COPY --from=ipass-core /rust/Cargo.toml /rust/Cargo.lock /ipass-core/rust/
|
||
COPY --from=ipass-core /rust/crates/ /ipass-core/rust/crates/
|
||
COPY --from=ipass-core /rust/vendor/ /ipass-core/rust/vendor/
|
||
|
||
# SSO 插件 crate(只 1 个 plugin)
|
||
COPY --from=ipass-plugin-sso /Cargo.toml /Cargo.lock /ipass-plugin-sso/
|
||
COPY --from=ipass-plugin-sso /src/ /ipass-plugin-sso/src/
|
||
|
||
# 装配层(本仓)
|
||
COPY rust/ /build/rust/
|
||
|
||
# cargo 国内镜像源(rsproxy):base 镜像没带 cargo 配置,直连 crates.io
|
||
# 在国内网络下会长时间悬挂在下载阶段(ipass-sso 同款)。
|
||
RUN printf '[source.crates-io]\nreplace-with = "rsproxy-sparse"\n\n[source.rsproxy-sparse]\nregistry = "sparse+https://rsproxy.cn/index/"\n\n[net]\ngit-fetch-with-cli = true\n' \
|
||
> "${CARGO_HOME:-/usr/local/cargo}/config.toml"
|
||
|
||
WORKDIR /build/rust
|
||
# nginx 源码树路径与 base 镜像内一致(bindgen 从 objs/Makefile 抽 -I)
|
||
ENV NGINX_SOURCE_DIR=/src/nginx-1.30.4
|
||
ENV NGINX_BUILD_DIR=/src/nginx-1.30.4/objs
|
||
ENV CARGO_TARGET_DIR=/build/rust/target
|
||
|
||
# 依赖下载与编译中间产物都落在 BuildKit cache mount(不进镜像层、跨构建复用):
|
||
# registry/git 缓存 crate 下载,target 缓存依赖 rlib —— 源码不变时二次构建跳过重编
|
||
# (冷编十几分钟 → 命中后 1~2 分钟)。
|
||
# ⚠️ target 是 cache mount,内容不会进入镜像层,所以 .so 必须复制到普通路径 /out,
|
||
# 再由 runtime 阶段 COPY --from=builder /out/...(否则 COPY 找不到文件)。
|
||
# ⚠️ 原写法 `cargo build ... 2>&1 | tail -15` 的退出码取自 tail,cargo 失败会被判
|
||
# 成功并缓存坏层;改为落日志 + 失败打尾部再非零退出(ipass-all 同款修正)。
|
||
RUN --mount=type=cache,id=ipass-cargo-registry,target=/usr/local/cargo/registry,sharing=locked \
|
||
--mount=type=cache,id=ipass-cargo-git,target=/usr/local/cargo/git,sharing=locked \
|
||
--mount=type=cache,id=ipass-sso-cargo-target,target=/build/rust/target,sharing=locked \
|
||
set -eu; \
|
||
cargo build --release > /tmp/cargo-build.log 2>&1 \
|
||
|| { tail -30 /tmp/cargo-build.log; exit 1; }; \
|
||
tail -3 /tmp/cargo-build.log; \
|
||
mkdir -p /out; \
|
||
cp /build/rust/target/release/libipass_sso_gateway.so /out/
|
||
|
||
# ===========================================================================
|
||
# runtime: 极简镜像,只带 nginx 二进制 + 模块 + 静态资源 + 种子数据
|
||
# ===========================================================================
|
||
FROM debian:bookworm-slim AS runtime
|
||
|
||
ARG NGINX_VERSION=1.30.4
|
||
|
||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||
libpcre2-8-0 \
|
||
zlib1g \
|
||
libssl3 \
|
||
ca-certificates \
|
||
curl \
|
||
tzdata \
|
||
python3 \
|
||
&& rm -rf /var/lib/apt/lists/*
|
||
|
||
# 与 iPass 一致:TZ=Asia/Shanghai,util::day_id 按上海时区 0:00 切日(趋势/用量口径)
|
||
ENV TZ=Asia/Shanghai
|
||
|
||
COPY --from=builder /src/nginx-${NGINX_VERSION}/objs/nginx /usr/local/nginx/sbin/nginx
|
||
COPY --from=builder /src/nginx-${NGINX_VERSION}/conf/mime.types /usr/local/nginx/conf/mime.types
|
||
COPY --from=builder /out/libipass_sso_gateway.so /usr/local/nginx/modules/
|
||
|
||
# 生产配置(deploy/nginx.conf:全插件 location 布局 + 非 root 适配)
|
||
COPY deploy/nginx.conf /usr/local/nginx/conf/nginx.conf
|
||
|
||
# 管理台前端(Ruling 16):构建前由 scripts/collect-plugin-dist.sh 把
|
||
# ipass-core 壳收集进 deploy/shell、各插件 dist 收集进 deploy/static-plugins
|
||
COPY deploy/shell/ /usr/local/nginx/www/
|
||
COPY deploy/static-plugins/ /usr/local/nginx/static-plugins/
|
||
# 装配层自定义页清单(精简版不挂自定义页,无 deploy/custom/)
|
||
# 老管理台 sso 五 tab 单页(精简版只搬这一页,sso 插件 UMD 的 5 个 tab 全指它;
|
||
# 全自包含 inline css+js,零外部资产,nginx 走 /legacy/ 静态段)
|
||
COPY deploy/legacy/ /usr/local/nginx/legacy/
|
||
|
||
# 种子数据(admin/alice + OAuth 接入方 + 插件启停注册表)
|
||
COPY data-seed/ /usr/local/nginx/data-seed/
|
||
|
||
# 目录与属主:k8s 租户准入强制 runAsUser=10106,data 与 /tmp/nginx 必须
|
||
# 预先可写(挂 PVC 后属主由 fsGroup=10106 接管,chown 只保无卷直跑)。
|
||
# 日志走 /dev/std* 符号链接(kubectl logs 直接可见)。
|
||
RUN mkdir -p /usr/local/nginx/data /usr/local/nginx/logs /tmp/nginx \
|
||
&& chown -R 10106:10106 /usr/local/nginx/data /tmp/nginx \
|
||
&& ln -sf /dev/stdout /usr/local/nginx/logs/access.log \
|
||
&& ln -sf /dev/stderr /usr/local/nginx/logs/error.log
|
||
|
||
# 首次启动播种 admin/alice + 渲染配置 + 起 nginx
|
||
COPY scripts/container-entrypoint.sh /usr/local/nginx/scripts/container-entrypoint.sh
|
||
COPY scripts/seed-data.sh /usr/local/nginx/scripts/seed-data.sh
|
||
RUN chmod +x /usr/local/nginx/scripts/container-entrypoint.sh /usr/local/nginx/scripts/seed-data.sh
|
||
|
||
WORKDIR /usr/local/nginx
|
||
EXPOSE 18098
|
||
STOPSIGNAL SIGQUIT
|
||
|
||
ENTRYPOINT ["/usr/local/nginx/scripts/container-entrypoint.sh"]
|
||
CMD ["nginx", "-g", "daemon off;"]
|