perf: 使用 BuildKit 缓存加速 Docker 构建

- 添加 # syntax=docker/dockerfile:1 启用 BuildKit 新语法
- apt-get 使用 --mount=type=cache 缓存包下载
- pip install 使用 --mount=type=cache 缓存依赖
- npm ci 使用 --mount=type=cache 缓存模块
- 添加 --no-install-recommends 减少安装包体积

缓存命中后构建时间从 ~40s 降至 ~1.5s
This commit is contained in:
fawney19
2026-01-30 15:03:58 +08:00
parent 5603c72f40
commit 16fb06ff4c
4 changed files with 40 additions and 30 deletions

View File

@@ -1,3 +1,4 @@
# syntax=docker/dockerfile:1
# 运行镜像:从 base 提取产物到精简运行时 # 运行镜像:从 base 提取产物到精简运行时
# 构建命令: docker build -f Dockerfile.app -t aether-app:latest . # 构建命令: docker build -f Dockerfile.app -t aether-app:latest .
# 用于 GitHub Actions CI官方源 # 用于 GitHub Actions CI官方源
@@ -9,13 +10,14 @@ RUN cd frontend && npm run build
# ==================== 运行时镜像 ==================== # ==================== 运行时镜像 ====================
FROM python:3.14-slim FROM python:3.14-slim
WORKDIR /app WORKDIR /app
# 运行时依赖(无 gcc/nodejs/npm # 运行时依赖(无 gcc/nodejs/npm,使用 BuildKit 缓存加速
RUN apt-get update && apt-get install -y \ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get update && apt-get install -y --no-install-recommends \
nginx \ nginx \
supervisor \ supervisor \
libpq5 \ libpq5 \
curl \ curl
&& rm -rf /var/lib/apt/lists/*
# 从 base 镜像复制 Python 包 # 从 base 镜像复制 Python 包
COPY --from=builder /usr/local/lib/python3.14/site-packages /usr/local/lib/python3.14/site-packages COPY --from=builder /usr/local/lib/python3.14/site-packages /usr/local/lib/python3.14/site-packages
# 只复制需要的 Python 可执行文件 # 只复制需要的 Python 可执行文件

View File

@@ -1,3 +1,4 @@
# syntax=docker/dockerfile:1
# 运行镜像:从 base 提取产物到精简运行时(国内镜像源版本) # 运行镜像:从 base 提取产物到精简运行时(国内镜像源版本)
# 构建命令: docker build -f Dockerfile.app.local -t aether-app:latest . # 构建命令: docker build -f Dockerfile.app.local -t aether-app:latest .
# 用于本地/国内服务器部署 # 用于本地/国内服务器部署
@@ -14,14 +15,15 @@ FROM python:3.14-slim
WORKDIR /app WORKDIR /app
# 运行时依赖(使用清华镜像源) # 运行时依赖(使用清华镜像源 + BuildKit 缓存加速
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list.d/debian.sources && \ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
apt-get update && apt-get install -y \ --mount=type=cache,target=/var/lib/apt,sharing=locked \
sed -i 's/deb.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 \
nginx \ nginx \
supervisor \ supervisor \
libpq5 \ libpq5 \
curl \ curl
&& rm -rf /var/lib/apt/lists/*
# 从 base 镜像复制 Python 包 # 从 base 镜像复制 Python 包
COPY --from=builder /usr/local/lib/python3.14/site-packages /usr/local/lib/python3.14/site-packages COPY --from=builder /usr/local/lib/python3.14/site-packages /usr/local/lib/python3.14/site-packages

View File

@@ -1,3 +1,4 @@
# syntax=docker/dockerfile:1
# 构建镜像:编译环境 + 预编译的依赖 # 构建镜像:编译环境 + 预编译的依赖
# 用于 GitHub Actions CI 构建(不使用国内镜像源) # 用于 GitHub Actions CI 构建(不使用国内镜像源)
# 构建命令: docker build -f Dockerfile.base -t aether-base:latest . # 构建命令: docker build -f Dockerfile.base -t aether-base:latest .
@@ -6,20 +7,22 @@ FROM python:3.14-slim
WORKDIR /app WORKDIR /app
# 构建工具 # 构建工具(使用 BuildKit 缓存加速)
RUN apt-get update && apt-get install -y \ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get update && apt-get install -y --no-install-recommends \
libpq-dev \ libpq-dev \
gcc \ gcc \
nodejs \ nodejs \
npm \ npm
&& rm -rf /var/lib/apt/lists/*
# Python 依赖 # Python 依赖(使用 BuildKit 缓存加速)
COPY pyproject.toml README.md ./ COPY pyproject.toml README.md ./
RUN mkdir -p src && touch src/__init__.py && \ RUN --mount=type=cache,target=/root/.cache/pip \
SETUPTOOLS_SCM_PRETEND_VERSION=0.1.0 pip install --no-cache-dir . && \ mkdir -p src && touch src/__init__.py && \
pip cache purge SETUPTOOLS_SCM_PRETEND_VERSION=0.1.0 pip install .
# 前端依赖(只安装,不构建) # 前端依赖(只安装,不构建,使用 BuildKit 缓存加速
COPY frontend/package*.json ./frontend/ COPY frontend/package*.json ./frontend/
RUN cd frontend && npm ci RUN --mount=type=cache,target=/root/.npm \
cd frontend && npm ci

View File

@@ -1,3 +1,4 @@
# syntax=docker/dockerfile:1
# 构建镜像:编译环境 + 预编译的依赖(国内镜像源版本) # 构建镜像:编译环境 + 预编译的依赖(国内镜像源版本)
# 构建命令: 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 变化时需要重建 # 只在 pyproject.toml 或 frontend/package*.json 变化时需要重建
@@ -5,24 +6,26 @@ FROM python:3.14-slim
WORKDIR /app WORKDIR /app
# 构建工具(使用清华镜像源) # 构建工具(使用清华镜像源 + BuildKit 缓存加速
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list.d/debian.sources && \ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
apt-get update && apt-get install -y \ --mount=type=cache,target=/var/lib/apt,sharing=locked \
sed -i 's/deb.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 \
libpq-dev \ libpq-dev \
gcc \ gcc \
nodejs \ nodejs \
npm \ npm
&& rm -rf /var/lib/apt/lists/*
# pip 镜像源 # pip 镜像源
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# Python 依赖 # Python 依赖(使用 BuildKit 缓存加速)
COPY pyproject.toml README.md ./ COPY pyproject.toml README.md ./
RUN mkdir -p src && touch src/__init__.py && \ RUN --mount=type=cache,target=/root/.cache/pip \
SETUPTOOLS_SCM_PRETEND_VERSION=0.1.0 pip install --no-cache-dir . && \ mkdir -p src && touch src/__init__.py && \
pip cache purge SETUPTOOLS_SCM_PRETEND_VERSION=0.1.0 pip install .
# 前端依赖(只安装,不构建,使用淘宝镜像源) # 前端依赖(只安装,不构建,使用淘宝镜像源 + BuildKit 缓存加速
COPY frontend/package*.json ./frontend/ COPY frontend/package*.json ./frontend/
RUN cd frontend && npm config set registry https://registry.npmmirror.com && npm ci RUN --mount=type=cache,target=/root/.npm \
cd frontend && npm config set registry https://registry.npmmirror.com && npm ci