当前位置: 移动技术网 > IT编程>开发语言>Java > maven docker 插件集成的几个小坑

maven docker 插件集成的几个小坑

2018年08月26日  | 移动技术网IT编程  | 我要评论
昨晚看springboot视频的时候,发现可以使用docker maven plugin这个插件直接build出 docker 镜像到远程服务器上,感觉很方便,于是自己也试了一下,但是碰到了几个问题,记录一下。 一、开启docker远程端口 视频中使用的是centos,然而我是Ubuntu。好吧,其 ...

昨晚看springboot视频的时候,发现可以使用docker-maven-plugin这个插件直接build出 docker 镜像到远程服务器上,感觉很方便,于是自己也试了一下,但是碰到了几个问题,记录一下。

一、开启docker远程端口

视频中使用的是centos,然而我是ubuntu。好吧,其实没啥区别,但是还是有一点小区别的,,比如文件位置不同。

ubuntu下需要编辑的文件为

vim /etc/default/docker

在最后一行加上

docker_opts="-h unix:///var/run/docker.sock -h tcp://0.0.0.0:6732"

好,我们把端口设置为了6732,视频中就讲了这个,可能是系统原因,此时我本地用 telnet 访问这个端口是不通的。

我们还需要

 vim /lib/systemd/system/docker.service

增加一行

environmentfile=-/etc/default/docker

指定使用我们刚才编辑的文件

然后修改

execstart=/usr/bin/dockerd -h fd:// 

execstart=/usr/bin/dockerd -h fd:// $docker_opts

其实就是指定使用我们刚才编辑的参数

好了,此时telnet 通了。

二、配置pom文件

按照视频中的写法

在properties中增加一行指定远程主机的位置,端口为我们刚才配置的6732

    <properties>
        <project.build.sourceencoding>utf-8</project.build.sourceencoding>
        <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
        <java.version>1.8</java.version>
        <dockerhost>http://x.x.x.x:6732</dockerhost>
    </properties>

然后增加一个plugin

<plugin>
    <groupid>com.spotify</groupid>
    <artifactid>docker-maven-plugin</artifactid>
    <version>1.0.0</version>
    <configuration>     
        <!--  imagename>maventest</imagename>  -->                  
        <imagename>java</imagename>
        <baseimage>java</baseimage>
        <entrypoint>["java", "-jar", "/${project.build.finalname}.jar"]</entrypoint>
        <resources>
            <resource>
                <targetpath>/</targetpath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalname}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>

然后我们执行 mvn -dskiptests clean package docker:build
好,build成功。

问题来了,按照这样写是没问题的。

但是我突然想到怎么指定我build出来的image名称呢。然后我就把imagename改为了maventest

然后就报错了。。。

[error] failed to execute goal com.spotify:docker-maven-plugin:0.4.3:build (default-cli) on project maventest: exception caught: java.util.concurrent.executionexception: com.spotify.docker.client.shaded.javax.ws.rs.processingexception: org.apache.http.client.clientprotocolexception: cannot retry request with a non-repeatable request entity: connection reset by peer -> [help 1]

看这个错一脸懵逼,链接出错???刚还不是好的吗。又把名字换成java,又可以了。。于是百度了一下,有的说升级版本成0.4.4就可以了,有的说升级成1.0.0可以,然而,并没有什么用。

百度提供的有效信息就这么多了,,

于是去 github上看了下,终于在 issues 中发现这样一个问题,

user could be warned if an illegal image name is specified

i have struggled for some time now with a strange problem, where mvn docker:build failed; amongst the error output, "connection reset" was found. i finally figured out what caused the issue: my maven project's artifact id had upper-case letters in it, and i am using ${myprefix}/${project.artifactid} as the image name. however, docker seems to not allow upper-case letters for images names.

看起来是我一样的问题,他说他的项目名中有大写字母,,,,,看到这里,恍然大悟,把imagename 改成 ttt 试了下,果然可以。。

最后想说一下,,其实这个错误是不应该犯得,因为视频中讲了image的命名规范

only [a-z0-9-_.]

没仔细看,,浪费一个小时。。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网