feat: Docker 打包、交付链路与命名脱敏

镜像与构建
- 六阶段 Dockerfile(base/wheels/deps/manifest/core|guard),core 不含 torch
- 构建期自证:gen_manifest --strict 拦未登记模块,core 阶段重算 sha256
- compose 四服务 host 网络(llm.url 强制回环),config.core/local 可挂载覆盖

无 docker 环境的四段链路
- pull_oci_image.py      直拉 OCI 镜像 rootfs(纯标准库,走加速器)
- replay_image_build.sh  按 stage chroot 重放 Dockerfile
- run_image_chroot.sh    等价 docker run(uid 10001 + 入口 + host 网络)
- pack_image.py          封成 docker save 格式包,自带 diff_id 校验

交付
- make_package.sh 生成自包含目录:镜像包 + 溯源清单 + 构建输入 + 脚本 + 源码快照
- 文档:从零复现指南、docker 打包方案、镜像构建限制与替代方案、交付包 README

命名
- Qwen 品牌名统一替换为 large_model / small_model / large_model_guard,
  覆盖 vLLM 服务名、模型目录、配置、脚本、测试与文档
- 模型架构字段(architectures / model_type / tokenizer_class)属上游定义,
  改名会导致加载失败,保留原值;check_local_model.py 的架构判断同理

验证:smoke 6/6;test.py 20 通过 0 失败 1 警告;gen --strict 67 文件 / 22 进镜像
This commit is contained in:
chenleixu committed 2026-11-27 18:20:16 +00:00
1 parent bfdc5e9168
commit b82e44ec32
46 files changed
+10271 -484

No files matched your search

