Maven编译Docker 镜像并推送到Nexus
[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/
前提条件:
- 有一台电脑 安装了maven
- 安装了jdk1.8 以上版本
- 安装了 git
- 安装了intellij idea
- 电脑能上网
- 有一台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>11394019@qq.com</email>
</configuration>
</server>
- 配置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
更多推荐
所有评论(0)