improve: use alibaba mirror for maven and npmmirror for pnpm

This commit is contained in:
2026-09-21 13:07:50 +08:00
parent a23e2f600a
commit 931d3ee1b7
3 changed files with 80 additions and 7 deletions

View File

@@ -3,12 +3,22 @@
# 构建:docker build -t lab-backend . # 构建:docker build -t lab-backend .
# 运行:docker run -p 8080:8080 -e SPRING_PROFILES_ACTIVE=mariadb \ # 运行:docker run -p 8080:8080 -e SPRING_PROFILES_ACTIVE=mariadb \
# -e DB_HOST=host.docker.internal -e DB_USER=lab -e DB_PASSWORD=lab123456 lab-backend # -e DB_HOST=host.docker.internal -e DB_USER=lab -e DB_PASSWORD=lab123456 lab-backend
#
# 国内加速:构建阶段使用阿里云 Maven 镜像(docker/maven-settings.xml)
# 如需走官方中央仓库:docker build --build-arg MAVEN_MIRROR=false -t lab-backend .
# ===================================================================== # =====================================================================
# ---------- 构建阶段 ---------- # ---------- 构建阶段 ----------
FROM docker.io/library/maven:3.9-eclipse-temurin-21 AS build FROM maven:3.9-eclipse-temurin-21 AS build
WORKDIR /build WORKDIR /build
# MAVEN_MIRROR=false 时跳过阿里云镜像配置,使用默认中央仓库
ARG MAVEN_MIRROR=true
COPY docker/maven-settings.xml /tmp/maven-settings.xml
RUN if [ "$MAVEN_MIRROR" = "true" ]; then \
mkdir -p /root/.m2 && cp /tmp/maven-settings.xml /root/.m2/settings.xml; \
fi
# 先只复制 pom,利用 Docker 层缓存预热依赖 # 先只复制 pom,利用 Docker 层缓存预热依赖
COPY pom.xml . COPY pom.xml .
RUN mvn -B -q -Dmaven.test.skip=true dependency:go-offline RUN mvn -B -q -Dmaven.test.skip=true dependency:go-offline
@@ -19,7 +29,7 @@ RUN mvn -B -q -Dmaven.test.skip=true package \
&& cp target/*.jar /build/app.jar && cp target/*.jar /build/app.jar
# ---------- 运行阶段 ---------- # ---------- 运行阶段 ----------
FROM docker.io/library/eclipse-temurin:21-jre FROM eclipse-temurin:21-jre
WORKDIR /opt/app WORKDIR /opt/app
# 以非 root 用户运行 # 以非 root 用户运行

57
docker/maven-settings.xml Normal file
View File

@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
仅供 Docker 镜像构建使用的 Maven 配置:
通过阿里云公共仓库代理加速国内依赖下载(镜像构建阶段复制到 /root/.m2/settings.xml)。
说明:
- mirrorOf 使用 * 表示所有仓库(含 central)都走阿里云公共仓库;
- 本地开发仍使用默认中央仓库,不受本文件影响;
- 如不需要镜像,可删除 Dockerfile 中的 COPY 指令。
-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<mirrors>
<mirror>
<id>aliyun-public</id>
<name>Aliyun Public Repository</name>
<url>https://maven.aliyun.com/repository/public</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>aliyun-repos</id>
<!-- 兜底仓库:部分插件/依赖在公共仓库未命中时可回退 -->
<repositories>
<repository>
<id>aliyun-central</id>
<url>https://maven.aliyun.com/repository/central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
<repository>
<id>aliyun-spring</id>
<url>https://maven.aliyun.com/repository/spring</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun-plugin</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>aliyun-repos</activeProfile>
</activeProfiles>
</settings>

View File

@@ -1,24 +1,30 @@
# ===================================================================== # =====================================================================
# 前端镜像:pnpm 构建静态资源 -> Nginx 托管(同时反代 /api 到后端) # 前端镜像:pnpm 构建静态资源 -> Nginx 托管(同时反代 /api 到后端)
# 构建:docker build -t lab-frontend ./frontend # 构建:docker build -t lab-frontend ./frontend
#
# 国内加速:npm/pnpm 使用 npmmirror(淘宝镜像)
# 如需走官方源:docker build --build-arg NPM_REGISTRY=https://registry.npmjs.org -t lab-frontend ./frontend
# ===================================================================== # =====================================================================
# ---------- 构建阶段 ---------- # ---------- 构建阶段 ----------
FROM docker.io/library/node:24-alpine AS build FROM node:24-alpine AS build
WORKDIR /app WORKDIR /app
# 使用与本地一致的 pnpm 主版本,锁定依赖保证可复现 # 使用 npmmirror 加速 npm 全局安装与依赖拉取
RUN npm install -g pnpm@11.3.0 ARG NPM_REGISTRY=https://registry.npmmirror.com
RUN npm config set registry "$NPM_REGISTRY" \
&& npm install -g pnpm@11.3.0 \
&& pnpm config set registry "$NPM_REGISTRY"
# 先装依赖,充分利用层缓存 # 先装依赖,充分利用层缓存
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./ COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
RUN pnpm install --frozen-lockfile RUN pnpm install --frozen-lockfile --registry="$NPM_REGISTRY"
COPY . . COPY . .
RUN pnpm build RUN pnpm build
# ---------- 运行阶段 ---------- # ---------- 运行阶段 ----------
FROM docker.io/library/nginx:1.27-alpine FROM nginx:1.27-alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/dist /usr/share/nginx/html COPY --from=build /app/dist /usr/share/nginx/html