Maven插件打包发布远程Docker镜像

dockerfile-maven-plugin插件的介绍

dockerfile-maven-plugin目前这款插件非常成熟,它集成了Maven和Docker,该插件的官方文档地址如下:

地址:https://github.com/spotify/dockerfile-maven

其他说明:

  • dockerfile是用来构建Docker项目的,这也是该插件所使用的,它们是强制性的。
    将Docker构建过程集成到Maven构建过程中。如果你绑定了默认阶段,当你输入 mvn package 命令时,你将得到一个Docker镜像;当你输入 mvn deploy 命令时,该镜像将会被推送。
  • 在pom.xml文件中使用goals标签声明我们想要做什么。这个里面的配置就相当于我们输入了 mvn dockerfile:build 命令后,再输入 mvn dockerfile:tag 命令,接着再输入 mvn dockerfile:push 命令。
  • 该插件需要Java 7或更高版本和Apache Maven 3或更高版本(dockerfile-maven-plugin <= 1.4.6 需要Maven >= 3,其它情况需要Maven >= 3.5.2)。在实践中要运行集成测试或者使用该插件,需要一个Docker运行环境。

开放远程Docker远程访问端口

bash 复制代码
# vim /lib/systemd/system/docker.service

ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2377 -H unix:///var/run/docker.sock

在配置项中修改该属性,该方式会直接暴露可以操控docker进程的端口,需要注意安全,修改完后重启docker服务

systemctl restart docker

在客户端添加系统环境变量参数

DOCKER_HOST
tcp://xx.xx.xx.xx:2375

为项目添加插件

父项目pom文件

xml 复制代码
<properties>
        <dockerfile-maven-plugin.version>1.4.13</dockerfile-maven-plugin.version>
        <docker.image.prefix>registry.xx.cn/xx</docker.image.prefix>
        <dockerfile.skip>false</dockerfile.skip>
        <docker.image.tag>1.0.0</docker.image.tag>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<plugin>
                    <!-- 每个模块不继承此插件依赖 false-->
                    <inherited>true</inherited>
                    <groupId>com.spotify</groupId>
                    <artifactId>dockerfile-maven-plugin</artifactId>
                    <version>${dockerfile-maven-plugin.version}</version>
<!--                    <executions>-->
<!--                        <execution>-->
<!--                            <id>default</id>-->
<!--                            <phase>package</phase>-->
<!--                            <goals>-->
<!--                                <goal>build</goal>-->
<!--                                <goal>tag</goal>-->
<!--                            </goals>-->
<!--                            <configuration>-->
<!--                                <tag>${project.version}</tag>-->
<!--                            </configuration>-->
<!--                        </execution>-->
<!--                    </executions>-->
                    <configuration>
                        <!-- 上下文路径配置,此处设置为项目根路径 用来读取Dockerfile-->
                        <contextDirectory>${project.basedir}</contextDirectory>
                        <!--使用maven setting认证-->
                        <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
                        <!-- 镜像仓库用户名 -->
                        <username>xxxx</username>
                        <!-- 镜像仓库密码 -->
                        <password>123456</password>
                        <!-- 标记 -->
                        <tag>${project.version}</tag>
                        <!-- 作为Dockerfile文件传入-->
                        <buildArgs>
                            <no-cache>true</no-cache>
                            <pull>true</pull>
                            <!-- <JAR_FILE>target/vp-gateway.jar</JAR_FILE> -->
                        </buildArgs>
                        <!-- 跳过默认标记 -->
                        <skipTag>true</skipTag>
                        <!-- 强制创建新标记 -->
                        <force>true</force>
                        <!-- 输出更详细信息 -->
                        <verbose>true</verbose>
                        <!-- 跳过插件操作 跳过所有的子模块 全局跳过 -->
                        <skip>${dockerfile.skip}</skip>
                        <!--关闭缓存-->
                        <noCache>true</noCache>
                        <!-- 自动更新镜像 -->
                        <pullNewerImage>true</pullNewerImage>
                        <!-- Dockerfile所在目录路径 -->
                        <dockerfile>Dockerfile</dockerfile>
                    </configuration>
</plugin>

子模块pom目录

xml 复制代码
<!-- docker打包插件,groupId、artifactId、version表示插件自生的版本信息 -->
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>${dockerfile-maven-plugin.version}</version>
    <configuration>
        <repository>${docker.image.prefix}/${project.build.finalName}</repository> <!-- 指定镜像构建后要推送的仓库地址 -->
        <!-- 指定构建镜像的版本tag -->
        <tag>${docker.image.tag}</tag>
        <!-- 不跳过 -->
        <skip>false</skip>
        <buildArgs>
            <!--Maven 构建的 jar 包相对路径和名称-->
            <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
        </buildArgs>
    </configuration>
</plugin>

Dockerfile文件

bash 复制代码
# 贝尔实验室 Spring 官方推荐镜像 JDK下载地址 https://bell-sw.com/pages/downloads/
FROM  bellsoft/liberica-openjdk-debian:8-cds
#FROM  registry.flow.cn/library/eclipse-temurin:8-jre


LABEL maintainer="Zhujj"
ARG APP_NAME=test.jar

RUN mkdir -p /app

WORKDIR /app

COPY ./target/${APP_NAME}.jar ./app.jar

# 环境变量
ENV SERVER_PORT=8080 LANG=C.UTF-8 LC_ALL=C.UTF-8 TZ=Asia/Shanghai JAVA_OPTS="-Xms512m -Xmx2048m"

EXPOSE ${SERVER_PORT}


ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -Dserver.port=${SERVER_PORT} \
           # 应用名称 如果想区分集群节点监控 改成不同的名称即可
           #-Dskywalking.agent.service_name=${APP_NAME} \
           #-javaagent:/app/skywalking/agent/skywalking-agent.jar
           -#agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:30005 \
           -XX:+HeapDumpOnOutOfMemoryError ${JAVA_OPTS} \
           -jar app.jar

打包发布为远程docker镜像

进入到子模块根目录执行下面的命令:

bash 复制代码
mvn clean install -Pprod dockerfile:build dockerfile:push

-Pprod 指定profile为prod

发布成功之后就可以在仓库上看到自己的镜像了。

相关推荐
Chen-Edward6 小时前
有了Spring为什么还有要Spring Boot?
java·spring boot·spring
jianghx10246 小时前
Docker部署ES,开启安全认证并且设置账号密码(已运行中)
安全·elasticsearch·docker·es账号密码设置
陈小桔7 小时前
idea中重新加载所有maven项目失败,但maven compile成功
java·maven
小学鸡!7 小时前
Spring Boot实现日志链路追踪
java·spring boot·后端
xiaogg36787 小时前
阿里云k8s1.33部署yaml和dockerfile配置文件
java·linux·kubernetes
逆光的July7 小时前
Hikari连接池
java
微风粼粼7 小时前
eclipse 导入javaweb项目,以及配置教程(傻瓜式教学)
java·ide·eclipse
番茄Salad7 小时前
Spring Boot临时解决循环依赖注入问题
java·spring boot·spring cloud
天若有情6738 小时前
Spring MVC文件上传与下载全面详解:从原理到实战
java·spring·mvc·springmvc·javaee·multipart
祈祷苍天赐我java之术8 小时前
Redis 数据类型与使用场景
java·开发语言·前端·redis·分布式·spring·bootstrap