使用 'mvn compile jib:build ' 推送到 Docker 注册表失败

Failure on Push to Docker Registry using 'mvn compile jib:build '

我正在尝试使用 Google JIB maven 插件构建我的 docker 映像并将其部署到私有注册表。但是,它因访问私有注册表而失败。

我已经在我的 Windows10 机器上安装了 Docker Desktop v19.03.1。接下来,我尝试使用 Google JIB Maven 插件来构建我的 Spring 启动应用程序,然后推送在私有注册表上构建的映像。

我已将 'example.registry.com' 的 CERT 添加到 Windows 受信任的 CERTS。我能够使用 docker 命令登录到私有注册表来验证这一点,如下所示:-

docker 登录example.registry.com

此命令returns 身份验证成功,这意味着我添加的 CERT 已被接受。

并且在登录到私有注册表之后,我能够使用 docker 命令 PULLPUSH 图像线到 'example.registry.com' 成功。

下面是我的POM.xml文件。

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>1.5.1</version>
    <configuration>
        <to>
           <image>example.registry.com/inventory-service-docker:1.0</image>
            <auth>
               <username>plaintextusernameforRegistry</username>
               <password>plaintestpasswordforRegistry</password>
           </auth>
        </to>
        <from>                           
             <image>openjdk:8</image>
        </from>
        <container>
           <ports>
              <port>8089</port>
           </ports>
        </container>
        <allowInsecureRegistries>true</allowInsecureRegistries> <!-- this is commented on the first run.-->
    </configuration>
</plugin>

当我从带有 pom.xml 文件的目录从控制台发出 运行 'mvn compile jib:build' 命令时,它确实开始处理,但是在稍后阶段失败并出现以下错误 :-

[ERROR] 执行目标失败 com.google.cloud.tools:jib-maven-plugin:1.5.1:build (default-cli) on project inventory-service-docker: Build image failed, perhaps you should use a registry that supports HTTPS or set the configuration parameter 'allowInsecureRegistries': Failed to verify the server at https://example.registry.com/v2/inventory-service-docker/blobs/sha256:9cc2ad81d40d54dcae7fa5e8e17d9c34e8bba3b7c2cc7e26fb22734608bda32e 因为只有安全连接是允许的。

当我 运行 按照上面的错误建议设置配置参数 'allowInsecureRegistries' 后执行相同的命令时,我得到如下不同的错误:-

[ERROR] 执行目标失败 com.google.cloud.tools:jib-maven-plugin:1.5.1:build (default-cli) on项目库存服务-docker:构建映像失败:无法通过注册表 example.registry.com/inventory-service-docker 进行身份验证,因为:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径

看起来我在使用未安全连接到 'example.registry.com' 的 JIB 时缺少一些配置。

(在我开始之前,请不要使用 Jib 1.5.x,因为这些版本存在网络性能问题。)

JVM 不使用 Windows 证书存储。这个doc

One big problem of the JRE is that it completely ignores the Windows certificate stores. Instead of using the windows certificate store it uses its own implementation

我刚刚在另一个答案中找到的 Windows 的一个可能选项是从 Java 开始 -Djavax.net.ssl.trustStoreType=WINDOWS-ROOT。但我还没有验证自己是否有效。

另一种方法是将您的证书添加到默认的 Java 证书存储 (truststore) 文件 (cacerts)。它通常位于 <JDK>/lib/sercurity/<JRE>/jre/lib/security/.

有多种方法可以添加证书(例如,使用各种 GUI 工具)。例如,

  • 在命令行上
keytool.exe -import -storepass "changeit" -keystore "C:\Program Files (x86)\Java\jre1.8.0_144\lib\security\cacerts" -alias certificate.cer -file "C:\certificate.cer" -noprompt

最后,对于那些想要获得更多解决此类 Java 证书问题的想法的人,请查看此 comment