Files
sso/scripts/container-entrypoint.sh
T
admin 67a8e731cc feat(deploy): 容器化构建(多阶段 Dockerfile + compose + entrypoint + 共用 base)
- 主镜像 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)
2026-09-24 16:27:05 +08:00

75 lines
3.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# ============================================================================
# 容器入口脚本(首发 + 每次启动都跑)
#
# 干的事:
# 1. 首次启动:data/ 为空时跑 seed-data.sh,生成 admin/123456 + alice/alice123
# 2. 每次启动:把容器外的 env(OAUTH_ISSUER 等)打印出来供排障
# 3. nginx -t 语法预检
# 4. exec nginx(让 nginx 拿到 PID 1,否则 stop signal 收不到)
#
# 设计:
# - nginx.test.conf 用了 env 占位(OAUTH_ISSUER / OAUTH_SIGNING_SECRET 等),
# nginx 启动时会从环境变量读取,无需重新渲染模板
# - seed-data.sh 用 python3 算 SHA-256 散列(与 rust/src/util.rs::hash_password
# 逐位一致:sha256("salt:password") 后迭代 1000 轮 sha256(x + salt))
# ============================================================================
set -euo pipefail
DATA_DIR="/usr/local/nginx/data"
SEED_DIR="/usr/local/nginx/data-seed"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
LOG_DIR="/usr/local/nginx/logs"
c_ok() { printf '\033[32m✓\033[0m %s\n' "$*"; }
c_warn() { printf '\033[33m!\033[0m %s\n' "$*"; }
c_err() { printf '\033[31m✗\033[0m %s\n' "$*" >&2; }
# ---------------------------------------------------------------- 0. 基础目录
mkdir -p "$DATA_DIR" "$LOG_DIR"
# ---------------------------------------------------------------- 1. 打印关键 env(排障)
printf '\n== ipass-sso 启动参数 ==\n'
c_ok "OAUTH_ISSUER = ${OAUTH_ISSUER:-<未设置,默认走 nginx.conf 占位>}"
c_ok "OAUTH_COOKIE_DOMAIN= ${OAUTH_COOKIE_DOMAIN:-<未设置>}"
c_ok "ADMIN_USER = ${ADMIN_USER:-admin}"
if [ -n "${ADMIN_PASSWORD:-}" ]; then
c_ok "ADMIN_PASSWORD = <已注入>"
else
c_warn "ADMIN_PASSWORD = <未注入>,seed 将使用默认 123456"
fi
if [ -n "${OAUTH_SIGNING_SECRET:-}" ]; then
c_ok "OAUTH_SIGNING_SECRET= <已注入>"
else
c_warn "OAUTH_SIGNING_SECRET= <未注入>,seed 将生成随机值(重启即丢!)"
fi
# ---------------------------------------------------------------- 2. 数据物化(首次启动)
NEED_SEED=0
[ ! -f "$DATA_DIR/users.json" ] && NEED_SEED=1
[ ! -f "$DATA_DIR/oauth_clients.json" ] && NEED_SEED=1
if [ "$NEED_SEED" = "1" ]; then
printf '\n== 首次启动:seed-data.sh 物化 admin/alice ==\n'
# nginx worker 是 nobody(uid 65534),必须可写
chown -R 65534:65534 "$DATA_DIR" 2>/dev/null || c_warn "chown 65534 失败(继续)"
ADMIN_PASSWORD="${ADMIN_PASSWORD:-123456}" \
ALICE_PASSWORD="${ALICE_PASSWORD:-alice123}" \
DATA_DIR="$DATA_DIR" \
SEED_DIR="$SEED_DIR" \
bash /usr/local/nginx/scripts/seed-data.sh || \
{ c_err "seed-data.sh 失败"; exit 1; }
c_ok "data 目录物化完成"
else
c_ok "data 目录已有,跳过 seed"
fi
# ---------------------------------------------------------------- 3. nginx 语法预检
printf '\n== nginx -t ==\n'
/usr/local/nginx/sbin/nginx -t -c "$NGINX_CONF" 2>&1 || { c_err "nginx 配置语法错误"; exit 1; }
# ---------------------------------------------------------------- 4. exec nginx(PID 1 拿信号)
printf '\n== 起 nginx ==\n'
exec /usr/local/nginx/sbin/nginx -c "$NGINX_CONF" -g 'daemon off;'