| 日期 | 更新说明 |
|---|---|
| 2025年11月8日 | 初版发布 |
前言
Spring Boot4预计年底 11 月份发布,目前截止文章发布已经发布到了4.0.0 RC2版本;本文作为Spring Boo4专题学习预览篇,带你一起聊聊 Spring Boot4打包Docker镜像相关内容。
前置准备
- 需要搭建或者准备好 Spring Boot 项目,手动推荐搭建:https://start.spring.io/
笔者的 Spring Boot 项目 Github 链接:https://github.com/will-we/blog/tree/main/java/springboot4-samples
- 搭建
docker环境
传统 Docker 构建
Dockerfile内容如下
ARG RUNTIME_IMAGE=amazoncorretto:17.0.7-alpine FROM ${RUNTIME_IMAGE} AS runtime # 拷贝打包后的可执行 JAR COPY ./springboot4-samples-0.0.1.jar /app/app.jar WORKDIR /app # Spring Boot 默认端口 EXPOSE 8080 # 可通过环境变量调整运行参数/激活配置文件 ENV SPRING_PROFILES_ACTIVE=default JAVA_TOOL_OPTIONS="" # 直接运行可执行 JAR ENTRYPOINT ["java","-jar","/app/app.jar"]
- 手动构建镜像:
docker build -t springboot4-samples:latest .
如下图:
- 启动镜像
docker run -p 8080:8080 springboot4-samples
日志如下:

