7eab4bc508
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 38s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
AI Code Review / AI Code Review (pull_request) Successful in 1m13s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m31s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 1m40s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 1m49s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 2m7s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m24s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 3m24s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 3m23s
CI/CD Pipeline / Validate - Code Quality (pull_request) Successful in 7m19s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 9m9s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / Integration Tests (pull_request) Successful in 4m59s
CI/CD Pipeline / CI Gate (pull_request) Successful in 6s
ACR Cleanup / ACR Image Cleanup (pull_request_target) Successful in 53s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 1m14s
PR #1360 introduced FFmpeg-based frame extraction as fallback for cover generation, but the API Docker image did not include FFmpeg binary. Worker image already has it via worker-base-runtime, but API image only had libpq5 in its runtime stage. This caused subprocess calls to fail with FileNotFoundError when the cover generation tried to use _extract_frames_with_ffmpeg fallback. Fix: add ffmpeg to apt-get install in runtime stage of api.Dockerfile.
89 lines
3.2 KiB
Docker
Executable File
89 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 运行时依赖,ffmpeg 用于封面兜底取帧)
|
||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||
libpq5 \
|
||
ffmpeg \
|
||
&& 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"]
|