From 137e2ccc4c4deaf993bb6d6545775e59f7fe9148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Wed, 29 Apr 2026 13:22:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E5=B8=83=E9=95=9C=E5=83=8F=E9=87=8C?= =?UTF-8?q?=E5=B0=BD=E9=87=8F=E4=B8=8D=E8=A6=81=E5=8C=85=E5=90=AB=E6=BA=90?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 22 ++++++++++++++++------ Dockerfile.modelscope | 17 ++++++++++++----- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index d17ca36..14a6ecb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # 使用Python 3.12官方镜像作为基础镜像 -FROM python:3.12-slim +FROM python:3.12-slim AS base # 设置工作目录 WORKDIR /app @@ -43,17 +43,27 @@ RUN pip install --no-cache-dir playwright && \ RUN npm install -g playwright sharp nodemailer dotenv && \ npx playwright install chromium -# 复制应用代码 -COPY . . - # 创建必要的目录 -RUN mkdir -p /app/projects +RUN mkdir -p /app/projects RUN mkdir -p /app/public RUN mkdir -p /app/models # 下载sentence-transformers模型到models目录 RUN python -c "from sentence_transformers import SentenceTransformer; model = SentenceTransformer('TaylorAI/gte-tiny'); model.save('/app/models/gte-tiny')" +FROM base AS bytecode-builder + +# 复制应用代码,仅在构建阶段编译为字节码 +COPY . . +RUN python -m compileall -b /app && \ + find /app -type f -name '*.py' -delete && \ + find /app -type d -name '__pycache__' -prune -exec rm -rf {} + + +FROM base AS runtime + +# 只复制编译后的应用文件,避免源码进入最终镜像层 +COPY --from=bytecode-builder /app /app + # 暴露端口 EXPOSE 8001 @@ -62,4 +72,4 @@ HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \ CMD curl -f http://localhost:8001/api/health || exit 1 # 启动命令 - profile通过环境变量PROFILE配置,默认为balanced -CMD ["sh", "-c", "python3 start_unified.py --profile ${PROFILE:-balanced}"] +CMD ["sh", "-c", "python3 start_unified.pyc --profile ${PROFILE:-balanced}"] diff --git a/Dockerfile.modelscope b/Dockerfile.modelscope index 9cd3a3a..5a07676 100644 --- a/Dockerfile.modelscope +++ b/Dockerfile.modelscope @@ -1,5 +1,5 @@ # 使用Python 3.12官方镜像作为基础镜像 -FROM docker.1ms.run/python:3.12-slim +FROM docker.1ms.run/python:3.12-slim AS base # 设置工作目录 WORKDIR /app @@ -52,11 +52,18 @@ RUN mkdir -p /app/projects RUN mkdir -p /app/public RUN mkdir -p /app/models -# 从modelscope下载sentence-transformers模型到models目录 -#RUN python -c "from modelscope import snapshot_download; model_dir = snapshot_download('TaylorAI/gte-tiny'); import shutil; shutil.move(model_dir, '/app/models/gte-tiny')" +FROM base AS bytecode-builder -# 复制应用代码 +# 复制应用代码,仅在构建阶段编译为字节码 COPY . . +RUN python -m compileall -b /app && \ + find /app -type f -name '*.py' -delete && \ + find /app -type d -name '__pycache__' -prune -exec rm -rf {} + + +FROM base AS runtime + +# 只复制编译后的应用文件,避免源码进入最终镜像层 +COPY --from=bytecode-builder /app /app # 暴露端口 EXPOSE 8001 @@ -66,4 +73,4 @@ HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \ CMD curl -f http://localhost:8001/api/health || exit 1 # 启动命令 - 使用优化的统一启动脚本 -CMD ["sh", "-c", "python3 start_unified.py --profile ${PROFILE:-balanced}"] +CMD ["sh", "-c", "python3 start_unified.pyc --profile ${PROFILE:-balanced}"]