镜像与构建 - 六阶段 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 进镜像
177 lines
8.2 KiB
Docker
177 lines
8.2 KiB
Docker
# syntax=docker/dockerfile:1.7
|
||
#
|
||
# agent-safe-middleware 镜像,两个目标:
|
||
#
|
||
# --target core 纯编排:不装 torch / transformers / 模型,判定走 vLLM HTTP。
|
||
# 最终形态,约 200 MB。需要先应用 docs/目前方案说明.md §6 的 vLLM 补丁。
|
||
#
|
||
# --target guard 过渡形态:进程内 transformers 加载 guard 模型,需要挂载模型目录。
|
||
# torch CPU 版约 +300 MB;torch cu124 约 +6.4 GB。
|
||
#
|
||
# 每个模块的功能可溯源:
|
||
# manifest 阶段跑 scripts/gen_manifest.py --strict(有未登记模块直接失败),
|
||
# core/guard 阶段再把 MANIFEST.json 里的 sha256 逐个重算一遍——构建期自证,
|
||
# 运行时还能用 `docker run --rm --entrypoint python <img> /service/gen_manifest.py verify` 复验。
|
||
# 登记表在 deploy/docker/modules.json,人读版清单在 docs/模块溯源.md(由脚本生成)。
|
||
#
|
||
# 构建:
|
||
# docker build --target core -t agent-safe-middleware:core .
|
||
# docker build --target guard -t agent-safe-middleware:guard .
|
||
# docker build --target guard --build-arg TORCH_INDEX=https://download.pytorch.org/whl/cu124 \
|
||
# -t agent-safe-middleware:guard-cu124 .
|
||
#
|
||
# 想留下溯源头就把版本号传进来(不进构建缓存,只进 label):
|
||
# docker build --target core --build-arg GIT_REV=$(git rev-parse --short HEAD) .
|
||
#
|
||
# 模型与配置都不进镜像:模型挂卷,配置挂到 /service/config.yaml 覆盖默认。
|
||
|
||
ARG PYTHON_VERSION=3.12
|
||
ARG GIT_REV=unknown
|
||
ARG BUILD_TIME=unknown
|
||
|
||
# --------------------------------------------------------------------------- #
|
||
# 公共底座
|
||
# --------------------------------------------------------------------------- #
|
||
FROM python:${PYTHON_VERSION}-slim AS base
|
||
|
||
# 不要 python:3.14:lupa / torch / tokenizers 的 cp312 wheel 齐,3.14 要现编译
|
||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||
PYTHONUNBUFFERED=1 \
|
||
PIP_NO_CACHE_DIR=1 \
|
||
PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
||
PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple
|
||
|
||
# .doc 抽取依赖 LibreOffice(约 400 MB),只有需要老格式附件时才开
|
||
ARG WITH_DOC_CONVERTER=0
|
||
RUN set -eux; \
|
||
if [ "$WITH_DOC_CONVERTER" = "1" ]; then \
|
||
apt-get update; \
|
||
apt-get install -y --no-install-recommends libreoffice-writer fonts-noto-cjk; \
|
||
rm -rf /var/lib/apt/lists/*; \
|
||
fi
|
||
|
||
RUN groupadd --gid 10001 app \
|
||
&& useradd --uid 10001 --gid 10001 --no-create-home --shell /usr/sbin/nologin app
|
||
|
||
WORKDIR /service
|
||
|
||
# --------------------------------------------------------------------------- #
|
||
# 轮子:把自家代码打成 wheel,避免把 setuptools / 源码树留在运行时镜像里
|
||
# --------------------------------------------------------------------------- #
|
||
FROM base AS wheels
|
||
COPY pyproject.toml ./
|
||
COPY middleware ./middleware
|
||
RUN pip wheel --no-deps --wheel-dir /wheels .
|
||
|
||
# --------------------------------------------------------------------------- #
|
||
# 依赖层:故意和代码层分开,改插件或改代码不会重装依赖
|
||
# --------------------------------------------------------------------------- #
|
||
FROM base AS deps-core
|
||
COPY requirements.txt ./
|
||
RUN --mount=type=cache,target=/root/.cache/pip \
|
||
pip install -r requirements.txt
|
||
|
||
FROM base AS deps-guard
|
||
ARG TORCH_VERSION=2.6.0
|
||
ARG TORCH_INDEX=https://download.pytorch.org/whl/cpu
|
||
COPY requirements.txt requirements-guard.txt ./
|
||
RUN --mount=type=cache,target=/root/.cache/pip \
|
||
pip install -r requirements.txt \
|
||
&& pip install --index-url "$TORCH_INDEX" "torch==${TORCH_VERSION}" \
|
||
&& pip install -r requirements-guard.txt
|
||
|
||
# --------------------------------------------------------------------------- #
|
||
# 溯源:只拿"要进镜像的那些文件"生成清单,漏登记就在这里构建失败
|
||
# --------------------------------------------------------------------------- #
|
||
FROM base AS manifest
|
||
WORKDIR /src
|
||
COPY pyproject.toml config.yaml ./
|
||
COPY middleware ./middleware
|
||
COPY plugins ./plugins
|
||
COPY deploy/docker/modules.json ./deploy/docker/modules.json
|
||
COPY scripts/gen_manifest.py ./scripts/gen_manifest.py
|
||
# 中文文件名留在仓库里,镜像内改用 ASCII 路径,避免 docker save/export 在 Windows 上翻车
|
||
RUN python scripts/gen_manifest.py gen --strict --out /out \
|
||
&& cp "/out/模块溯源.md" /out/MODULES.md
|
||
|
||
# --------------------------------------------------------------------------- #
|
||
# core:无 torch、无模型,判定全部走 vLLM HTTP
|
||
# --------------------------------------------------------------------------- #
|
||
FROM base AS core
|
||
ARG GIT_REV
|
||
ARG BUILD_TIME
|
||
LABEL org.opencontainers.image.title="agent-safe-middleware" \
|
||
org.opencontainers.image.description="Lua-first HTTP/SSE safety middleware (vLLM guard backend)" \
|
||
org.opencontainers.image.version="0.1.0" \
|
||
org.opencontainers.image.revision="${GIT_REV}" \
|
||
org.opencontainers.image.created="${BUILD_TIME}" \
|
||
ai.asm.target="core" \
|
||
ai.asm.module.registry="deploy/docker/modules.json" \
|
||
ai.asm.module.manifest="/service/MANIFEST.json"
|
||
|
||
COPY --from=deps-core /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
|
||
COPY --from=deps-core /usr/local/bin /usr/local/bin
|
||
COPY --from=wheels /wheels /wheels
|
||
RUN pip install --no-deps --no-index --find-links=/wheels agent-safe-middleware \
|
||
&& rm -rf /wheels
|
||
|
||
# 镜像里的 config.yaml 是仓库根那份(清单按它算 sha256,不能换成 deploy/docker/ 下的)。
|
||
# 运行时必须用 -v 覆盖成容器配置,见 docker-compose.yml;不挂会因 backend/device 不匹配启动失败。
|
||
COPY config.yaml ./
|
||
COPY plugins ./plugins
|
||
|
||
# 溯源二件套 + 构建期自证:装进去的每个文件都要对得上清单里的 sha256
|
||
COPY --from=manifest /out/MANIFEST.json /service/MANIFEST.json
|
||
COPY --from=manifest /out/MODULES.md /service/MODULES.md
|
||
COPY scripts/gen_manifest.py /service/gen_manifest.py
|
||
RUN python /service/gen_manifest.py verify --manifest /service/MANIFEST.json \
|
||
&& python -c "import json;m=json.load(open('/service/MANIFEST.json'));print('module bundle:',m['bundle_digest'])"
|
||
|
||
ENV HF_HUB_OFFLINE=1
|
||
USER 10001:10001
|
||
EXPOSE 8080
|
||
HEALTHCHECK --interval=15s --timeout=5s --start-period=30s --retries=3 \
|
||
CMD ["python", "-c", "import sys,urllib.request; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/readyz', timeout=3).status == 200 else 1)"]
|
||
ENTRYPOINT ["agent-safe-middleware"]
|
||
CMD ["--config", "/service/config.yaml"]
|
||
|
||
# --------------------------------------------------------------------------- #
|
||
# guard:进程内 transformers,需要 -v /path/to/small_model:/service/models/small_model:ro
|
||
# --------------------------------------------------------------------------- #
|
||
FROM base AS guard
|
||
ARG GIT_REV
|
||
ARG BUILD_TIME
|
||
LABEL org.opencontainers.image.title="agent-safe-middleware" \
|
||
org.opencontainers.image.description="Lua-first HTTP/SSE safety middleware (in-process guard backend)" \
|
||
org.opencontainers.image.version="0.1.0" \
|
||
org.opencontainers.image.revision="${GIT_REV}" \
|
||
org.opencontainers.image.created="${BUILD_TIME}" \
|
||
ai.asm.target="guard" \
|
||
ai.asm.module.registry="deploy/docker/modules.json" \
|
||
ai.asm.module.manifest="/service/MANIFEST.json"
|
||
|
||
COPY --from=deps-guard /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
|
||
COPY --from=deps-guard /usr/local/bin /usr/local/bin
|
||
COPY --from=wheels /wheels /wheels
|
||
RUN pip install --no-deps --no-index --find-links=/wheels agent-safe-middleware \
|
||
&& rm -rf /wheels
|
||
|
||
COPY config.yaml ./
|
||
COPY plugins ./plugins
|
||
|
||
COPY --from=manifest /out/MANIFEST.json /service/MANIFEST.json
|
||
COPY --from=manifest /out/MODULES.md /service/MODULES.md
|
||
COPY scripts/gen_manifest.py /service/gen_manifest.py
|
||
RUN python /service/gen_manifest.py verify --manifest /service/MANIFEST.json \
|
||
&& python -c "import json;m=json.load(open('/service/MANIFEST.json'));print('module bundle:',m['bundle_digest'])"
|
||
|
||
ENV HF_HUB_OFFLINE=1 \
|
||
HF_HOME=/tmp/hf
|
||
USER 10001:10001
|
||
EXPOSE 8080
|
||
# 模型冷加载 10–30 s,start-period 给足;判定超时按 config 的 timeout_seconds
|
||
HEALTHCHECK --interval=15s --timeout=5s --start-period=180s --retries=3 \
|
||
CMD ["python", "-c", "import sys,urllib.request; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/readyz', timeout=3).status == 200 else 1)"]
|
||
ENTRYPOINT ["agent-safe-middleware"]
|
||
CMD ["--config", "/service/config.yaml"]
|