SpringBoot插件构建
mvn spring-boot:build-image
构建日志如下:
[INFO] Replacing main artifact D:SpaceGitghwill-weblogjavaspringboot4-samplestargetspringboot4-samples-0.0.1.jar with repackaged archive, adding nested dependencies in BOOT-INF/. [INFO] The original artifact has been renamed to D:SpaceGitghwill-weblogjavaspringboot4-samplestargetspringboot4-samples-0.0.1.jar.original [INFO] [INFO] <<< spring-boot:4.0.0-SNAPSHOT:build-image (default-cli) < package @ springboot4-samples <<< [INFO] [INFO] [INFO] --- spring-boot:4.0.0-SNAPSHOT:build-image (default-cli) @ springboot4-samples --- [INFO] Building image 'docker.io/library/springboot4-samples:0.0.1' [INFO] [INFO] > Pulling builder image 'docker.io/paketobuildpacks/builder-noble-java-tiny:latest' 100% [INFO] > Pulled builder image 'paketobuildpacks/builder-noble-java-tiny@sha256:cc76fed86425b3335e4defaaf87ff28207b0fa9ba56807ec800f62d42cb3543a' [INFO] > Pulling run image 'docker.io/paketobuildpacks/ubuntu-noble-run-tiny:0.0.38' for platform 'linux/amd64' 100% [INFO] > Pulled run image 'paketobuildpacks/ubuntu-noble-run-tiny@sha256:fff1b886c0e466792b5fabd716c078f061ae1d95d187b53074803d8a99533908' [INFO] > Executing lifecycle version v0.20.17 [INFO] > Using build cache volume 'pack-cache-4e8131ef3603.build' [INFO] [INFO] > Running creator [INFO] [creator] ===> ANALYZING [INFO] [creator] Restoring data for SBOM from previous image [INFO] [creator] ===> DETECTING [INFO] [creator] target distro name/version labels not found, reading /etc/os-release file [INFO] [creator] 6 of 26 buildpacks participating [INFO] [creator] paketo-buildpacks/ca-certificates 3.10.4 [INFO] [creator] paketo-buildpacks/bellsoft-liberica 11.4.1 [INFO] [creator] paketo-buildpacks/syft 2.23.0 [INFO] [creator] paketo-buildpacks/executable-jar 6.13.3 [INFO] [creator] paketo-buildpacks/dist-zip 5.10.3 [INFO] [creator] paketo-buildpacks/spring-boot 5.33.4 [INFO] [creator] ===> RESTORING [INFO] [creator] Restoring metadata for "paketo-buildpacks/ca-certificates:helper" from app image [INFO] [creator] Restoring metadata for "paketo-buildpacks/bellsoft-liberica:java-security-properties" from app image [INFO] [creator] Restoring metadata for "paketo-buildpacks/bellsoft-liberica:jre" from app image [INFO] [creator] Restoring metadata for "paketo-buildpacks/bellsoft-liberica:helper" from app image [INFO] [creator] Restoring metadata for "paketo-buildpacks/syft:syft" from cache [INFO] [creator] Restoring metadata for "paketo-buildpacks/spring-boot:helper" from app image [INFO] [creator] Restoring metadata for "paketo-buildpacks/spring-boot:spring-cloud-bindings" from app image [INFO] [creator] Restoring metadata for "paketo-buildpacks/spring-boot:web-application-type" from app image [INFO] [creator] Restoring data for "paketo-buildpacks/syft:syft" from cache [INFO] [creator] Restoring data for "paketo-buildpacks/spring-boot:spring-cloud-bindings" from cache [INFO] [creator] Restoring data for SBOM from cache [INFO] [creator] ===> BUILDING [INFO] [creator] target distro name/version labels not found, reading /etc/os-release file [INFO] [creator] [INFO] [creator] Paketo Buildpack for CA Certificates 3.10.4 [INFO] [creator] https://github.com/paketo-buildpacks/ca-certificates [INFO] [creator] Build Configuration: [INFO] [creator] $BP_EMBED_CERTS false Embed certificates into the image [INFO] [creator] $BP_ENABLE_RUNTIME_CERT_BINDING true Deprecated: Enable/disable certificate helper layer to add certs at runtime [INFO] [creator] $BP_RUNTIME_CERT_BINDING_DISABLED false Disable certificate helper layer to add certs at runtime [INFO] [creator] Launch Helper: Reusing cached layer [INFO] [creator] [INFO] [creator] Paketo Buildpack for BellSoft Liberica 11.4.1 [INFO] [creator] https://github.com/paketo-buildpacks/bellsoft-liberica [INFO] [creator] Build Configuration: [INFO] [creator] $BP_JVM_JLINK_ARGS --no-man-pages --no-header-files --strip-debug --compress=1 configure custom link arguments (--output must be omitted) [INFO] [creator] $BP_JVM_JLINK_ENABLED false enables running jlink tool to generate custom JRE [INFO] [creator] $BP_JVM_TYPE JRE the JVM type - JDK or JRE [INFO] [creator] $BP_JVM_VERSION 21 the Java version [INFO] [creator] Launch Configuration: [INFO] [creator] $BPL_DEBUG_ENABLED false enables Java remote debugging support [INFO] [creator] $BPL_DEBUG_PORT 8000 configure the remote debugging port [INFO] [creator] $BPL_DEBUG_SUSPEND false configure whether to suspend execution until a debugger has attached [INFO] [creator] $BPL_HEAP_DUMP_PATH write heap dumps on error to this path [INFO] [creator] $BPL_JAVA_NMT_ENABLED true enables Java Native Memory Tracking (NMT) [INFO] [creator] $BPL_JAVA_NMT_LEVEL summary configure level of NMT, summary or detail [INFO] [creator] $BPL_JFR_ARGS configure custom Java Flight Recording (JFR) arguments [INFO] [creator] $BPL_JFR_ENABLED false enables Java Flight Recording (JFR) [INFO] [creator] $BPL_JMX_ENABLED false enables Java Management Extensions (JMX) [INFO] [creator] $BPL_JMX_PORT 5000 configure the JMX port [INFO] [creator] $BPL_JVM_HEAD_ROOM 0 the headroom in memory calculation [INFO] [creator] $BPL_JVM_LOADED_CLASS_COUNT 35% of classes the number of loaded classes in memory calculation [INFO] [creator] $BPL_JVM_THREAD_COUNT 250 the number of threads in memory calculation [INFO] [creator] $JAVA_TOOL_OPTIONS the JVM launch flags [INFO] [creator] Using Java version 17 extracted from MANIFEST.MF [INFO] [creator] BellSoft Liberica JRE 17.0.17: Reusing cached layer [INFO] [creator] Launch Helper: Reusing cached layer [INFO] [creator] Java Security Properties: Reusing cached layer [INFO] [creator] [INFO] [creator] Paketo Buildpack for Syft 2.23.0 [INFO] [creator] https://github.com/paketo-buildpacks/syft [INFO] [creator] [INFO] [creator] Paketo Buildpack for Executable JAR 6.13.3 [INFO] [creator] https://github.com/paketo-buildpacks/executable-jar [INFO] [creator] Class Path: Contributing to layer [INFO] [creator] Writing env/CLASSPATH.delim [INFO] [creator] Writing env/CLASSPATH.prepend [INFO] [creator] Process types: [INFO] [creator] executable-jar: java org.springframework.boot.loader.launch.JarLauncher (direct) [INFO] [creator] task: java org.springframework.boot.loader.launch.JarLauncher (direct) [INFO] [creator] web: java org.springframework.boot.loader.launch.JarLauncher (direct) [INFO] [creator] [INFO] [creator] Paketo Buildpack for Spring Boot 5.33.4 [INFO] [creator] https://github.com/paketo-buildpacks/spring-boot [INFO] [creator] Build Configuration: [INFO] [creator] $BPL_JVM_CDS_ENABLED false whether to enable CDS optimizations at runtime [INFO] [creator] $BPL_SPRING_AOT_ENABLED false whether to enable Spring AOT at runtime [INFO] [creator] $BP_JVM_CDS_ENABLED false whether to enable CDS & perform JVM training run [INFO] [creator] $BP_SPRING_AOT_ENABLED false whether to enable Spring AOT [INFO] [creator] $BP_SPRING_CLOUD_BINDINGS_DISABLED false whether to contribute Spring Boot cloud bindings support [INFO] [creator] $BP_SPRING_CLOUD_BINDINGS_VERSION 1 default version of Spring Cloud Bindings library to contribute [INFO] [creator] Launch Configuration: [INFO] [creator] $BPL_SPRING_CLOUD_BINDINGS_DISABLED false whether to auto-configure Spring Boot environment properties from bindings [INFO] [creator] $BPL_SPRING_CLOUD_BINDINGS_ENABLED true Deprecated - whether to auto-configure Spring Boot environment properties from bindings [INFO] [creator] Creating slices from layers index [INFO] [creator] dependencies (17.3 MB) [INFO] [creator] spring-boot-loader (457.5 KB) [INFO] [creator] snapshot-dependencies (3.2 MB) [INFO] [creator] application (111.2 KB) [INFO] [creator] Spring Cloud Bindings 2.0.4: Reusing cached layer [INFO] [creator] Web Application Type: Reusing cached layer [INFO] [creator] Launch Helper: Reusing cached layer [INFO] [creator] 4 application slices [INFO] [creator] Image labels: [INFO] [creator] org.opencontainers.image.title [INFO] [creator] org.opencontainers.image.version [INFO] [creator] org.springframework.boot.version [INFO] [creator] ===> EXPORTING [INFO] [creator] Reusing layer 'paketo-buildpacks/ca-certificates:helper' [INFO] [creator] Reusing layer 'paketo-buildpacks/bellsoft-liberica:helper' [INFO] [creator] Reusing layer 'paketo-buildpacks/bellsoft-liberica:java-security-properties' [INFO] [creator] Reusing layer 'paketo-buildpacks/bellsoft-liberica:jre' [INFO] [creator] Reusing layer 'paketo-buildpacks/executable-jar:classpath' [INFO] [creator] Reusing layer 'paketo-buildpacks/spring-boot:helper' [INFO] [creator] Reusing layer 'paketo-buildpacks/spring-boot:spring-cloud-bindings' [INFO] [creator] Reusing layer 'paketo-buildpacks/spring-boot:web-application-type' [INFO] [creator] Reusing layer 'buildpacksio/lifecycle:launch.sbom' [INFO] [creator] Reused 5/5 app layer(s) [INFO] [creator] Reusing layer 'buildpacksio/lifecycle:launcher' [INFO] [creator] Reusing layer 'buildpacksio/lifecycle:config' [INFO] [creator] Reusing layer 'buildpacksio/lifecycle:process-types' [INFO] [creator] Adding label 'io.buildpacks.lifecycle.metadata' [INFO] [creator] Adding label 'io.buildpacks.build.metadata' [INFO] [creator] Adding label 'io.buildpacks.project.metadata' [INFO] [creator] Adding label 'org.opencontainers.image.title' [INFO] [creator] Adding label 'org.opencontainers.image.version' [INFO] [creator] Adding label 'org.springframework.boot.version' [INFO] [creator] Setting default process type 'web' [INFO] [creator] Saving docker.io/library/springboot4-samples:0.0.1... [INFO] [creator] *** Images (f85eff5507fe): [INFO] [creator] docker.io/library/springboot4-samples:0.0.1 [INFO] [creator] Reusing cache layer 'paketo-buildpacks/syft:syft' [INFO] [creator] Adding cache layer 'paketo-buildpacks/syft:syft' [INFO] [creator] Reusing cache layer 'paketo-buildpacks/spring-boot:spring-cloud-bindings' [INFO] [creator] Adding cache layer 'paketo-buildpacks/spring-boot:spring-cloud-bindings' [INFO] [creator] Reusing cache layer 'buildpacksio/lifecycle:cache.sbom' [INFO] [creator] Adding cache layer 'buildpacksio/lifecycle:cache.sbom' [INFO] [INFO] Successfully built image 'docker.io/library/springboot4-samples:0.0.1' [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:19 min [INFO] Finished at: 2025-11-08T10:20:16+08:00 [INFO] ------------------------------------------------------------------------
查看下本地镜像

