首页 > 其他分享 >Springboot 的 maven项目利用 maven插件构建 docker 镜像(免 DockerFile编写)

Springboot 的 maven项目利用 maven插件构建 docker 镜像(免 DockerFile编写)

时间:2023-09-20 18:12:21浏览次数:42  
标签:INFO Adding 插件 Springboot creator boot maven org

Springboot 的 maven项目利用 maven插件构建 docker 镜像(免 DockerFile编写)

本小节目的是springboot 项目 maven 插件构建容器
实验步骤:
1. 本地创建springboot项目,写业务逻辑代码
2.提交代码到远程 git仓库
3.在 linux 环境拉取远程 git 仓库代码,构建镜像
4.把构建完成的镜像推送私有 harbor 镜像仓库

  1. Packaging OCI Images
    The plugin can create an OCI image from a jar or war file using Cloud Native Buildpacks (CNB). Images can be built on the command-line using the build-image goal. This makes sure that the package lifecycle has run before the image is created.
    For security reasons, images build and run as non-root users. See the CNB specification for more details.
    The easiest way to get started is to invoke mvn spring-boot:build-image on a project. It is possible to automate the creation of an image whenever the package phase is invoked, as shown in the following example:......
    Use build-image-no-fork when binding the goal to the package lifecycle. This goal is similar to build-image but does not fork the lifecycle to make sure package has run. In the rest of this section, build-image is used to refer to either the build-image or build-image-no-fork goals.
    While the buildpack runs from an executable archive, it is not necessary to execute the repackage goal first as the executable archive is created automatically if necessary. When the build-image repackages the application, it applies the same settings as the repackage goal would, that is dependencies can be excluded using one of the exclude options. The spring-boot-devtools and spring-boot-docker-compose modules are automatically excluded by default (you can control this using the excludeDevtools and excludeDockerCompose properties).

创建项目

  • 创建一个 springboot项目,maven管理,本次选择版本 2.7.11
      #添加web,写一个经典的 hello world请求响应
	    <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
      #添加actuator,内部含有 pod 健康检查探针
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
  • 添加 maven 插件
<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>2.7.11</version>
				<executions>				 
					<execution>
						<goals>
							<goal>build-image</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

完整的 pom.xml 文件

点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.11</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.ant.com</groupId>
	<artifactId>testk8s</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>testk8s</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-undertow</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-tomcat</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
	</dependencies>
	<!--    打包构建-->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>2.7.11</version>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
				<executions>
					<execution>
						<id>repackage</id>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
					<execution>
						<goals>
							<goal>build-image</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
	<!--	配置阿里云 maven仓库-->
	<repositories>
		<repository>
			<id>public</id>
			<name>aliyun nexus</name>
			<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>public</id>
			<name>aliyun nexus</name>
			<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>
</project>

项目推送到 git 仓库

#初始化
git init
#没有私库就推送到 github
git remote add origin [email protected]:server/testk8s.git
#后先就是创建本地分支
git pull origin dev   
git commit -m "init"
git push 

构建镜像(centOS)

  • 准备 maven,maven版本 3.2.5+
sudo yum install maven-3.3.0
#若没有镜像,安装最新版本
sudo yum install maven
#官网手动下载安装包,解压
tar -zxvf apache-maven-3.9.4-bin.tar.gz 
#替换低版本链接
sudo mv apache-maven-3.9.4 apache-maven
mkdir /usr/share/maven
mv apache-maven /usr/share/maven/
sudo ln -sfn /usr/share/maven/apache-maven/bin/mvn /usr/bin/mvn
mvn --version
  • 准备 git,拉取远程仓库代码
#安装 git
yum install git
#拉取代码
git clone [email protected]:server/testk8s.git
cd testk8s/
git pull  origin/dev:dev
  • 开始构建镜像
mvn clean package spring-boot:build-image

第一次构建要花好长好长时间,反正这一步我花了一个小时,有精度条,甚至第一次有可能失败,失败后再试一次,第二次时间时间就短
第一次失败信息

