You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Spring Boot Maven插件推送镜像到hub.docker.com失败求助

问题原因与解决方案

报错1:denied: requested access to the resource is denied 核心原因

你生成的镜像名称没有添加Docker Hub用户名作为前缀,Spring Boot插件默认生成的镜像格式为[项目组名]/[应用名]:[版本],直接推送会尝试写入Docker Hub的公共根镜像库library/,你没有该路径的写入权限,因此被拒绝。

报错2:unauthorized: refresh_token grants not allowed 核心原因

Spring Boot 2.5.x版本的Maven插件中,<publishRegistry>下的<token>字段是用于OAuth2刷新令牌授权场景,Docker Hub的个人访问令牌(PAT)属于密码类凭证,不能填入<token>标签,否则插件会调用Docker Hub不支持的刷新令牌授权流,触发该报错。

正确配置示例

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.5.3</version>
    <configuration>
        <!-- 必须配置:镜像名前缀为你的Docker Hub用户名 -->
        <image>
            <name>me/${project.artifactId}:${project.version}</name>
        </image>
        <docker>
            <publishRegistry>
                <url>https://index.docker.io/v1/</url>
                <username>me</username>
                <!-- Docker Hub个人访问令牌直接填入password字段 -->
                <password>hub.docker.com-generated-token</password>
                <email>me@gmail.com</email>
            </publishRegistry>
        </docker>
    </configuration>
    <!-- 绑定package阶段自动构建镜像,按需保留 -->
    <executions>
        <execution>
            <goals>
                <goal>build-image</goal>
            </goals>
            <phase>package</phase>
        </execution>
    </executions>
</plugin>

额外注意事项

  • 生成Docker Hub个人访问令牌时,必须勾选write:packages、read:packages权限,确保令牌有镜像推送权限
  • 不建议将凭证硬编码在pom.xml中,可通过Maven属性从环境变量、settings.xml的server配置中读取,执行命令时也可通过参数传递:mvn package -Ddocker.password=你的令牌
  • 配置前可先执行手动推送验证权限:docker tag 本地镜像名 me/镜像名:版本 && docker push me/镜像名:版本,确认手动推送正常后再运行Maven构建

内容的提问来源于stack exchange,提问作者user3465651

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 12:45:03