Files
Edu/services/ai/Dockerfile
2026-07-10 18:57:39 +08:00

60 lines
1.7 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.
# 多阶段构建G1 多阶段强制规则)
# Stage 1: builder - 安装依赖 + 生成 proto 代码
# Stage 2: runtime - 精简运行时镜像
# === Stage 1: builder ===
FROM python:3.12-slim AS builder
WORKDIR /app
# 安装 uv快速依赖管理
RUN pip install --no-cache-dir uv
# 先复制依赖文件,利用 Docker layer cache
COPY pyproject.toml ./
# 安装生产依赖(含 dev 依赖用于生成 proto
RUN uv sync --all-extras
# 复制源码
COPY src ./src
# 生成 gRPC 代码(如果 proto_gen 不存在或需更新)
RUN uv run python -m grpc_tools.protoc \
-I /app/proto \
--python_out=src/ai/proto_gen \
--grpc_python_out=src/ai/proto_gen \
/app/proto/ai.proto || echo "proto gen skipped (no proto dir)"
# === Stage 2: runtime ===
FROM python:3.12-slim AS runtime
WORKDIR /app
# 安装运行时系统依赖curl 用于健康检查)
RUN apt-get update && apt-get install -y --no-install-recommends \
curl=7.88.* \
&& rm -rf /var/lib/apt/lists/*
# 从 builder 复制虚拟环境
COPY --from=builder /app/.venv /app/.venv
# 从 builder 复制源码(含生成的 proto 代码)
COPY --from=builder /app/src /app/src
# 环境变量
ENV PATH="/app/.venv/bin:$PATH" \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONPATH="/app/src"
# 暴露端口HTTP 3008 + gRPC 50058
EXPOSE 3008 50058
# 健康检查HTTP /healthz
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD curl -f http://localhost:3008/healthz || exit 1
# 启动命令uvicorn + gRPC server 由 main.py 内部启动)
CMD ["python", "-m", "uvicorn", "ai.main:app", "--host", "0.0.0.0", "--port", "3008"]