点击查看代码
[INFO]     [creator]     unable to download https://github.com/bell-sw/Liberica/releases/download/8u372+7/bellsoft-jre8u372+7-linux-amd64.tar.gz
[INFO]     [creator]     unable to request https://github.com/bell-sw/Liberica/releases/download/8u372+7/bellsoft-jre8u372+7-linux-amd64.tar.gz
[INFO]     [creator]     Get "https://objects.githubusercontent.com/github-production-release-asset-2e65be/115621629/a26c3c5b-da95-4275-b072-9fc9dadb3e1d?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20230920%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230920T084204Z&X-Amz-Expires=300&X-Amz-Signature=f04d238723d3df35d74a1691028e25d7dd65c89265bce40d8a5c61a18344f43e&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=115621629&response-content-disposition=attachment%3B%20filename%3Dbellsoft-jre8u372%2B7-linux-amd64.tar.gz&response-content-type=application%2Foctet-stream": dial tcp: lookup objects.githubusercontent.com: i/o timeout
[INFO]     [creator]     ERROR: failed to build: exit status 1
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:42 h
[INFO] Finished at: 2023-09-20T16:42:10+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.7.10:build-image (default) on project k8s: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:2.7.10:build-image failed: Builder lifecycle 'creator' failed with status code 51 -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
第二次成功信息
点击查看代码
[INFO]     [creator]         Writing env.launch/BPL_JVM_THREAD_COUNT.default
[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]     Adding layer 'paketo-buildpacks/ca-certificates:helper'
[INFO]     [creator]     Adding layer 'paketo-buildpacks/bellsoft-liberica:helper'
[INFO]     [creator]     Adding layer 'paketo-buildpacks/bellsoft-liberica:java-security-properties'
[INFO]     [creator]     Adding layer 'paketo-buildpacks/bellsoft-liberica:jre'
[INFO]     [creator]     Adding layer 'paketo-buildpacks/executable-jar:classpath'
[INFO]     [creator]     Adding layer 'paketo-buildpacks/spring-boot:helper'
[INFO]     [creator]     Adding layer 'paketo-buildpacks/spring-boot:spring-cloud-bindings'
[INFO]     [creator]     Adding layer 'paketo-buildpacks/spring-boot:web-application-type'
[INFO]     [creator]     Adding layer 'buildpacksio/lifecycle:launch.sbom'
[INFO]     [creator]     Adding 5/5 app layer(s)
[INFO]     [creator]     Adding layer 'buildpacksio/lifecycle:launcher'
[INFO]     [creator]     Adding layer 'buildpacksio/lifecycle:config'
[INFO]     [creator]     Adding 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/k8s:0.0.1-SNAPSHOT...
[INFO]     [creator]     *** Images (0918fc41de57):
[INFO]     [creator]           docker.io/library/k8s:0.0.1-SNAPSHOT
[INFO]     [creator]     Adding cache layer 'paketo-buildpacks/syft:syft'
[INFO]     [creator]     Adding cache layer 'buildpacksio/lifecycle:cache.sbom'
[INFO] 
[INFO] Successfully built image 'docker.io/library/k8s:0.0.1-SNAPSHOT'
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  03:09 min
[INFO] Finished at: 2023-09-20T16:52:37+08:00
[INFO] ------------------------------------------------------------------------
  • 查看构建镜像结果
[root@localhost testk8s]# docker images
REPOSITORY                 TAG              IMAGE ID       CREATED        SIZE
paketobuildpacks/run       base-cnb         f2e5000af0cb   2 months ago   87.1MB
paketobuildpacks/builder   base             050ed48532b2   43 years ago   1.31GB
testk8s                        0.0.1-SNAPSHOT   0918fc41de57   43 years ago   229MB

可以看到我们已经构建出镜像testk8s ,这个名字及时项目的artifactId

  • 把镜像推送到 harbor 私库
#登录 harbor
[root@localhost testk8s]# docker login http://192.168.10.109:30002
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

#给需要推送的镜像 tag 标签,先需要在 harbor 创建项目,这里是 test
[root@localhost testk8s]# docker images 
REPOSITORY                 TAG              IMAGE ID       CREATED        SIZE
paketobuildpacks/run       base-cnb         f2e5000af0cb   2 months ago   87.1MB
paketobuildpacks/builder   base             050ed48532b2   43 years ago   1.31GB
k8s                        0.0.1-SNAPSHOT   0918fc41de57   43 years ago   229MB
[root@localhost testk8s]# docker tag k8s:0.0.1-SNAPSHOT 192.168.10.109:30002/test/test:0.0.1-SNAPSHOT

