Maven編譯Docker 鏡像並推送到Nexus

前提條件:

  1. 有一臺電腦 安裝了maven
  2. 安裝了jdk1.8 以上版本
  3. 安裝了 git
  4. 安裝了intellij idea
  5. 電腦能上網
  6. 有一臺nexus 服務器,如果沒有可以參考我的另一篇文章:Sonatype Nexus 自動化部署實踐 https://blog.csdn.net/happyfreeangel/article/details/89481720
    我的nexus 服務器器是支持https的,可以 端口是:上傳鏡像端口是1443, 下載鏡像端口是2443,
    域名是nexus.linkaixin.com

spotify docker-maven-plugin 官方源代碼地址:
https://github.com/spotify/docker-maven-plugin

下面是一個大型網上商店的源代碼,用於測試鏡像打包,上傳nexus.
https://github.com/HappyFreeAngel/mall.git

git clone https://github.com/HappyFreeAngel/mall.git

cd mall
mvn clean compile package docker:build -DpushImage

實現了編譯,打包,封裝docker 鏡像,推送docker 鏡像到nexus 倉庫。

結果如下:
本地鏡像結果如下:
docker images | grep ‘nexus.linkaixin.com:1443/mall’

在這裏插入圖片描述
遠程倉庫查看: 結果如下
![在這裏插入圖片描述](https://img-blog.csdnimg.cn/2019081616103873.png在這裏插入圖片描述

看起來很簡單的插件,使用起來其實坑很多。
我後來查看了源代碼,通過幾種測試方法,才發現問題所在。

需要配置的文件: 1. .m2/settings.xml

    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
    <server>
       <id>nexus-docker-registry</id>
       <username>admin</username>
       <password>admin123</password>
       <configuration>
          <email>[email protected]</email>
       </configuration>
    </server>
  1. 配置pom.xml
<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 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.macro.mall</groupId>
    <artifactId>mall-admin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mall-admin</name>
    <description>mall-admin project for mall</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <skipTests>true</skipTests>
    </properties>

    <parent>
        <groupId>com.macro.mall</groupId>
        <artifactId>mall</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>com.macro.mall</groupId>
            <artifactId>mall-mbg</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!--JWT(Json Web Token)登錄支持-->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.0</version>
        </dependency>
        <!-- 阿里雲OSS -->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>2.5.0</version>
        </dependency>
        <!--集成logstash-->
        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>4.8</version>
        </dependency>
        <!--lombok依賴-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.2.0</version>
                <executions>
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>

                        <configuration>
                            <imageName>${docker-registry-host}:${docker-registry-host-push-port}/mall/${project.artifactId}:${project.version}</imageName>
                        </configuration>
                    </execution>

                    <execution>
                        <id>tag-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>tag</goal>
                        </goals>
                        <configuration>
                            <image>mall/${project.artifactId}:${project.version}</image>
                            <newName>${docker-registry-host}:${docker-registry-host-push-port}/mall/${project.artifactId}:${project.version}</newName>
                        </configuration>
                    </execution>

                    <execution>
                        <id>push-image</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>push</goal>
                        </goals>
                        <configuration>
                            <imageName>${docker-registry-host}:${docker-registry-host-push-port}/mall/${project.artifactId}:${project.version}</imageName>
                        </configuration>
                    </execution>

                </executions>
                <configuration>
                    <imageName>${docker-registry-host}:${docker-registry-host-push-port}/mall/${project.artifactId}:${project.version}</imageName>
                    <dockerHost>unix:///var/run/docker.sock</dockerHost>
<!--                    <dockerCertPath>/Users/linyingjie/nexus.linkaixin.com.crt</dockerCertPath>-->
                    <baseImage>nexus.linkaixin.com:2443/common-docker-starter:openjdk-8u191-alpine3.8-support-ssh-login-1.0.0</baseImage>
                    <entryPoint>["java", "-jar", "-Dspring.profiles.active=prod","/${project.build.finalName}.jar"]</entryPoint>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                    <serverId>nexus-docker-registry</serverId>
                    <!-- <registryUrl>119.145.41.171:8082/v1/</registryUrl> -->
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

踩到的坑如下:
問題1:
[ERROR] No plugin found for prefix ‘docker’ in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/linyingjie/.m2/repository), alimaven (http://maven.aliyun.com/nexus/content/groups/public/)] -> [Help 1]

解決辦法: .m2/settings.xml 添加 com.spotify

    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
    <pluginGroup>io.fabric8</pluginGroup>
    <pluginGroup>com.spotify</pluginGroup>
  </pluginGroups>

問題2:
[WARNING] Failed to push nexus.linkaixin.com:1443/mall/mall-admin:1.0-SNAPSHOT, retrying in 10 seconds (1/5).
[INFO] Pushing nexus.linkaixin.com:1443/mall/mall-admin:1.0-SNAPSHOT
The push refers to repository [nexus.linkaixin.com:1443/mall/mall-admin]
214b9fe2899d: Preparing
27e730ca5883: Layer already exists
70eee88b4bab: Layer already exists
4e2f9475b031: Layer already exists
10beb411784d: Layer already exists
aa0369d6605f: Layer already exists
612d6b123925: Waiting
36d9b4d72ec4: Waiting
1de5d7cd5bd1: Waiting
b845f393fc2b: Waiting
dbc783c89851: Waiting
7bff100f35cb: Waiting
[WARNING] Failed to push nexus.linkaixin.com:1443/mall/mall-admin:1.0-SNAPSHOT, retrying in 10 seconds (2/5).

這個原因是因爲這個插件優先使用了.docker/config.json,但是這個會超時失敗,不知道是什麼原因
刪除裏面的nexus.linkaixin.com 的2條記錄,就正常了。

{
	"auths": {
		"nexus.linkaixin.com:1443": {},
		"nexus.linkaixin.com:2443": {}

	},
	"HttpHeaders": {
		"User-Agent": "Docker-Client/18.09.2 (darwin)"
	},
	"credsStore": "osxkeychain",
	"stackOrchestrator": "swarm"
}

改成下面:
{
	"auths": {
	

	},
	"HttpHeaders": {
		"User-Agent": "Docker-Client/18.09.2 (darwin)"
	},
	"credsStore": "osxkeychain",
	"stackOrchestrator": "swarm"
}

這個其實就是讓docker-maven-plugin 在config.json 裏找不到關於nexus.linkaixin.com賬號配置,
讓它搜索settings.xml 裏的配置賬號信息。

[INFO] — docker-maven-plugin:1.2.0:tag (tag-image) @ mall-admin —
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier, FixedRegistryAuthSupplier]
[INFO] Creating tag nexus.linkaixin.com:1443/mall/mall-admin:1.0-SNAPSHOT from mall/mall-admin:1.0-SNAPSHOT
[INFO] Pushing nexus.linkaixin.com:1443/mall/mall-admin:1.0-SNAPSHOT
The push refers to repository [nexus.linkaixin.com:1443/mall/mall-admin]
e595cf98d1ac: Layer already exists
27e730ca5883: Layer already exists
70eee88b4bab: Layer already exists
4e2f9475b031: Layer already exists
10beb411784d: Layer already exists
aa0369d6605f: Layer already exists
612d6b123925: Layer already exists
36d9b4d72ec4: Layer already exists
1de5d7cd5bd1: Layer already exists
b845f393fc2b: Layer already exists
dbc783c89851: Layer already exists
7bff100f35cb: Layer already exists
1.0-SNAPSHOT: digest: sha256:2d07d5530edcd6436c28068b7c36b78132e49859c2ba4bd78206ed50cdc595d1 size: 2826

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