增加可选部署

This commit is contained in:
dxin
2025-12-22 11:12:04 +08:00
parent 62a4ced1a6
commit 9abd8bdf46
22 changed files with 3421 additions and 37 deletions

View File

@@ -1,3 +1,6 @@
###############################################
# Stage 1: Builder (构建依赖 + venv
###############################################
FROM uswccr.ccs.tencentyun.com/lessie/python:3.12-slim AS builder
ENV PYTHONDONTWRITEBYTECODE=1 \
@@ -5,32 +8,71 @@ ENV PYTHONDONTWRITEBYTECODE=1 \
WORKDIR /data/webapps/lessie_sourcing_agents
# Install build prerequisites required for native wheels, then remove leftover apt metadata.
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
ca-certificates \
cmake \
curl \
git \
libomp-dev \
libopenblas-dev \
ninja-build \
pkg-config \
&& \
# ------------------------------------------------------------
# 1. APT 源替换 + 安装依赖(合并为一个 RUN最大化缓存
# ------------------------------------------------------------
RUN set -eux; \
sed -i 's@deb.debian.org@mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list.d/debian.sources; \
sed -i 's@security.debian.org@mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list.d/debian.sources; \
\
apt-get update; \
apt-get install -y --no-install-recommends \
build-essential \
ca-certificates \
cmake \
curl \
git \
libomp-dev \
libopenblas-dev \
ninja-build \
pkg-config; \
rm -rf /var/lib/apt/lists/*
# ------------------------------------------------------------
# 2. Pip 国内源设置(提前设置以便缓存命中)
# ------------------------------------------------------------
RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ \
&& pip config set global.trusted-host mirrors.aliyun.com
# ------------------------------------------------------------
# 3. 安装 uv
# ------------------------------------------------------------
RUN python -m pip install --no-cache-dir "uv"
# ------------------------------------------------------------
# 4. 拷贝依赖文件(只有这些变了才会重新跑 uv sync
# ------------------------------------------------------------
COPY pyproject.toml uv.lock requirements.in requirements.txt ./
# RUN python -m uv venv --python /usr/local/bin/python /opt/venv && \
# python -m uv pip install --python /opt/venv/bin/python --no-cache -r requirements.txt
ENV UV_PROJECT_ENVIRONMENT=/opt/venv
RUN python -m uv venv /opt/venv && \
python -m uv sync --frozen --no-dev --python /usr/local/bin/python
# ------------------------------------------------------------
# 5. 设置 uv 变量(放在 COPY 前面确保有效缓存)
# ------------------------------------------------------------
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
UV_HTTP_TIMEOUT=600 \
UV_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple \
UV_EXTRA_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple
# ------------------------------------------------------------
# 6. 创建 venv + 安装依赖(强烈建议使用 BuildKit 的 cache mount
# ------------------------------------------------------------
RUN --mount=type=cache,target=/root/.cache \
python -m uv venv /opt/venv && \
python -m uv sync --frozen --no-dev --python /usr/local/bin/python --index $UV_INDEX_URL
# ------------------------------------------------------------
# 7. llama_index 强烈推荐写入 pyproject.toml而不是单独安装
# ------------------------------------------------------------
RUN python -m uv pip install --python /opt/venv/bin/python --no-cache-dir llama_index
###############################################
# Stage 2: Runtime (最小化生产镜像)
###############################################
FROM uswccr.ccs.tencentyun.com/lessie/python:3.12-slim AS runtime
ARG APP_PORT=8000
@@ -40,32 +82,35 @@ ENV APP_ENV=local \
WORKDIR /data/webapps/lessie_sourcing_agents
# Runtime dependencies required by packages like faiss-cpu.
RUN apt-get update && apt-get install -y --no-install-recommends \
libgomp1 \
libomp5 \
libopenblas0 \
&& \
rm -rf /var/lib/apt/lists/*
# Bring in the pre-built virtual environment from the builder stage.
# ------------------------------------------------------------
# 8. 拷贝 venv仅包含已安装依赖
# ------------------------------------------------------------
COPY --from=builder /opt/venv /opt/venv
# Ship only the application sources in the runtime image.
# ------------------------------------------------------------
# 9. 仅拷贝业务代码(不会影响依赖缓存)
# ------------------------------------------------------------
COPY . .
# ------------------------------------------------------------
# 10. 设置环境变量和 PATH
# ------------------------------------------------------------
ENV PATH="/opt/venv/bin:${PATH}" \
VIRTUAL_ENV="/opt/venv"
EXPOSE ${APP_PORT}
# ------------------------------------------------------------
# 11. 日志目录
# ------------------------------------------------------------
RUN mkdir -p /data/webapps/lessie_sourcing_agents/logs
ENV LOG_DIR=/data/webapps/lessie_sourcing_agents/logs
VOLUME ["/data/webapps/lessie_sourcing_agents/logs"]
# ------------------------------------------------------------
# 12. Entrypoint
# ------------------------------------------------------------
COPY scripts/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
ENV LOG_DIR=/data/webapps/lessie_sourcing_agents/logs
VOLUME ["/data/webapps/lessie_sourcing_agents/logs"]
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]