From 005225cf079ead7486e124ab61348c0f49efc165 Mon Sep 17 00:00:00 2001 From: AlexanderWei <494120176@qq.com> Date: Wed, 5 Jun 2024 15:27:47 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=AE=8C=E6=88=90spring-boot-maven-plugin?= =?UTF-8?q?=20oci=20build?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/pom.xml b/pom.xml index 5bc273c..ef237dc 100755 --- a/pom.xml +++ b/pom.xml @@ -39,37 +39,6 @@ - - com.google.cloud.tools - jib-maven-plugin - 3.4.2 - - - eclipse-temurin:22.0.1_8-jre-ubi9-minimal - - - ${project.artifactId}:latest - - - com.alex.springbootbuilddocker.SpringBootBuildDockerApplication - USE_CURRENT_TIMESTAMP - - - - - - - - - - - - - - org.codehaus.mojo - exec-maven-plugin - 3.3.0 - -- Gitee From 33b36b4006c2b47f74108203668ad0730207c6b5 Mon Sep 17 00:00:00 2001 From: AlexanderWei <494120176@qq.com> Date: Wed, 5 Jun 2024 17:34:43 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=AE=8C=E5=96=84native=E9=95=9C=E5=83=8F?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E5=92=8C=E5=A2=9E=E5=8A=A0Dockerfile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 9 +++++++++ pom.xml | 20 +++++++++++++------- readme.md | 48 ++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d80040b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM ubuntu:jammy + +WORKDIR /app + +COPY target/spring-boot-build-docker ./ + +RUN chmod a+x spring-boot-build-docker + +ENTRYPOINT [ "./spring-boot-build-docker" ] \ No newline at end of file diff --git a/pom.xml b/pom.xml index ef237dc..7f67177 100755 --- a/pom.xml +++ b/pom.xml @@ -2,6 +2,11 @@ 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.2.6 + com.alex spring-boot-build-docker @@ -10,31 +15,32 @@ spring-boot-build-docker - 22 - 22 - 22 + 21 + 21 + 21 UTF-8 UTF-8 - 3.2.6 org.springframework.boot spring-boot-starter-web - ${spring-boot.version} + + org.graalvm.buildtools + native-maven-plugin + org.springframework.boot spring-boot-maven-plugin - 3.2.6 - ${project.artifactId}:latest + ${project.artifactId}-native:latest ${maven.build.timestamp} diff --git a/readme.md b/readme.md index 9c2d004..8231ed4 100644 --- a/readme.md +++ b/readme.md @@ -1,12 +1,44 @@ # spring-boot 容器化构建 ## docker镜像构建的三种方式 -1. springboot -2. jib -3. docker +1. spring-boot-maven-plugin +2. jib-maven-plugin +3. docker-maven-plugin -Global Jib Configuration +## native 镜像构建 -Some options can be set in the global Jib configuration file. The file is at the following locations on each platform: -Linux: [config root]/google-cloud-tools-java/jib/config.json, where [config root] is $XDG_CONFIG_HOME ($HOME/.config/ if not set) -Mac: [config root]/Google/Jib/config.json, where [config root] is $XDG_CONFIG_HOME ($HOME/Library/Preferences/ if not set) -Windows: [config root]\Google\Jib\Config\config.json, where [config root] is %XDG_CONFIG_HOME% (%LOCALAPPDATA% if not set) \ No newline at end of file +### 环境参数 + +- 操作系统:WSL 2: Ubuntu 22.04.4 LTS jammy +- graal版本: Oracle GraalVM 21.0.3+7.1 (build 21.0.3+7-LTS-jvmci-23.1-b37) +- docker版本:Docker version 26.1.1, build 4cf5afa + +1. 构建本地可执行文件 + +``` shell +./mvnw clean native:compile -Pnative +``` + +该命令依赖gcc和zlib,需要先执行 + +```shell +sudo apt-get install gcc +sudo apt-get install zlib1g-dev +``` +构建成功后,启动应用,执行 + +```shell +./target/spring-boot-build-docker +``` + +启动速度极快,几十毫秒 + +该方式无需联网,可以构建后通过Dockerfile继续构建docker镜像,但是需要保持构建环境和运行环境的类库兼容性。可以考虑通过同一个基础镜像构建构建镜像和运行镜像。 + +2. 构建oci镜像 + +``` shell +./mvnw clean spring-boot:build-image -Pnative +``` +通过docker-compose.yaml启动容器,同样也是极快启动 + +该方式是通过paketobuildpacks在docker镜像中编译和构建镜像,能保证编译环境与运行环境的一致性,屏蔽了不同操作系统版本的影响。该方式是比较适合方式。但是构建过程中有些依赖需要从github上下载,有网络联通问题,且插件参数不能配置镜像或离线构建。 \ No newline at end of file -- Gitee From 572ad6fe596a50df812917a1d763121aa599e391 Mon Sep 17 00:00:00 2001 From: AlexanderWei <494120176@qq.com> Date: Wed, 5 Jun 2024 10:04:56 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Dock?= =?UTF-8?q?erfile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index d80040b..0000000 --- a/Dockerfile +++ /dev/null @@ -1,9 +0,0 @@ -FROM ubuntu:jammy - -WORKDIR /app - -COPY target/spring-boot-build-docker ./ - -RUN chmod a+x spring-boot-build-docker - -ENTRYPOINT [ "./spring-boot-build-docker" ] \ No newline at end of file -- Gitee