解决 Spring boot Docker Mave 插件自动 push 失败

今天在 Spring Boot 中配置,使用 Maven 命令: mvn -X install -P product dockerfile:build,在自动打包的时候,就 push 镜像到 docker hub,但是一直报错:

1
2
3
4
5
6
7
8
[INFO] The push refers to repository [docker.io/yuanmomo/pic-cloud]
..........
..........
denied: requested access to the resource is denied unauthorized: authentication required

[WARNING] An attempt failed, will retry 1 more times
org.apache.maven.plugin.MojoExecutionException: Could not push image
at com.spotify.plugin.dockerfile.PushMojo.execute(PushMojo.java:90)

但是在命令行用 docker push,能推送成功:

1
2
3
4
5
6
7
8
9
» docker push yuanmomo/pic-cloud:1.0.3
The push refers to repository [docker.io/yuanmomo/pic-cloud]
516b24b82955: Pushed
a50347edbcc7: Pushed
6ada88baebc2: Pushed
ceaf9e1ebef5: Layer already exists
9b9b7f3d56a0: Layer already exists
f1b5933fe4b5: Layer already exists
1.0.3: digest: sha256:9dbb0031be9116a144ff3db5347fe517012b5d91edf343eab84a20a68e906f52 size: 1575

看路径都是推送到 docker.io,那没有错,应该是配置的问题。

查看 maven 插件 dockerfile-maven-plugin 的官网文档有说配置认证的方法

看文档的第一句就是,Since version 1.3.0, the plugin will automatically use any configuration in your ~/.dockercfg or ~/.docker/config.json。

~/.dockercfg 这个是旧的配置文件;

~/.docker/config.json 是新的配置文件。那么参考 docker login -h 中的说明,配置config.json 如下:

1
2
3
4
5
6
7
"auths": {
"https://index.docker.io/v1/": {
"username":"用户名,不是邮箱",
"password":"密码"
}
},
........

修改后,保存,再次推送,成功。

基于依赖 Maven config/Settings.xml 来配置认证的请参考插件的文档配置。

Just for my love !!