怎么使用Maven打包构建Docker镜像并推送到仓库

寻技术 JAVA编程 2023年11月02日 56

这篇文章主要介绍了怎么使用Maven打包构建Docker镜像并推送到仓库的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么使用Maven打包构建Docker镜像并推送到仓库文章都会有所收获,下面我们一起来看看吧。

    一,服务器Docker配置

    修改daemon.json文件,开放端口2375

    "hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]

    我的文件如下所示:

    {
          "insecure-registries":["10.xx.xx.xx:5000"],
        "hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]
    }
    {
    "graph":"/Data/docker"
    }

    二,本地项目maven配置

    2.1 pom.xml

    在项目的pom文件引入docker-maven-plugin插件

    <plugin>
                    <!-- https://mvnrepository.com/artifact/com.spotify/docker-maven-plugin -->
                    <groupId>com.spotify</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <version>1.2.2</version>
                    <executions>
                        <!-- 当mvn执行install操作的时候,执行docker的build和push -->
                        <execution>
                            <id>build</id>
                            <phase>install</phase>
                            <goals>
                                <goal>build</goal>
                                <!--                                <goal>push</goal>-->
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <!-- 连接到 带docker环境的linux服务器 编译image -->
                        <dockerHost>http://10.xx.xx.xx:2375</dockerHost>
    
                        <!--格式:私有仓库/镜像名称:版本号, 如果要执行push操作, 那么镜像名称必须为私有仓库为前缀,不然无效。-->
                        <imageName>10.xx.xx.xx:5000/${project.artifactId}:${docker.tag}</imageName>
    
                        <!--指定dockerfile文件路径-->
                        <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
    
                        <!-- 指定jar包路径,这里对应Dockerfile中复制 jar 包到 docker 容器指定目录配置,也可以写到 Docokerfile 中 -->
                        <resources>
                            <resource>
                                <targetPath>/</targetPath>
                                <directory>${project.build.directory}</directory>
                                <include>${project.build.finalName}.jar</include>
                            </resource>
                        </resources>
    
                        <!--跳过测试-->
    
                        <!-- push到 docker hub 开始 -->
                        <!-- serverId 这个是配置在maven的setting.xml中私服的登录账户信息-->
                        <!--                    <serverId>docker-hub</serverId>-->
                        <retryPushCount>3</retryPushCount>
                        <retryPushTimeout>2000</retryPushTimeout>
                        <registryUrl>10.xx.xx.xx:5000</registryUrl>
                        <!-- 是否自
    关闭

    用微信“扫一扫”