From bd76264de278daa796e2f6b3297fa3920866de81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=82=A6?= Date: Fri, 12 Sep 2025 13:52:16 +0800 Subject: [PATCH] =?UTF-8?q?build(deploy):=20=E6=B7=BB=E5=8A=A0=E5=A4=9A?= =?UTF-8?q?=E9=98=B6=E6=AE=B5=20Dockerfile=20=E4=BB=A5=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E5=89=8D=E5=90=8E=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 引入一个新的多阶段 `Dockerfile.build`,用于将 Flutter 前端和 Spring Boot 后端的构建过程整合到单个 Docker 镜像中。 - 第一阶段使用 Flutter 镜像构建 Web 静态资源。 - 第二阶段使用 Maven 镜像编译 Java 后端应用。 - 最终阶段将前两阶段的产物(Web 资源和 JAR 包)复制到一个精简的 JRE 基础镜像中,以创建最终的运行时镜像。 此变更简化了部署流程,并显著减小了最终生产镜像的体积。`docker-compose.yml` 已同步更新以使用此新的构建文件。 --- deploy/open/Dockerfile.build | 53 ++++++++++++++++++++++++++++++++++ deploy/open/docker-compose.yml | 2 +- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 deploy/open/Dockerfile.build diff --git a/deploy/open/Dockerfile.build b/deploy/open/Dockerfile.build new file mode 100644 index 0000000..16b7717 --- /dev/null +++ b/deploy/open/Dockerfile.build @@ -0,0 +1,53 @@ +# Stage 1: Build the Flutter web client +FROM cirrusci/flutter:3.22.2 as flutter_builder +WORKDIR /app + +# Copy Flutter project files +COPY AINoval/pubspec.yaml AINoval/pubspec.lock ./AINoval/ +RUN cd AINoval && flutter pub get + +# Copy the rest of the Flutter project source code +COPY AINoval/ ./AINoval/ + +# Build the Flutter web application +RUN cd AINoval && flutter build web --release --web-renderer canvaskit + +# Stage 2: Build the Java Spring Boot server +FROM maven:3.9-eclipse-temurin-21 as java_builder +WORKDIR /app + +# Copy only the pom.xml to leverage Docker cache for dependencies +COPY AINovalServer/pom.xml ./AINovalServer/ +RUN mvn -f AINovalServer/pom.xml dependency:go-offline + +# Copy the rest of the server source code +COPY AINovalServer/ ./AINovalServer/ + +# Build the application, skipping tests to speed up the process +RUN mvn -f AINovalServer/pom.xml clean package -DskipTests + +# Stage 3: Create the final runtime image +FROM eclipse-temurin:21-jre + +ENV TZ=Asia/Shanghai \ + LANG=C.UTF-8 \ + LC_ALL=C.UTF-8 \ + SPRING_PROFILES_ACTIVE=prod \ + JVM_XMS=512m \ + JVM_XMX=512m + +# Fix JDK 21 reflective access for BigDecimal in Spring Data Mongo +ENV JAVA_TOOL_OPTIONS="--add-opens=java.base/java.math=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.time=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED" + +WORKDIR /app + +# Copy the server jar from the java_builder stage and rename it +COPY --from=java_builder /app/AINovalServer/target/ai-novel-server-0.0.1-SNAPSHOT.jar /app/ainoval-server.jar + +# Copy the built web assets from the flutter_builder stage +COPY --from=flutter_builder /app/AINoval/build/web/ /app/web/ + +EXPOSE 18080 + +# Serve the prebuilt web from filesystem via Spring static locations +CMD sh -c "java -Xms${JVM_XMS} -Xmx${JVM_XMX} -Dfile.encoding=UTF-8 -Dspring.web.resources.static-locations=file:/app/web/ -jar /app/ainoval-server.jar" diff --git a/deploy/open/docker-compose.yml b/deploy/open/docker-compose.yml index 0bb0dce..50fad64 100644 --- a/deploy/open/docker-compose.yml +++ b/deploy/open/docker-compose.yml @@ -19,7 +19,7 @@ services: image: ainoval/ainoval-server:latest build: context: .. - dockerfile: open/Dockerfile + dockerfile: open/Dockerfile.build container_name: ainoval-server env_file: - ./production.env