#推送到harbor
[root@localhost testk8s]# docker images 
REPOSITORY                       TAG              IMAGE ID       CREATED        SIZE
paketobuildpacks/run             base-cnb         f2e5000af0cb   2 months ago   87.1MB
192.168.10.109:30002/test/test   0.0.1-SNAPSHOT   0918fc41de57   43 years ago   229MB
k8s                              0.0.1-SNAPSHOT   0918fc41de57   43 years ago   229MB
paketobuildpacks/builder         base             050ed48532b2   43 years ago   1.31GB
[root@localhost testk8s]# docker push 192.168.10.109:30002/test/test:0.0.1-SNAPSHOT

可以登录 harbor 查看

标签:INFO,Adding,插件,Springboot,creator,boot,maven,org
From: https://www.cnblogs.com/qlsem/p/17718000.html

相关文章

  • 极光笔记 | 大语言模型插件
    在人工智能领域,大语言模型(LLMs)是根据预训练数据集进行”学习“,获取可以拟合结果的参数,虽然随着参数的增加,模型的功能也会随之增强。但无论专业领域的小模型,还是当下最火、效果最好的大模型,都有一个共同的劣势:无法准确/正确地回答出训练数据集以外(区别于验证集和测试集的新增数据,如......
  • 求生之路2服务器搭建插件安装及详细的游戏参数配置教程windows
    求生之路2服务器搭建插件安装及详细的游戏参数配置教程windows大家好我是艾西,最近研究了下l4d2(求生之路2)这款游戏的搭建以及架设过程。今天就给喜欢l4d2这款游戏的小伙伴们分享下怎么搭建架设一个自己的服务器。毕竟自己当服主是热爱游戏每一个人的梦想,在自己的服务器里为所欲为在......
  • Maven配置阿里云仓库地址
    Maven下载与安装:  https://www.cnblogs.com/ychun/p/15547423.htmlMaven配置阿里云仓库地址: https://developer.aliyun.com/mvn/guide  ......
  • Spring,SpringMVC,SpringBoot,SpringCloud有什么区别?
    简单介绍Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。Spring使你能够编写更干净、更可管理、并且更易于测试的代码。SpringMVC是Spring的一个模块,一个web框架。通过DispatcherServlet,ModelAndView和ViewResolver,开发web应用变得很容易。主要针对的是网站......
  • SpringBoot2集成RabbitMQ(注解+回调)
    一、概述RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。RabbitMQ主要是为了实现系统之间的双向解耦而实现的。当生产者大量产生数据时,消费者无法快速消费,那么需要一个中......
  • js实现选中某个区域展示引导信息(插件库)
    在公司做项目,经常存在类似「引导」的功能,引导用户该先点哪后点哪,或者做一些首次使用的提示找到了个比较好用且轻量化的js组件intro.js ,有常规的灰色遮罩高亮特定dom提示的(hello-world),也有用户点到指定标签/位置上的提示(hints)感觉还是不错的,并且,有免费版本......
  • gradle/maven/eclipse工程相互转化
    。gradle/maven/eclipse工程相互转化:前提安装好相应的工具和插件。1、Maven->eclipsemvneclipse:eclipse 2、eclipse->maven安装好maven插件后,在eclipse工程右键项目:转换为maven工程即可。 3、gradle->eclipse编辑build.gradle文件,在文件最前面增加一行:a......
  • 销售出库单增加二开审核插件
    【目录】1、 扩展销售出库单,标识规范2、 创建审核服务插件,编写审核逻辑3、 审核操作注册服务插件4、 签入元数据和代码  【详细操作】1、 引入销售出库单,扩展销售出库单,标识规范      2、 创建审核服务插件,编写审核逻辑  将类改成公......
  • Springboot 全局日期时间格式处理
    From: https://www.cnblogs.com/Baker-Street/p/16156297.html大家伙在日常开发中可能都遇到过,前端传递的时间字符串,后台如果用日期接收(Date或者jdk8的Local日期)接收,经mvc接收后就报错了,先解析原因:1.get请求和post表单请求中如果含有时间字符串,则spring底层是用的Par......
  • 【精品】SpringBoot统一日期类型处理
    From: https://blog.csdn.net/lianghecai52171314/article/details/127106664方案一:给日期字段添加注解/***创建时间*///返回时间类型@JsonFormat(pattern=GlobalConst.DATETIME_PATTERN,timezone="GMT+8")//接收时间类型@DateTimeFormat(pattern=GlobalConst.DATETIM......