perf(docker): 优化 Docker 构建性能并启用缓存卷支持

- 在后端 Dockerfile 中实现分层缓存,先复制 pom.xml 文件再下载依赖
- 在前端 Dockerfile 中实现 npm 依赖缓存卷,提升构建效率
- 在网关 Dockerfile 中实现 Maven 依赖缓存卷,减少重复下载
- 在 Makefile 中启用 Docker BuildKit 支持缓存卷等高级功能
- 使用离线模式编译避免网络请求,加快构建速度
- 优化 COPY 操作顺序以更好利用 Docker 层缓存机制
This commit is contained in:
2026-01-30 11:17:40 +08:00
parent 0b69845a29
commit c51cd2b6e4
4 changed files with 71 additions and 9 deletions

View File

@@ -2,12 +2,21 @@ FROM node:20-alpine AS builder
WORKDIR /app
# 配置 npm 淘宝镜像
RUN npm config set registry https://registry.npmmirror.com
# 先复制 package.json 和 package-lock.json,利用 Docker 层缓存
COPY frontend/package.json frontend/package-lock.json* ./
# 使用缓存卷安装依赖(不复制源代码时即可缓存依赖层)
RUN --mount=type=cache,target=/root/.npm \
if [ -f package-lock.json ]; then npm ci; else npm install; fi
# 复制所有源代码
COPY frontend ./
# 配置 npm 淘宝镜像
RUN npm config set registry https://registry.npmmirror.com && \
if [ -f package-lock.json ]; then npm ci; else npm install; fi && \
npm run build
# 构建(使用缓存的依赖)
RUN npm run build
FROM nginx:1.29 AS runner