Spring Boot4先行篇:第一篇 Spring Boot 创建 Docker 镜像
日期更新说明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 .如下图:https://p0-xtjj-private.juejin.cn/tos-cn-i-73owjymdk6/970ec75f402c405193ac972e99b95579~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAgd2lsbF93ZQ==:q75.awebp?policy=eyJ2bSI6MywidWlkIjoiMzMzMzM3NDk4NTYzODk0MSJ9&rk3s=e9ecf3d6&x-orig-authkey=f32326d3454f2ac7e96d3d06cdbb035152127018&x-orig-expires=1762663705&x-orig-sign=pxRL5bnXBqiDsIubZCk5UatD5V4%3D
[*]启动镜像
docker run -p 8080:8080 springboot4-samples日志如下:
https://p0-xtjj-private.juejin.cn/tos-cn-i-73owjymdk6/120f0f9597c94675aad25dec5c96bbd8~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAgd2lsbF93ZQ==:q75.awebp?policy=eyJ2bSI6MywidWlkIjoiMzMzMzM3NDk4NTYzODk0MSJ9&rk3s=e9ecf3d6&x-orig-authkey=f32326d3454f2ac7e96d3d06cdbb035152127018&x-orig-expires=1762663706&x-orig-sign=0ACCvxElx%2FlJOGzUdf6OEsxLiqM%3D
SpringBoot插件构建
mvn spring-boot:build-image构建日志如下:
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/.
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
<<< spring-boot:4.0.0-SNAPSHOT:build-image (default-cli) < package @ springboot4-samples <<<
--- spring-boot:4.0.0-SNAPSHOT:build-image (default-cli) @ springboot4-samples ---
Building image 'docker.io/library/springboot4-samples:0.0.1'
> Pulling builder image 'docker.io/paketobuildpacks/builder-noble-java-tiny:latest' 100%
> Pulled builder image 'paketobuildpacks/builder-noble-java-tiny@sha256:cc76fed86425b3335e4defaaf87ff28207b0fa9ba56807ec800f62d42cb3543a'
> Pulling run image 'docker.io/paketobuildpacks/ubuntu-noble-run-tiny:0.0.38' for platform 'linux/amd64' 100%
> Pulled run image 'paketobuildpacks/ubuntu-noble-run-tiny@sha256:fff1b886c0e466792b5fabd716c078f061ae1d95d187b53074803d8a99533908'
> Executing lifecycle version v0.20.17
> Using build cache volume 'pack-cache-4e8131ef3603.build'
> Running creator
===> ANALYZING
Restoring data for SBOM from previous image
===> DETECTING
target distro name/version labels not found, reading /etc/os-release file
6 of 26 buildpacks participating
paketo-buildpacks/ca-certificates 3.10.4
paketo-buildpacks/bellsoft-liberica 11.4.1
paketo-buildpacks/syft 2.23.0
paketo-buildpacks/executable-jar 6.13.3
paketo-buildpacks/dist-zip 5.10.3
paketo-buildpacks/spring-boot 5.33.4
===> RESTORING
Restoring metadata for "paketo-buildpacks/ca-certificates:helper" from app image
Restoring metadata for "paketo-buildpacks/bellsoft-liberica:java-security-properties" from app image
Restoring metadata for "paketo-buildpacks/bellsoft-liberica:jre" from app image
Restoring metadata for "paketo-buildpacks/bellsoft-liberica:helper" from app image
Restoring metadata for "paketo-buildpacks/syft:syft" from cache
Restoring metadata for "paketo-buildpacks/spring-boot:helper" from app image
Restoring metadata for "paketo-buildpacks/spring-boot:spring-cloud-bindings" from app image
Restoring metadata for "paketo-buildpacks/spring-boot:web-application-type" from app image
Restoring data for "paketo-buildpacks/syft:syft" from cache
Restoring data for "paketo-buildpacks/spring-boot:spring-cloud-bindings" from cache
Restoring data for SBOM from cache
===> BUILDING
target distro name/version labels not found, reading /etc/os-release file
Paketo Buildpack for CA Certificates 3.10.4
https://github.com/paketo-buildpacks/ca-certificates
Build Configuration:
$BP_EMBED_CERTS falseEmbed certificates into the image
$BP_ENABLE_RUNTIME_CERT_BINDING true Deprecated: Enable/disable certificate helper layer to add certs at runtime
$BP_RUNTIME_CERT_BINDING_DISABLEDfalseDisable certificate helper layer to add certs at runtime
Launch Helper: Reusing cached layer
Paketo Buildpack for BellSoft Liberica 11.4.1
https://github.com/paketo-buildpacks/bellsoft-liberica
Build Configuration:
$BP_JVM_JLINK_ARGS --no-man-pages --no-header-files --strip-debug --compress=1configure custom link arguments (--output must be omitted)
$BP_JVM_JLINK_ENABLED false enables running jlink tool to generate custom JRE
$BP_JVM_TYPE JRE the JVM type - JDK or JRE
$BP_JVM_VERSION 21 the Java version
Launch Configuration:
$BPL_DEBUG_ENABLED false enables Java remote debugging support
$BPL_DEBUG_PORT 8000 configure the remote debugging port
$BPL_DEBUG_SUSPEND false configure whether to suspend execution until a debugger has attached
$BPL_HEAP_DUMP_PATH write heap dumps on error to this path
$BPL_JAVA_NMT_ENABLED true enables Java Native Memory Tracking (NMT)
$BPL_JAVA_NMT_LEVEL summary configure level of NMT, summary or detail
$BPL_JFR_ARGS configure custom Java Flight Recording (JFR) arguments
$BPL_JFR_ENABLED false enables Java Flight Recording (JFR)
$BPL_JMX_ENABLED false enables Java Management Extensions (JMX)
$BPL_JMX_PORT 5000 configure the JMX port
$BPL_JVM_HEAD_ROOM 0 the headroom in memory calculation
$BPL_JVM_LOADED_CLASS_COUNT35% of classes the number of loaded classes in memory calculation
$BPL_JVM_THREAD_COUNT 250 the number of threads in memory calculation
$JAVA_TOOL_OPTIONS the JVM launch flags
Using Java version 17 extracted from MANIFEST.MF
BellSoft Liberica JRE 17.0.17: Reusing cached layer
Launch Helper: Reusing cached layer
Java Security Properties: Reusing cached layer
Paketo Buildpack for Syft 2.23.0
https://github.com/paketo-buildpacks/syft
Paketo Buildpack for Executable JAR 6.13.3
https://github.com/paketo-buildpacks/executable-jar
Class Path: Contributing to layer
Writing env/CLASSPATH.delim
Writing env/CLASSPATH.prepend
Process types:
executable-jar: java org.springframework.boot.loader.launch.JarLauncher (direct)
task: java org.springframework.boot.loader.launch.JarLauncher (direct)
web: java org.springframework.boot.loader.launch.JarLauncher (direct)
Paketo Buildpack for Spring Boot 5.33.4
https://github.com/paketo-buildpacks/spring-boot
Build Configuration:
$BPL_JVM_CDS_ENABLED falsewhether to enable CDS optimizations at runtime
$BPL_SPRING_AOT_ENABLED falsewhether to enable Spring AOT at runtime
$BP_JVM_CDS_ENABLED falsewhether to enable CDS & perform JVM training run
$BP_SPRING_AOT_ENABLED falsewhether to enable Spring AOT
$BP_SPRING_CLOUD_BINDINGS_DISABLED falsewhether to contribute Spring Boot cloud bindings support
$BP_SPRING_CLOUD_BINDINGS_VERSION 1 default version of Spring Cloud Bindings library to contribute
Launch Configuration:
$BPL_SPRING_CLOUD_BINDINGS_DISABLEDfalsewhether to auto-configure Spring Boot environment properties from bindings
$BPL_SPRING_CLOUD_BINDINGS_ENABLED true Deprecated - whether to auto-configure Spring Boot environment properties from bindings
Creating slices from layers index
dependencies (17.3 MB)
spring-boot-loader (457.5 KB)
snapshot-dependencies (3.2 MB)
application (111.2 KB)
Spring Cloud Bindings 2.0.4: Reusing cached layer
Web Application Type: Reusing cached layer
Launch Helper: Reusing cached layer
4 application slices
Image labels:
org.opencontainers.image.title
org.opencontainers.image.version
org.springframework.boot.version
===> EXPORTING
Reusing layer 'paketo-buildpacks/ca-certificates:helper'
Reusing layer 'paketo-buildpacks/bellsoft-liberica:helper'
Reusing layer 'paketo-buildpacks/bellsoft-liberica:java-security-properties'
Reusing layer 'paketo-buildpacks/bellsoft-liberica:jre'
Reusing layer 'paketo-buildpacks/executable-jar:classpath'
Reusing layer 'paketo-buildpacks/spring-boot:helper'
Reusing layer 'paketo-buildpacks/spring-boot:spring-cloud-bindings'
Reusing layer 'paketo-buildpacks/spring-boot:web-application-type'
Reusing layer 'buildpacksio/lifecycle:launch.sbom'
Reused 5/5 app layer(s)
Reusing layer 'buildpacksio/lifecycle:launcher'
Reusing layer 'buildpacksio/lifecycle:config'
Reusing layer 'buildpacksio/lifecycle:process-types'
Adding label 'io.buildpacks.lifecycle.metadata'
Adding label 'io.buildpacks.build.metadata'
Adding label 'io.buildpacks.project.metadata'
Adding label 'org.opencontainers.image.title'
Adding label 'org.opencontainers.image.version'
Adding label 'org.springframework.boot.version'
Setting default process type 'web'
Saving docker.io/library/springboot4-samples:0.0.1...
*** Images (f85eff5507fe):
docker.io/library/springboot4-samples:0.0.1
Reusing cache layer 'paketo-buildpacks/syft:syft'
Adding cache layer 'paketo-buildpacks/syft:syft'
Reusing cache layer 'paketo-buildpacks/spring-boot:spring-cloud-bindings'
Adding cache layer 'paketo-buildpacks/spring-boot:spring-cloud-bindings'
Reusing cache layer 'buildpacksio/lifecycle:cache.sbom'
Adding cache layer 'buildpacksio/lifecycle:cache.sbom'
Successfully built image 'docker.io/library/springboot4-samples:0.0.1'
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time:01:19 min
Finished at: 2025-11-08T10:20:16+08:00
------------------------------------------------------------------------查看下本地镜像
https://p0-xtjj-private.juejin.cn/tos-cn-i-73owjymdk6/91aa6d7b2e1b4932bf92e2a5306d5663~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAgd2lsbF93ZQ==:q75.awebp?policy=eyJ2bSI6MywidWlkIjoiMzMzMzM3NDk4NTYzODk0MSJ9&rk3s=e9ecf3d6&x-orig-authkey=f32326d3454f2ac7e96d3d06cdbb035152127018&x-orig-expires=1762663705&x-orig-sign=nAqAwYFfp78bixvwfFiDyXDqXuU%3D
构建对比分析
SpringBoot 插件构建
实际上发生的事情:
[*]打包应用(Jar/War)
[*]
[*]先执行 package 阶段,生成一个可执行的 fat jar。
[*]调用 Cloud Native Buildpacks(CNB)
[*]
[*]Spring Boot Plugin 内部调用 Paketo Buildpacks(官方推荐)。
[*]构建镜像(通过 lifecycle)
[*]
[*]使用 CNB 的 Lifecycle 阶段:detect → analyze → build → export。
[*]生成 OCI 镜像
[*]
[*]输出一个可直接运行的 Docker 镜像(通常是 docker.io/library/:)。
[*]推送(可选)
[*]
[*]若配置了 publish=true,则自动推送到远程镜像仓库。
https://p0-xtjj-private.juejin.cn/tos-cn-i-73owjymdk6/7e93ae37b50a432698303cd8dd8aabfc~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAgd2lsbF93ZQ==:q75.awebp?policy=eyJ2bSI6MywidWlkIjoiMzMzMzM3NDk4NTYzODk0MSJ9&rk3s=e9ecf3d6&x-orig-authkey=f32326d3454f2ac7e96d3d06cdbb035152127018&x-orig-expires=1762663705&x-orig-sign=g4USfWi9sHDBykBBsM%2F%2B79z6C2M%3D
阶段分析
阶段作用Detect检测是否有合适的 buildpack 来构建该项目(Spring Boot → Java buildpack)Analyze分析缓存、上一次构建产物Build运行 buildpacks 构建层(JDK层、依赖层、应用层)Export导出成最终 OCI 镜像(可直接 docker run)对比项BuildpacksDockerfile构建语言声明式、自动检测命令式、需人工编写优化自动层缓存、分层优化手动优化层安全性由社区维护的基础镜像需要自己维护适合场景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.1https://p0-xtjj-private.juejin.cn/tos-cn-i-73owjymdk6/69f70a1ef9f64559b0f058e19728e3b2~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAgd2lsbF93ZQ==:q75.awebp?policy=eyJ2bSI6MywidWlkIjoiMzMzMzM3NDk4NTYzODk0MSJ9&rk3s=e9ecf3d6&x-orig-authkey=f32326d3454f2ac7e96d3d06cdbb035152127018&x-orig-expires=1762663705&x-orig-sign=XFZfXvpARbUTU4ABACBcXP3J0NI%3D
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! 新版吗?好像是停更了吧。 很好很强大我过来先占个楼 待编辑 谢谢分享,试用一下
页:
[1]