huoyan-enterprise/backend/Dockerfile

97 lines
3.4 KiB
Docker
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.

# =============================================================================
# 阶段一:编译阶段
# 将核心业务目录编译为原生二进制,源码不进入最终镜像
# =============================================================================
FROM python:3.11-slim-bookworm AS builder
WORKDIR /build
# 替换为阿里云 apt 镜像源,解决国内网络访问 Debian 官方源超时问题
RUN sed -i 's|http://deb.debian.org|http://mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources \
|| sed -i 's|http://deb.debian.org|http://mirrors.aliyun.com|g' /etc/apt/sources.list
# 系统依赖gcc/g++ 用于 Nuitka 编译patchelf 用于修正共享库路径
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc g++ patchelf ccache \
&& rm -rf /var/lib/apt/lists/*
# 配置镜像源
ENV PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/
ENV UV_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/
ENV UV_HTTP_TIMEOUT=120
ENV UV_HTTP_RETRIES=5
RUN pip install --no-cache-dir uv
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-dev
# nuitka 安装到 venv 中,确保与 .venv/bin/python 一致
RUN uv pip install --no-cache-dir nuitka
# 复制所有源码
COPY . .
# 编译需要保护的目录admin api core logger models services tools utils
# --module编译为可 import 的扩展模块(.so而不是独立可执行文件
# --follow-import-to只跟踪项目自身的包不拉入标准库和第三方包
# --output-dir编译产物输出目录每个包单独编译
RUN for pkg in admin api core logger models services tools utils; do \
uv run python -m nuitka \
--module \
--follow-import-to=admin \
--follow-import-to=api \
--follow-import-to=core \
--follow-import-to=logger \
--follow-import-to=models \
--follow-import-to=services \
--follow-import-to=tools \
--follow-import-to=utils \
--include-package=${pkg} \
--output-dir=/compiled \
--remove-output \
${pkg} ; \
done
# =============================================================================
# 阶段二:运行阶段
# 只包含编译产物和必要运行时,不含任何 .py 源码
# =============================================================================
FROM python:3.11-slim-bookworm
WORKDIR /app
ENV PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/
ENV UV_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/
ENV UV_HTTP_TIMEOUT=120
ENV UV_HTTP_RETRIES=5
RUN pip install --no-cache-dir uv
# 复制依赖声明文件并安装生产依赖
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-dev
# 安装 Playwright Chromium 浏览器及系统依赖crawl4ai 网页抓取功能依赖)
RUN uv run playwright install chromium \
&& uv run playwright install-deps chromium
# 从编译阶段复制二进制模块(.so 文件)
COPY --from=builder /compiled/ ./
# 保留必要的明文文件
# main.pyuvicorn 入口,只有两行 import不含业务逻辑
COPY main.py ./
# prompt/:提示词模板,运行时动态读取
COPY prompt/ ./prompt/
# services/fonts/:字体资源文件
COPY services/fonts/ ./services/fonts/
# 工作目录固定为 /app所有路径相关代码基于 cwd 解析
ENV PYTHONUNBUFFERED=1
ENV APP_DIR=/app
EXPOSE 7862
CMD ["uv", "run", "python", "-m", "main"]