在尝试使用Maven 插件docker-maven-plugin 打包推送镜像的时候

docker-maven-plugin插件push 一直失败,最终推送失败。

[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:1.2.0:build (default-cli) on project discovery-service: Exception caught: no basic auth credentials -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

报了找不到凭证的异常。

我们到该插件开发作者的githubissues - mvn -DpushImage Fail : no basic auth credentials#309

tdelesio commented on 1 Apr 2017: One thing I would recommend that really tripped me up was the version of the plugin. I was using a very old plugin version and it was giving me this error. When I changed to the current version it magiclly worked.

这个问题似乎是由于版本问题导致的,所以使用低版本就可以解决问题,这里为了解决这个问题我使用1.1.1的版本。配置如下

<properties>
	<docker.registry>locahost:5000</docker.registry>
</properties>
<plugins>
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.1.1</version>
    <configuration>
        <imageName>${docker.registry}/${project.name}:${project.version}</imageName>
        <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
        <skipDockerBuild>false</skipDockerBuild>
        <pushImage>true</pushImage>
        <serverId>nexus</serverId>
        <registryUrl>${docker.registry}</registryUrl>
        <resources>
            <resource>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>
</plugins>

该配置中,配置了远程私有私服的相关信息。(如认证凭证和仓库地址)

<serverId>nexus</serverId>
<registryUrl>${docker.registry}</registryUrl>

由于使用的是私服,所以还需要在Maven的配置文件settings.xml中设置,serviceId配置私服认证的ID和口令。

<servers>
    <server>
      <id>nexus</id>
      <username>admin</username>
      <password>admin</password>
    </server>
</servers>

这样在pom.xml的文件中运行下面命令,就可以自动打包并且推送到私服仓库中

mvn clean package docker:build
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