Files
jenkins-pipeline/Dockerfile/python/lessie-sourcing-agents/Dockerfile
2025-12-22 11:12:04 +08:00

117 lines
4.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.

###############################################
# Stage 1: Builder (构建依赖 + venv
###############################################
FROM uswccr.ccs.tencentyun.com/lessie/python:3.12-slim AS builder
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
WORKDIR /data/webapps/lessie_sourcing_agents
# ------------------------------------------------------------
# 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 ./
ENV UV_PROJECT_ENVIRONMENT=/opt/venv
# ------------------------------------------------------------
# 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
ENV APP_ENV=local \
APP_PORT=${APP_PORT}
WORKDIR /data/webapps/lessie_sourcing_agents
# ------------------------------------------------------------
# 8. 拷贝 venv仅包含已安装依赖
# ------------------------------------------------------------
COPY --from=builder /opt/venv /opt/venv
# ------------------------------------------------------------
# 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
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]