无法使用 docker-maven-plugin 推送到 Azure 注册表



我遵循了本教程: https://www.baeldung.com/spring-boot-azure

但是,尽管我在设置中设置了凭据.xml但对于 maven,我仍然收到此错误:

[WARNING] Failed to push apiefrontdoor.azurecr.io/apiefrontdoor, retrying in 10 seconds (5/5).
[INFO] Pushing apiefrontdoor.azurecr.io/apiefrontdoor
The push refers to repository [apiefrontdoor.azurecr.io/apiefrontdoor]
c7f1d24c6c55: Preparing
a46d8ce138df: Preparing
42fd82c87bc8: Preparing
ceaf9e1ebef5: Preparing
9b9b7f3d56a0: Preparing
f1b5933fe4b5: Waiting
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  04:36 min
[INFO] Finished at: 2020-06-02T18:24:11-07:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:1.2.2:build (default-cli) on project apiefrontdoor: Exception caught: unauthorized: authentication required -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.spotify:docker-maven-plugin:1.2.2:build (default-cli) on project apiefrontdoor: Exception caught
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: org.apache.maven.plugin.MojoExecutionException: Exception caught
at com.spotify.docker.AbstractDockerMojo.execute (AbstractDockerMojo.java:151)
at com.spotify.docker.BuildMojo.execute (BuildMojo.java:329)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: com.spotify.docker.client.exceptions.DockerException: unauthorized: authentication required
at com.spotify.docker.client.AnsiProgressHandler.progress (AnsiProgressHandler.java:55)
at com.spotify.docker.Utils$DigestExtractingProgressHandler.progress (Utils.java:191)
at com.spotify.docker.client.ProgressStream.tail (ProgressStream.java:77)
at com.spotify.docker.client.DefaultDockerClient$ResponseTailReader.call (DefaultDockerClient.java:2754)
at com.spotify.docker.client.DefaultDockerClient$ResponseTailReader.call (DefaultDockerClient.java:2738)
at java.util.concurrent.FutureTask.run (FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:624)
at java.lang.Thread.run (Thread.java:748)
[ERROR]
[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

这是我的绒球.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.apieproject</groupId>
<artifactId>apiefrontdoor</artifactId>
<version>0.0.1</version>
<name>frontdoor</name>
<description>APIE Project Landing Project</description>
<properties>
<jib-maven-plugin.version>2.3.0</jib-maven-plugin.version>
<azure.containerRegistry>apiefrontdoor</azure.containerRegistry>
<docker.image.prefix>apiefrontdoor.azurecr.io</docker.image.prefix>
<java.version>1.8</java.version>
</properties>
<dependencies>
....
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>jib-maven-plugin</artifactId>
<groupId>com.google.cloud.tools</groupId>
<version>${jib-maven-plugin.version}</version>
<configuration>
<from>
<image>mcr.microsoft.com/java/jre:8-zulu-alpine</image>
</from>
<to>
<image>${docker.image.prefix}/${project.artifactId}</image>
</to>
</configuration>
</plugin>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-webapp-maven-plugin</artifactId>
<version>1.9.1</version>
<configuration>
<authentication>
<serverId>azure-auth</serverId>
</authentication>
<resourceGroup>apieproject</resourceGroup>
<appName>apiefrontdoor</appName>
<region>Central US</region>
<containerSettings>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
</containerSettings>
<appServicePlanName>ASP-apieproject-ae27 (P1v2: 1)</appServicePlanName>
<appSettings>
<property>
<name>PORT</name>
<value>8080</value>
</property>
</appSettings>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.2</version>
<configuration>
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<serverId>${azure.containerRegistry}</serverId>
<registryUrl>https://${docker.image.prefix}</registryUrl>
<dockerDirectory>docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>

这是我的设置.xml

<servers>
<server>
<id>azure-auth</id>
<configuration>
<client>my-client</client>
<tenant>my-tenant</tenant>
<key>my-key</key>
<environment>AZURE</environment>
</configuration>
</server>
<server>
<id>apiefrontdoor</id>
<username>my-username</username>
<password>my-password</password>
</server>
</servers>

我可以使用"docker 登录"独立登录到 Azure 容器注册表,因此这不是凭据问题。但是,尽管项目成功构建,但我无法将其推送到同一注册表。请协助。

最有可能的是,问题在于服务器中用于 settings.xml file 和 docker.image.prefix 的 id,pom.xml 文件中。它完全匹配。阅读 url 中的示例以了解更多信息。

我能够根据此讨论线索解决此问题

  • 如讨论中所述,应创建具有 AcrPush 和 AcrImageSigner 权限的 Azure 容器注册表服务主体。因此,请不要为此使用容器注册表访问密钥。
  • 之后,而不是像这样运行构建和推送的组合:

    mvn docker:build -DpushImage
    
  • 当然,在登录到docker注册表后,我将命令分为两个不同的命令:

    a) mvn docker:build
    b) docker push <repo_name>.azurecr.io/<repo_name>:latest
    

注意:"最新"已配置为我的标签。如果您使用其他标记进行版本控制,请将"latest"替换为您刚刚构建的最新版本

最新更新