faf018030f
CI/CD Pipeline / Check if frontend-only change (push) Has been skipped
CI/CD Pipeline / Build Production API Image (push) Has been skipped
CI/CD Pipeline / Build Production Web Image (push) Has been skipped
CI/CD Pipeline / Build Production Worker Image (push) Has been skipped
CI/CD Pipeline / Deploy Production (push) Has been skipped
CI/CD Pipeline / Production Browser E2E (push) Has been skipped
CI/CD Pipeline / Build Staging Web Image (push) Successful in 45s
CI/CD Pipeline / Unit Tests (push) Successful in 2m22s
CI/CD Pipeline / Frontend Lint (push) Successful in 2m45s
CI/CD Pipeline / Build Staging API Image (push) Successful in 2m51s
CI/CD Pipeline / Frontend Unit Tests (push) Failing after 2m21s
CI/CD Pipeline / Validate Code Quality And Tests (push) Failing after 3m2s
CI/CD Pipeline / Integration Tests (push) Failing after 1m3s
CI/CD Pipeline / Build Staging Worker Image (push) Has been cancelled
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (push) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (push) Has been cancelled
CI/CD Pipeline / ACR Image Cleanup (push) Has been cancelled
Co-authored-by: xiaoxia <dev@xiaoxiajianji.com> Co-committed-by: xiaoxia <dev@xiaoxiajianji.com>
88 lines
3.2 KiB
Docker
Executable File
88 lines
3.2 KiB
Docker
Executable File
# ============================================================
|
||
# API Dockerfile - FastAPI 应用
|
||
# 优化:多阶段构建 + pip cache mount + 依赖分层缓存
|
||
# ============================================================
|
||
|
||
# ==================== Builder 阶段 ====================
|
||
FROM git.xiaoxiajianji.com/xiaoxia/base/python:3.12-slim AS builder
|
||
|
||
# 使用阿里云镜像加速
|
||
RUN sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \
|
||
sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list 2>/dev/null || true
|
||
|
||
# 安装编译依赖(仅 builder 需要)
|
||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||
gcc \
|
||
libpq-dev \
|
||
&& rm -rf /var/lib/apt/lists/*
|
||
|
||
# 创建虚拟环境
|
||
RUN python -m venv /opt/venv
|
||
ENV PATH="/opt/venv/bin:$PATH"
|
||
|
||
WORKDIR /tmp
|
||
|
||
# ---- 依赖分层:基础依赖(变化少,缓存命中率高)----
|
||
COPY requirements-base.txt /tmp/requirements-base.txt
|
||
|
||
RUN --mount=type=cache,target=/root/.cache/pip,sharing=locked \
|
||
pip install --no-cache-dir -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com \
|
||
-r /tmp/requirements-base.txt \
|
||
&& rm /tmp/requirements-base.txt
|
||
|
||
# ---- 依赖分层:业务依赖(变化频繁)----
|
||
COPY requirements.txt /tmp/requirements.txt
|
||
|
||
RUN --mount=type=cache,target=/root/.cache/pip,sharing=locked \
|
||
pip install --no-cache-dir -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com \
|
||
-r /tmp/requirements.txt \
|
||
&& rm /tmp/requirements.txt
|
||
|
||
# ---- Python 依赖瘦身 ----
|
||
RUN find /opt/venv -name "*.so" -type f -exec strip --strip-all {} \; 2>/dev/null || true
|
||
RUN find /opt/venv -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null; \
|
||
find /opt/venv -name "*.pyc" -delete 2>/dev/null || true
|
||
|
||
# ==================== Runtime 阶段 ====================
|
||
FROM git.xiaoxiajianji.com/xiaoxia/base/python:3.12-slim AS runtime
|
||
|
||
# 构建参数:版本号(CI 传入 commit hash)
|
||
ARG APP_VERSION=dev
|
||
|
||
# 使用阿里云镜像加速
|
||
RUN sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \
|
||
sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list 2>/dev/null || true
|
||
|
||
# 只装运行时需要的库(libpq5 是 psycopg2 运行时依赖)
|
||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||
libpq5 \
|
||
&& rm -rf /var/lib/apt/lists/*
|
||
|
||
# 从 builder 复制虚拟环境
|
||
COPY --from=builder /opt/venv /opt/venv
|
||
|
||
# 设置工作目录
|
||
WORKDIR /app
|
||
|
||
# 复制应用代码
|
||
COPY apps/api/ /app/apps/api/
|
||
COPY packages/ /app/packages/
|
||
COPY alembic.ini /app/alembic.ini
|
||
COPY migrations/ /app/migrations/
|
||
COPY alembic/ /app/alembic/
|
||
COPY scripts/ /app/scripts/
|
||
|
||
# 设置环境变量
|
||
ENV PATH="/opt/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
||
ENV PYTHONPATH=/app
|
||
ENV PYTHONUNBUFFERED=1
|
||
ENV APP_VERSION=$APP_VERSION
|
||
|
||
# 健康检查
|
||
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
||
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health', timeout=5)"
|
||
|
||
# API 入口点
|
||
WORKDIR /app/apps/api
|
||
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|