refactor: separate frontend build from base image for faster incremental builds

This commit is contained in:
fawney19
2025-12-19 16:02:38 +08:00
parent c2ddc6bd3c
commit 7f07122aea
3 changed files with 18 additions and 10 deletions

View File

@@ -1,7 +1,14 @@
# 运行镜像:从 base 提取产物到精简运行时 # 运行镜像:从 base 提取产物到精简运行时
# 构建命令: docker build -f Dockerfile.app -t aether-app:latest . # 构建命令: docker build -f Dockerfile.app -t aether-app:latest .
# 代码变更只需重建此镜像,无需重建 base
FROM aether-base:latest AS builder FROM aether-base:latest AS builder
WORKDIR /app
# 复制前端源码并构建
COPY frontend/ ./frontend/
RUN cd frontend && npm run build
# ==================== 运行时镜像 ==================== # ==================== 运行时镜像 ====================
FROM python:3.12-slim FROM python:3.12-slim
@@ -23,7 +30,7 @@ COPY --from=builder /usr/local/bin/gunicorn /usr/local/bin/
COPY --from=builder /usr/local/bin/uvicorn /usr/local/bin/ COPY --from=builder /usr/local/bin/uvicorn /usr/local/bin/
COPY --from=builder /usr/local/bin/alembic /usr/local/bin/ COPY --from=builder /usr/local/bin/alembic /usr/local/bin/
# 从 base 镜像复制前端产物 # 从 builder 阶段复制前端构建产物
COPY --from=builder /app/frontend/dist /usr/share/nginx/html COPY --from=builder /app/frontend/dist /usr/share/nginx/html
# 复制后端代码 # 复制后端代码

View File

@@ -1,6 +1,7 @@
# 构建镜像:编译环境 + 预编译的依赖 # 构建镜像:编译环境 + 预编译的依赖
# 用于 GitHub Actions CI 构建(不使用国内镜像源) # 用于 GitHub Actions CI 构建(不使用国内镜像源)
# 构建命令: docker build -f Dockerfile.base -t aether-base:latest . # 构建命令: docker build -f Dockerfile.base -t aether-base:latest .
# 只在 pyproject.toml 或 frontend/package*.json 变化时需要重建
FROM python:3.12-slim FROM python:3.12-slim
WORKDIR /app WORKDIR /app
@@ -19,11 +20,11 @@ RUN mkdir -p src && touch src/__init__.py && \
SETUPTOOLS_SCM_PRETEND_VERSION=0.1.0 pip install --no-cache-dir . && \ SETUPTOOLS_SCM_PRETEND_VERSION=0.1.0 pip install --no-cache-dir . && \
pip cache purge pip cache purge
# 前端构建 # 前端依赖(只安装,不构建
COPY frontend/ ./frontend/ COPY frontend/package*.json ./frontend/
RUN cd frontend && npm ci && npm run build && rm -rf node_modules && npm cache clean --force RUN cd frontend && npm ci
# 产物位置: # 产物位置:
# - Python 包: /usr/local/lib/python3.12/site-packages # - Python 包: /usr/local/lib/python3.12/site-packages
# - Python 可执行文件: /usr/local/bin # - Python 可执行文件: /usr/local/bin
# - 前端构建产物: /app/frontend/dist # - 前端 node_modules: /app/frontend/node_modules

View File

@@ -1,5 +1,6 @@
# 构建镜像:编译环境 + 预编译的依赖(国内镜像源版本) # 构建镜像:编译环境 + 预编译的依赖(国内镜像源版本)
# 构建命令: docker build -f Dockerfile.base.local -t aether-base:latest . # 构建命令: docker build -f Dockerfile.base.local -t aether-base:latest .
# 只在 pyproject.toml 或 frontend/package*.json 变化时需要重建
FROM python:3.12-slim FROM python:3.12-slim
WORKDIR /app WORKDIR /app
@@ -22,12 +23,11 @@ RUN mkdir -p src && touch src/__init__.py && \
SETUPTOOLS_SCM_PRETEND_VERSION=0.1.0 pip install --no-cache-dir . && \ SETUPTOOLS_SCM_PRETEND_VERSION=0.1.0 pip install --no-cache-dir . && \
pip cache purge pip cache purge
# 前端构建使用淘宝镜像源) # 前端依赖(只安装,不构建使用淘宝镜像源)
COPY frontend/ ./frontend/ COPY frontend/package*.json ./frontend/
RUN cd frontend && npm config set registry https://registry.npmmirror.com && \ RUN cd frontend && npm config set registry https://registry.npmmirror.com && npm ci
npm ci && npm run build && rm -rf node_modules && npm cache clean --force
# 产物位置: # 产物位置:
# - Python 包: /usr/local/lib/python3.12/site-packages # - Python 包: /usr/local/lib/python3.12/site-packages
# - Python 可执行文件: /usr/local/bin # - Python 可执行文件: /usr/local/bin
# - 前端构建产物: /app/frontend/dist # - 前端 node_modules: /app/frontend/node_modules