【问题标题】:dockerfile-maven plugin and GCRdockerfile-maven 插件和 GCR
【发布时间】:2021-05-07 19:10:44
【问题描述】:

我按照步骤将 docker 镜像推送到 Google Cloud Registry here

我可以手动运行docker push eu.gcr.io/[PROJECT_ID]/[IMAGE_TAG],它会成功运行。但是,dockerfile maven 插件会由于缺少凭据而失败:

org.apache.maven.plugin.MojoExecutionException: Could not push image
...
Caused by: com.spotify.docker.client.exceptions.DockerException: unauthorized: You don't have the needed permissions to perform this operation, and you may have invalid credentials. To authenticate your request, follow the steps in: https://cloud.google.com/container-registry/docs/advanced-authentication

此时,我的~/.docker/config.json 文件看起来像:

{
  "credHelpers": {
    "gcr.io": "gcloud",
    "us.gcr.io": "gcloud",
    "eu.gcr.io": "gcloud",
    "asia.gcr.io": "gcloud",
    "staging-k8s.gcr.io": "gcloud",
    "marketplace.gcr.io": "gcloud"
  }
}

经过大量调试,我找到了一个可行的配置:

{
  "auths": {
    "eu.gcr.io": {}
  },
  "credHelpers": {
    "eu.gcr.io": "gcr"
  },
  "credsStore": "gcr"
}

我必须安装docker-credential-gcr 模块:

gcloud components install docker-credential-gcr

我有两个问题:

  1. 为什么我需要以这种方式更改我的配置?
  2. 是否值得在 GitHub 上提出问题,或者这是我设置的一个奇怪的怪癖?

编辑:

我的 Maven 配置的相关部分是:

        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>1.4.13</version>
            <configuration>
                <repository>eu.gcr.io/[PROJECT_ID]/[IMAGE_NAME]</repository>
                <tag>2.0.0</tag>
            </configuration>
            <executions>
                <execution>
                    <id>default</id>
                    <goals>
                        <goal>build</goal>
                        <goal>push</goal>
                    </goals>
                </execution>
                <execution>
                    <id>tag-image-latest</id>
                    <goals>
                        <goal>tag</goal>
                    </goals>
                    <configuration>
                        <tag>latest</tag>
                    </configuration>
                </execution>
            </executions>
        </plugin>

【问题讨论】:

    标签: docker maven google-container-registry dockerfile-maven-plugin


    【解决方案1】:

    尝试在配置标签中添加&lt;useMavenSettingsForAuth&gt;true&lt;/useMavenSettingsForAuth&gt;,并在 maven settings.xml 中指定您的服务器凭据:

    <server>
       <id>docker.io</id>
       <username>XXX</username>
       <password>XXX</password>
    </server>
                                    
    

    此外,如果您想将映像推送到私有注册表,您需要两件事:API 访问范围使用注册表验证您的虚拟机

    this post已经有详细解答,你可以深入了解你的问题。

    希望对您有所帮助。

    【讨论】:

    • 感谢您的回答,但问题不在于通过注册表进行身份验证 - 我可以毫无问题地执行 docker push。我只在使用 maven dockerfile 插件时遇到问题。
    • 我已经编辑了上面的答案,提供了有关 maven dockerfile 插件问题的更多信息。让我知道该方法是否有效!
    猜你喜欢
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    相关资源
    最近更新 更多