+216
View File
@@ -0,0 +1,216 @@
#!/usr/bin/env bash
# 在没有容器运行时的机器上,按 Dockerfile 的每个 stage 逐条重放构建。
#
# 适用场景:目标机是个无特权容器(CapEff=0、无 cap_sys_admin),dockerd 起不来,
# 但需要验证 Dockerfile 本身是否成立。做法是把真实 base 镜像的 rootfs 拉下来
# (scripts/pull_oci_image.py),用 chroot 执行相同的命令序列。
#
# 与 docker build 的差异(必须知道):
# 1. chroot 不是容器:没有 namespace / cgroup / seccomp 隔离,不是只读根文件系统。
# 2. 各 stage 共用同一个 rootfs,靠"最后清理跨 stage 临时目录"来对齐最终内容。
# 3. USER 10001 只是切换身份,不改变文件系统视图。
# 相同点:base 镜像内容、pip 源、依赖集合、文件落点、MANIFEST 生成与自证、入口命令。
#
# 用法:
# sudo scripts/replay_image_build.sh --rootfs /home/chenleixu/imgroot --src .
#
# 退出码:任一步骤失败即非零。
set -uo pipefail
ROOTFS=""
SRC="."
TARGET="core"
PIP_INDEX="https://pypi.tuna.tsinghua.edu.cn/simple"
while [ $# -gt 0 ]; do
case "$1" in
--rootfs) ROOTFS="$2"; shift 2 ;;
--src) SRC="$2"; shift 2 ;;
--target) TARGET="$2"; shift 2 ;;
--index) PIP_INDEX="$2"; shift 2 ;;
-h|--help) sed -n '2,26p' "$0"; exit 0 ;;
*) echo "未知参数:$1" >&2; exit 2 ;;
esac
done
[ -n "$ROOTFS" ] || { echo "必须给 --rootfs" >&2; exit 2; }
[ -x "$ROOTFS/usr/local/bin/python3" ] || { echo "$ROOTFS 不像 base 镜像 rootfs(缺 python3)" >&2; exit 2; }
SRC=$(cd "$SRC" && pwd)
STEP=0
FAILED=0
declare -a RESULTS=()
ok() { RESULTS+=("PASS $1"); printf ' \033[32m✔\033[0m %s\n' "$1"; }
bad() { RESULTS+=("FAIL $1"); FAILED=$((FAILED+1)); printf ' \033[31m✘\033[0m %s\n' "$1"; }
phase() { STEP=$((STEP+1)); printf '\n\033[1m[%s] %s\033[0m\n' "$STEP" "$1"; }
# 在 rootfs 里执行,环境变量对齐 Dockerfile 的 ENV
in_image() {
sudo -n chroot "$ROOTFS" /usr/bin/env -i \
PATH=/usr/local/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin \
LANG=C.UTF-8 HOME=/root TMPDIR=/tmp \
PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1 PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_INDEX_URL="$PIP_INDEX" \
"$@"
}
copy_in() { # copy_in <宿主路径> <镜像内绝对路径>
sudo -n mkdir -p "$ROOTFS$(dirname "$2")"
sudo -n rm -rf "$ROOTFS$2"
sudo -n cp -a "$1" "$ROOTFS$2"
}
echo "=============================================================="
echo " 重放 Dockerfile(target=$TARGET)"
echo " rootfs : $ROOTFS"
echo " 源码 : $SRC"
echo "=============================================================="
# ---------------------------------------------------------------------------
phase "stage base —— ENV / 用户 / WORKDIR"
# ---------------------------------------------------------------------------
if in_image python3 -c "import sys;print(sys.version.split()[0])" >/dev/null 2>&1; then
V=$(in_image python3 -c "import sys;print('%d.%d'%sys.version_info[:2])")
ok "base python $V 可用"
else
bad "base python 不可用"
fi
if in_image groupadd --gid 10001 app 2>/dev/null && in_image useradd --uid 10001 --gid 10001 --no-create-home --shell /usr/sbin/nologin app 2>/dev/null; then
ok "groupadd/useradd app(10001)"
elif in_image id app >/dev/null 2>&1; then
ok "app(10001) 已存在"
else
bad "建 app 用户失败(slim 里没有 useradd?)"
fi
in_image mkdir -p /service && ok "WORKDIR /service"
# ---------------------------------------------------------------------------
phase "stage wheels —— pip wheel --no-deps ."
# ---------------------------------------------------------------------------
copy_in "$SRC/pyproject.toml" /service/pyproject.toml
sudo -n rm -rf "$ROOTFS/service/middleware"
sudo -n cp -a "$SRC/middleware" "$ROOTFS/service/middleware"
if in_image python3 -m pip wheel --no-deps --wheel-dir /wheels /service >/tmp/wheels.log 2>&1; then
W=$(ls "$ROOTFS/wheels" 2>/dev/null | tr '\n' ' ')
ok "wheel 产出:$W"
else
bad "pip wheel 失败(见 /tmp/wheels.log 对应输出)"; tail -15 /tmp/wheels.log | sed 's/^/ /'
fi
# ---------------------------------------------------------------------------
phase "stage deps-core —— pip install -r requirements.txt"
# ---------------------------------------------------------------------------
copy_in "$SRC/requirements.txt" /service/requirements.txt
if in_image python3 -m pip install -r /service/requirements.txt >/tmp/deps.log 2>&1; then
N=$(in_image python3 -c "import sysconfig,os;p=sysconfig.get_paths()['purelib'];print(len(os.listdir(p)))")
ok "依赖安装完成,site-packages 条目 $N 个"
else
bad "依赖安装失败"; tail -20 /tmp/deps.log | sed 's/^/ /'
fi
# ---------------------------------------------------------------------------
phase "stage manifest —— gen --strict(未登记模块即失败)"
# ---------------------------------------------------------------------------
sudo -n mkdir -p "$ROOTFS/src/deploy/docker" "$ROOTFS/src/scripts"
copy_in "$SRC/pyproject.toml" /src/pyproject.toml
copy_in "$SRC/config.yaml" /src/config.yaml
sudo -n rm -rf "$ROOTFS/src/middleware" "$ROOTFS/src/plugins"
sudo -n cp -a "$SRC/middleware" "$ROOTFS/src/middleware"
sudo -n cp -a "$SRC/plugins" "$ROOTFS/src/plugins"
copy_in "$SRC/deploy/docker/modules.json" /src/deploy/docker/modules.json
copy_in "$SRC/scripts/gen_manifest.py" /src/scripts/gen_manifest.py
if in_image python3 /src/scripts/gen_manifest.py gen --strict --out /out >/tmp/manifest.log 2>&1; then
sudo -n cp "$ROOTFS/out/模块溯源.md" "$ROOTFS/out/MODULES.md"
BUNDLE=$(in_image python3 -c "import json;print(json.load(open('/out/MANIFEST.json'))['bundle_digest'][:16])")
CNT=$(in_image python3 -c "import json;m=json.load(open('/out/MANIFEST.json'))['totals'];print(m['image_files'],m['image_loc'],round(m['image_bytes']/1024))")
set -- $CNT
ok "清单生成:进镜像 $1 个文件 / $2 行 / $3 KB,bundle=${BUNDLE}…"
else
bad "gen --strict 失败(有未登记模块,或脚本报错)"; tail -20 /tmp/manifest.log | sed 's/^/ /'
fi
# ---------------------------------------------------------------------------
phase "stage core —— 装 wheel(不联网、不带依赖)"
# ---------------------------------------------------------------------------
if in_image python3 -m pip install --no-deps --no-index --find-links=/wheels agent-safe-middleware >/tmp/core.log 2>&1; then
sudo -n rm -rf "$ROOTFS/wheels"
ok "wheel 安装完成,/wheels 已删除"
if [ -x "$ROOTFS/usr/local/bin/agent-safe-middleware" ]; then
ok "入口脚本 /usr/local/bin/agent-safe-middleware 存在"
else
bad "入口脚本缺失(pyproject 的 [project.scripts] 没生成?)"
fi
else
bad "wheel 安装失败"; tail -20 /tmp/core.log | sed 's/^/ /'
fi
# ---------------------------------------------------------------------------
phase "stage core —— COPY config.yaml / plugins"
# ---------------------------------------------------------------------------
copy_in "$SRC/config.yaml" /service/config.yaml
sudo -n rm -rf "$ROOTFS/service/plugins"
sudo -n cp -a "$SRC/plugins" "$ROOTFS/service/plugins"
ok "config.yaml + $(ls "$SRC/plugins" | wc -l) 个插件落到 /service"
# ---------------------------------------------------------------------------
phase "stage core —— 溯源二件套 + 构建期自证"
# ---------------------------------------------------------------------------
copy_in "$ROOTFS/out/MANIFEST.json" /service/MANIFEST.json
copy_in "$ROOTFS/out/MODULES.md" /service/MODULES.md
copy_in "$SRC/scripts/gen_manifest.py" /service/gen_manifest.py
if in_image python3 /service/gen_manifest.py verify --manifest /service/MANIFEST.json >/tmp/verify.log 2>&1; then
grep -E '已核验|一致' /tmp/verify.log | sed 's/^/ /'
ok "构建期自证通过(sha256 全部一致)"
else
bad "构建期自证失败"; cat /tmp/verify.log | sed 's/^/ /'
fi
# ---------------------------------------------------------------------------
phase "收尾 —— 清掉跨 stage 的临时目录,对齐 core 最终内容"
# ---------------------------------------------------------------------------
# pip wheel 会在源码目录留下 build/ 与 *.egg-info;真实 Docker 里这些属于 wheels
# 阶段(core 是 FROM base,empty /service),所以必须清掉才算内容对齐
sudo -n rm -rf "$ROOTFS/src" "$ROOTFS/out" \
"$ROOTFS/service/pyproject.toml" "$ROOTFS/service/requirements.txt" \
"$ROOTFS/service/middleware" "$ROOTFS/service/build" "$ROOTFS/service"/*.egg-info
LEFTOVER=$(sudo -n ls -1 "$ROOTFS/service" | grep -E '^(src|out|build|.*\.egg-info|pyproject\.toml|requirements\.txt|middleware)$' || true)
if [ -n "$LEFTOVER" ]; then
bad "还有跨 stage 残留:$(echo "$LEFTOVER" | tr '\n' ' ')"
else
ok "已清理跨 stage 残留(/src /out build *.egg-info pyproject.toml requirements.txt middleware)"
fi
echo
echo " /service 内容:"
sudo -n ls -1 "$ROOTFS/service" | sed 's/^/ /'
# ---------------------------------------------------------------------------
phase "镜像形态校验 —— core 必须没有 torch"
# ---------------------------------------------------------------------------
if in_image python3 -c "import torch" 2>/dev/null; then
bad "core 里能 import torch(converter 混进来了)"
else
ok "core 里没有 torch(符合设计)"
fi
if in_image python3 -c "import middleware;print(middleware.__file__)" >/tmp/imp.log 2>&1; then
ok "middleware 可从 site-packages 导入:$(cat /tmp/imp.log | tr -d '\n')"
else
bad "middleware 导入失败"; cat /tmp/imp.log | sed 's/^/ /'
fi
echo
echo "=============================================================="
printf ' 结果:'
for r in "${RESULTS[@]}"; do printf '%s\n ' "$r"; done
echo
if [ "$FAILED" -eq 0 ]; then
printf ' 全部通过(%d 项)\n' "${#RESULTS[@]}"
else
printf ' \033[31m%d 项失败\033[0m / 共 %d 项\n' "$FAILED" "${#RESULTS[@]}"
fi
echo "=============================================================="
exit "$([ "$FAILED" -eq 0 ] && echo 0 || echo 1)"