构建对比分析
SpringBoot 插件构建
实际上发生的事情:
- 打包应用(Jar/War)
-
- 先执行
package阶段,生成一个可执行的 fat jar。
- 先执行
- 调用 Cloud Native Buildpacks(CNB)
-
- Spring Boot Plugin 内部调用 Paketo Buildpacks(官方推荐)。
- 构建镜像(通过 lifecycle)
-
- 使用 CNB 的 Lifecycle 阶段:
detect → analyze → build → export。
- 使用 CNB 的 Lifecycle 阶段:
- 生成 OCI 镜像
-
- 输出一个可直接运行的 Docker 镜像(通常是
docker.io/library/<artifactId>:<version>)。
- 输出一个可直接运行的 Docker 镜像(通常是
- 推送(可选)
-
- 若配置了
publish=true,则自动推送到远程镜像仓库。
- 若配置了

阶段分析
| 阶段 | 作用 |
|---|---|
| Detect | 检测是否有合适的 buildpack 来构建该项目(Spring Boot → Java buildpack) |
| Analyze | 分析缓存、上一次构建产物 |
| Build | 运行 buildpacks 构建层(JDK层、依赖层、应用层) |
| Export | 导出成最终 OCI 镜像(可直接 docker run) |
| 对比项 | Buildpacks | Dockerfile |
|---|---|---|
| 构建语言 | 声明式、自动检测 | 命令式、需人工编写 |
| 优化 | 自动层缓存、分层优化 | 手动优化层 |
| 安全性 | 由社区维护的基础镜像 | 需要自己维护 |
| 适合场景 | Spring Boot / Cloud Native 架构 | 需要复杂构建逻辑的项目 |
Docker 镜像推送
推送阿里云镜像仓库
docker tag springboot4-samples:0.0.1 registry.cn-hangzhou.aliyuncs.com/weirson/springboot4-samples:0.0.1 docker push registry.cn-hangzhou.aliyuncs.com/weirson/springboot4-samples:0.0.1
推送 Docker 镜像仓库
docker tag springboot4-samples:0.0.1 weirson/springboot4-samples:0.0.1 docker push weirson/springboot4-samples:0.0.1
