Spring Boot4先行篇:第一篇 Spring Boot 创建 Docker 镜像

日期 更新说明
2025年11月8日 初版发布

前言

Spring Boot4预计年底 11 月份发布,目前截止文章发布已经发布到了4.0.0 RC2版本;本文作为Spring Boo4专题学习预览篇,带你一起聊聊 Spring Boot4打包Docker镜像相关内容。

前置准备

  1. 需要搭建或者准备好 Spring Boot 项目,手动推荐搭建:start.spring.io/

笔者的 Spring Boot 项目 Github 链接:github.com/will-we/blo...

  1. 搭建docker环境

传统 Docker 构建

  1. Dockerfile内容如下
bash 复制代码
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"]
  1. 手动构建镜像:
erlang 复制代码
 docker build -t springboot4-samples:latest .

如下图:

  1. 启动镜像
arduino 复制代码
docker run -p 8080:8080 springboot4-samples

日志如下:

SpringBoot插件构建

arduino 复制代码
mvn spring-boot:build-image

构建日志如下:

less 复制代码
[INFO] Replacing main artifact D:\Space\Git\gh\will-we\blog\java\springboot4-samples\target\springboot4-samples-0.0.1.jar with repackaged archive, adding nested dependencies in BOOT-INF/.
[INFO] The original artifact has been renamed to D:\Space\Git\gh\will-we\blog\java\springboot4-samples\target\springboot4-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 插件构建

实际上发生的事情:

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

阶段分析

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

Docker 镜像推送

推送阿里云镜像仓库

bash 复制代码
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 镜像仓库

bash 复制代码
docker tag springboot4-samples:0.0.1 weirson/springboot4-samples:0.0.1
docker push weirson/springboot4-samples:0.0.1
相关推荐
Q_Q19632884753 小时前
python+django/flask基于协同过滤算法的理财产品推荐系统
spring boot·python·django·flask·node.js·php
码起来呗3 小时前
基于Spring Boot的乡村拼车小程序的设计与实现-项目分享
spring boot·后端·小程序
亚林瓜子5 小时前
Spring中的异步任务(CompletableFuture版)
java·spring boot·spring·async·future·异步
小蒜学长7 小时前
springboot基于Java的校园导航微信小程序的设计与实现(代码+数据库+LW)
java·spring boot·后端·微信小程序
Q_Q19632884757 小时前
python+django/flask基于深度学习的个性化携程美食数据推荐系统
spring boot·python·深度学习·django·flask·node.js·php
neoooo7 小时前
⚙️ Spring Boot × @RequiredArgsConstructor:写出最干净的依赖注入代码
spring boot·后端·spring
Q_Q5110082858 小时前
python+django/flask的篮球馆/足球场地/运动场地预约系统
spring boot·python·django·flask·node.js·php
Q_Q5110082859 小时前
python+django/flask的城市供水管网爆管预警系统-数据可视化
spring boot·python·django·flask·node.js·php
JosieBook10 小时前
【SpringBoot】31 核心功能 - 单元测试 - JUnit5 单元测试中的断言机制——验证你的代码是否按预期执行了
spring boot·单元测试·log4j