0、前提:docker、 maven、java均已安装,docker 镜像私服均已设置网络也开通
本文基于Docker进行构建,也可以指定构建信息到 POM 中构建,大家自行搜索
1、首先是一些网络和docker相关配置的打开
2375是docker远程操控的默认端口,通过这个端口可以直接对远程的docker daemon进行操作;
任何能连通到这台docker宿主机的的机器都可以随意操作
这台docker宿主机的docker daemon(docker run、docker ps、docker rm等等这些命令)
推荐进行tls+CA证书配置进行远程操控,这里不赘述,可以自行baidu google
这里为了方便使用,使用非安全的方式试验
配置docker,开放2375端口
Docker的2375端口是默认关闭的,如果我们要使用到它,就需要进行以下的配置:
1、安装有docker的服务器,修改docker.service文件:
vi /etc/systemd/system/docker.service
具体文件所在地,得看你安装docker的位置,如果不知道自己安装在哪里了,执行以下指令去查找:
sudo find / -name docker.service
2、修改docker.service
找到文件以下内容:
ExecStart=/usr/bin/dockerd
修改为:
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
3、重启docker服务
systemctl daemon-reload
systemctl restart docker
2、针对maven 的 setting.xml 文件的修改
1、增加server配置,填充私服harbor的账号密码
<server>
<id>docker-hub</id>
<username>admin</username>
<password>Harbor12345</password>
</server>
2、其余配置 和 maven本身配置自己私服没区别
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<server>
<id>docker-hub</id>
<username>admin</username>
<password>Harbor12345</password>
</server>
<server>
<id>nexus-central</id>
<username>admin</username>
<password>Aaaa1111</password>
</server>
<server>
<id>releases</id>
<username>admin</username>
<password>Aaaa1111</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>Aaaa1111</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus-public</id>
<mirrorOf>*</mirrorOf>
<name>Nexus central</name>
<url>http://dev07.dfhamit.cn:8081/repository/maven-public</url>
</mirror>
<!--
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>-->
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://dev07.dfhamit.cn:8081/repository/maven-public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://dev07.dfhamit.cn:8081/repository/maven-public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>aliyun</id>
<repositories>
<repository>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/spring</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/spring</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
<!-- <activeProfile>aliyun</activeProfile>-->
</activeProfiles>
</settings>
3、项目的pom文件,引入docker maven plugin 及相关配置
<!-- deploy 远程仓库 ,id与maven setting.xml中对应-->
<distributionManagement>
<repository>
<id>releases</id>
<name>dfham maven releases</name>
<url>http://10.118.193.117:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>dfham maven snapshots</name>
<url>http://10.118.193.117:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.3.RELEASE</version>
<configuration>
<executable>true</executable>
<!--springboot启动类目录 -->
<mainClass>com.dfham.perfma.PerfmaApplication</mainClass>
<!-- MAIN-CLASS:PropertiesLauncher,lib 外置 -->
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<!--创建一个自动可执行的jar或war文件 -->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 跳过单元测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>cert</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
</plugin>
<!-- https://blog.csdn.net/fly_duck/article/details/124709727 -->
<!-- mvn package -Dmaven.test.skip=true && mvn docker:build -DpushImage -DdockerImageTags=$CI_COMMIT_REF_NAME -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.0</version>
<!-- 绑定docker build 与 push 至 mvn deploy 阶段-->
<!-- 不绑定可以mvn命令行 https://www.jianshu.com/p/39881a8c5a1d
mvn clean package docker:build 只执行 build 操作
mvn clean package docker:build -DpushImage 执行 build 完成后 push 镜像
mvn clean package docker:build -DpushImageTag 执行 build 并 push 指定 tag 的镜像
mvn clean package docker:build -DpushImageTags -DdockerImageTags=imageTag_1 -DdockerImageTags=imageTag_2 -->
<executions>
<execution>
<id>build-push</id>
<phase>deploy</phase>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 连接到 带docker环境的linux服务器 编译image -->
<dockerHost>http://localhost:2375</dockerHost>
<!-- push到 docker hub 开始 -->
<!-- serverId 这个是配置在maven的setting.xml中私服的登录账户信息-->
<serverId>docker-hub</serverId>
<retryPushCount>1</retryPushCount>
<retryPushTimeout>2000</retryPushTimeout>
<registryUrl>http://10.118.192.228</registryUrl>
<!-- 格式:私有仓库/镜像名称:版本号, 如果要执行push操作, 那么镜像名称必须为私有仓库为前缀,不然无效。-->
<imageName>10.118.192.228/zhoushun/${project.artifactId}:3.0</imageName>
<!-- push到 docker hub 结束 -->
<!-- 指定tag版本 imageName中拼接版本号即可,此字段可不用-->
<!-- <imageTags>-->
<!-- <imageTag>3.0</imageTag>-->
<!-- </imageTags>-->
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<!-- optionally overwrite tags every time image is built with docker:build -->
<!-- <forceTags>true</forceTags>-->
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
4、Dockerfile,默认放在项目 src/main/docker 下,其他位置会copy多余的东西
FROM 10.118.192.228/zhoushun/openjdk:8-jre-alpine
WORKDIR /opt/perfma
COPY perfma-test-2.2.jar /opt/perfma
EXPOSE 8080
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && apk update
RUN apk add tzdata \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
CMD ["java","-jar","perfma-test-2.2.jar"]
# mvn clean package docker:build docker:push
# mvn package -Dmaven.test.skip=true && mvn docker:build -DpushImage -DdockerImageTags=$CI_COMMIT_REF_NAME
# docker run -it -d -p 8080:8080 --name perfma3.0 10.118.192.228/zhoushun/perfma-test:3.0
# docker exec -it perfma3.0 /bin/sh
# 删除tag none 的镜像
# docker image prune
5、运行过程
参考文章:
Docker基于Centos7构建JDK8镜像 (qq.com)
(1569条消息) 【docker的那些事】Dockerfile构建并推送到阿里云仓库_docker push 阿里云_陈橙橙丶的博客-CSDN博客
SpringBoot项目使用docker-maven-plugin插件构建docker镜像以及推送到docker hub或docker registry私服_OkidoGreen的博客-CSDN博客
(1570条消息) Spring Boot配置docker-maven-plugin实现构建与推送镜像_spring-boot-maven-plugin_明天再去学习的博客-CSDN博客 Docker-通过docker-maven-plugin插件实现docker镜像构建并自动发布到远程docker服务器 (bbsmax.com)文章来源:https://www.toymoban.com/news/detail-728353.html
docker-maven-plugin - 简书 (jianshu.com)文章来源地址https://www.toymoban.com/news/detail-728353.html
到了这里,关于Spring Boot配置docker-maven-plugin实现构建与推送镜像(Dockerfile)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!