idea Maven 插件 docker-maven-plugin 打包docker鏡像上傳到遠程倉庫

參考資料 https://my.oschina.net/u/3764794/blog/2995648

原理:

docker-maven-plugin 調用一個 docker 的api,進行打包鏡像,tag標籤,push到遠程倉庫。

遠程倉庫的密碼,在本地maven 的setting.xml裏配置一個server ,idea根據id可以獲取到遠程倉庫的賬號,密碼

一、docker主機開啓docker 遠程訪問API

Ubuntu:

vi /lib/systemd/system/docker.service

Centos7

vi /usr/lib/systemd/system/docker.service 
找到ExecStart
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

修改完後 重置重啓

systemctl daemon-reload
systemctl restart docker

防火牆開放端口:2375

驗證

http://ip:2375/images/json  或 curl http:/ip:2375/info

二、在docker主機配置遠程倉庫地址,以便非https 也能訪問

 

vi  /etc/docker/daemon.json

 

{
"registry-mirrors":  [
"https://dockerhub.azk8s.cn",
"https://reg-mirror.qiniu.com"
],
    "insecure-registries": ["registry1的IP地址:端口號","registry2的IP地址:端口號"]
}

修改完後 重置重啓

systemctl daemon-reload
systemctl restart docker

三、maven 配置 遠程倉庫的賬號密碼

編輯 maven settings.xml文件

 

 <server>  
        <id>local_docker</id>  
         <username>lulu</username>
        <password>123456</password>
 </server> 

 

四、pom 配置

 

<properties>
    <project.name>bluetoothled</project.name>
<!-- 私有倉庫配置,需要settings.xml文件配合serverId對應的倉庫服務 賬號密碼 -->
    <docker.serverId>local_docker</docker.serverId>
    <docker.registry>192.168.182.100:5000</docker.registry>
     <docker.host>http://192.168.182.100:2375</docker.host>
</properties>

 

    <build>
        <plugins>
            <!-- Srping Boot 打包工具 打包成可執行的jar  -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!--打包後 複製jar包到指定文件目錄-->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <!-- here the phase you need -->
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <!-- 複製資源後的輸出目錄 -->
                            <outputDirectory>target</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/docker</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- docker插件 -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.2.0</version>
                <configuration>
                    <!-- 私有倉庫配置,需要settings.xml文件配合serverId對應的服務地址 -->
                    <serverId>${docker.serverId}</serverId>
                    <!-- docker私服地址 -->
                    <registryUrl>${docker.registry}</registryUrl>
                    <!-- 指定docker server的地址,該配置不需要本機安裝docker -->
                    <dockerHost>${docker.host}</dockerHost>
                    <imageName>${project.name}/${project.artifactId}:${project.version}</imageName>
                    <imageTags>
                        <imageTag>${project.version}</imageTag>
                    </imageTags>
                    <!-- docker的構建目錄(構建上下文),包含Dockerfile  -->
                    <dockerDirectory>target</dockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
                <executions>
                    <!-- package之前清除上一次構建的image -->
                    <execution>
                        <id>remove-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>removeImage</goal>
                        </goals>
                        <configuration>
                            <imageName>
                               ${project.name}/${project.artifactId}
                            </imageName>
                            <imageTags>
                                <imageTag>${project.version}</imageTag>
                                <imageTag>latest</imageTag>
                            </imageTags>
                        </configuration>
                    </execution>
                    <execution>
                        <id>remove-tag-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>removeImage</goal>
                        </goals>
                        <configuration>
                            <imageName>
                                ${docker.registry}/${project.name}/${project.artifactId}
                            </imageName>
                            <imageTags>
                                <imageTag>${project.version}</imageTag>
                                <imageTag>latest</imageTag>
                            </imageTags>
                        </configuration>
                    </execution>
                    <!-- 將docker:build綁定到package這個phase  -->
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                        <configuration>
                            <!-- imageName中若不指定tag,則會打上latest -->
                            <imageName>${project.name}/${project.artifactId}:${project.version}</imageName>
                            <!-- 可以使用<imageTags>標籤打其他額外的tag -->
                        </configuration>
                    </execution>
                    <!-- 將docker:tag綁定到package這個phase  -->
                    <execution>
                        <id>tag-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>tag</goal>
                        </goals>
                        <configuration>
                            <!-- docker tag IMAGE[:TAG] [REGISTRY_HOST[:REGISTRY_PORT]/]REPOSITORY[:TAG]  -->
                            <!-- images與IMAGE[:TAG]對應,必須在build階段已經構建了 -->
                            <image>${project.name}/${project.artifactId}:${project.version}</image>
                            <!-- newName與tag命令的第二個參數對應 -->
                            <newName>
                                ${docker.registry}/${project.name}/${project.artifactId}:${project.version}
                            </newName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>tag-image-latest</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>tag</goal>
                        </goals>
                        <configuration>
                            <!-- docker tag IMAGE[:TAG] [REGISTRY_HOST[:REGISTRY_PORT]/]REPOSITORY[:TAG]  -->
                            <!-- images與IMAGE[:TAG]對應,必須在build階段已經構建了 -->
                            <image>${project.name}/${project.artifactId}:${project.version}</image>
                            <!-- newName與tag命令的第二個參數對應 -->
                            <newName>
                                ${docker.registry}/${project.name}/${project.artifactId}:latest
                            </newName>
                        </configuration>
                    </execution>
                    <!-- 將docker:push綁定到deploy這個phase  -->
                    <execution>
                        <id>push-image</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>push</goal>
                        </goals>
                        <configuration>
                            <imageName>
                                ${docker.registry}/${project.name}/${project.artifactId}:${project.version}
                            </imageName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>push-image-latest</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>push</goal>
                        </goals>
                        <configuration>
                            <imageName>
                                ${docker.registry}/${project.name}/${project.artifactId}:latest
                            </imageName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

 

docker-maven-plugin 詳細說明請參考https://my.oschina.net/u/3764794/blog/2995648

 

五、在idea中命令控制檯中使用
創建鏡像
mvn clean package docker:build
推送鏡像到Registry
mvn clean package docker:build -DpushImage
推送指定tag的鏡像到Registry
mvn clean package docker:build -DpushImageTag
執行 build、tag、push 操作
mvn deploy

如果想跳過 docker 某個過程時,只需要:
-DskipDockerBuild 跳過 build 鏡像
-DskipDockerTag 跳過 tag 鏡像
-DskipDockerPush 跳過 push 鏡像
-DskipDocker 跳過整個階段

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章