工欲善其事,必先利其器。笔者经过调研,有以下几款Docker的Maven插件进入笔者视野:
插件名称 | 官方地址 |
---|---|
docker-maven-plugin | https://github.com/spotify/docker-maven-plugin |
docker-maven-plugin | https://github.com/fabric8io/docker-maven-plugin |
docker-maven-plugin | https://github.com/bibryam/docker-maven-plugin |
笔者从Stars、文档易用性以及更新频率三个纬度考虑,选用了第一款。
我们以之前的项目:microservice-discovery-eureka为例:
<build>
<plugins>
<plugin>
<groupId>com.spotifygroupId>
<artifactId>docker-maven-pluginartifactId>
<version>0.4.12version>
<configuration>
<imageName>microservice-discovery-eurekaimageName>
<baseImage>javabaseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]entryPoint>
<resources>
<resource>
<targetPath>/targetPath>
<directory>${project.build.directory}directory>
<include>${project.build.finalName}.jarinclude>
resource>
resources>
configuration>
plugin>
plugins>
build>
mvn clean package docker:build
[INFO] Building image microservice-discovery-eureka
Step 1 : FROM java
Pulling from library/java
Digest: sha256:581a4afcbbedd8fdf194d597cb5106c1f91463024fb3a49a2d9f025165eb675f
Status: Downloaded newer image for java:latest
---> ea40c858f006
Step 2 : ADD /microservice-discovery-eureka-0.0.1-SNAPSHOT.jar //
---> d1c174083bca
Removing intermediate container 91913d847c20
Step 3 : ENTRYPOINT java -jar /microservice-discovery-eureka-0.0.1-SNAPSHOT.jar
---> Running in 0f2aeccdfd46
---> d57b027ca65a
Removing intermediate container 0f2aeccdfd46
Successfully built d57b027ca65a
[INFO] Built microservice-discovery-eureka
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:38 min
[INFO] Finished at: 2016-09-18T01:05:05-07:00
[INFO] Final Memory: 40M/198M
恭喜,构建成功了。
docker images
会发现该镜像已经被构建成功:REPOSITORY TAG IMAGE ID CREATED SIZE
microservice-discovery-eureka latest d57b027ca65a About a minute ago 681.5 MB
docker run -p 8761:8761 microservice-discovery-eureka
我们会发现该Docker镜像会很快地启动。
访问http://Docker宿主机IP:8761 ,能够正常看到Eureka界面。
上文讲述的方式是最简单的方式,很多时候,我们还是要借助Dockerfile进行构建的,首先我们在/microservice-discovery-eureka/src/main/docker目录下,建立文件Dockerfile
FROM java:8
VOLUME /tmp
ADD microservice-discovery-eureka-0.0.1-SNAPSHOT.jar app.jar
RUN bash -c 'touch /app.jar'
EXPOSE 9000
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
修改pom.xml
<build>
<plugins>
<plugin>
<groupId>com.spotifygroupId>
<artifactId>docker-maven-pluginartifactId>
<version>0.4.12version>
<configuration>
<imageName>microservice-discovery-eureka-dockerfileimageName>
<dockerDirectory>${project.basedir}/src/main/dockerdockerDirectory>
<resources>
<resource>
<targetPath>/targetPath>
<directory>${project.build.directory}directory>
<include>${project.build.finalName}.jarinclude>
resource>
resources>
configuration>
plugin>
plugins>
build>
其他步骤一样。这样即可使用Dockerfile进行构建Docker镜像啦。
<servers>
<server>
<id>docker-hubid>
<username>你的DockerHub用户名username>
<password>你的DockerHub密码password>
<configuration>
<email>你的DockerHub邮箱email>
configuration>
server>
servers>
<build>
<plugins>
<plugin>
<groupId>com.spotifygroupId>
<artifactId>docker-maven-pluginartifactId>
<version>0.4.12version>
<configuration>
<imageName>eacdy/testimageName>
<dockerDirectory>${project.basedir}/src/main/dockerdockerDirectory>
<resources>
<resource>
<targetPath>/targetPath>
<directory>${project.build.directory}directory>
<include>${project.build.finalName}.jarinclude>
resource>
resources>
<serverId>docker-hubserverId>
<registryUrl>https://index.docker.io/v1/registryUrl>
configuration>
plugin>
plugins>
build>
mvn clean package docker:build -DpushImage
在很多场景下,我们需要将镜像push到私有仓库中去,这边为了讲解的全面性,私有仓库采用的是配置登录认证的私有仓库。
<servers>
<server>
<id>docker-registryid>
<username>你的DockerHub用户名username>
<password>你的DockerHub密码password>
<configuration>
<email>你的DockerHub邮箱email>
configuration>
server>
servers>
<plugin>
<groupId>com.spotifygroupId>
<artifactId>docker-maven-pluginartifactId>
<version>0.4.12version>
<configuration>
<imageName>reg.itmuch.com/test-pull-registryimageName>
<dockerDirectory>${project.basedir}/src/main/dockerdockerDirectory>
<resources>
<resource>
<targetPath>/targetPath>
<directory>${project.build.directory}directory>
<include>${project.build.finalName}.jarinclude>
resource>
resources>
<serverId>docker-registryserverId>
configuration>
plugin>
mvn clean package docker:build -DpushImage
稍等片刻,将会push成功。
docker login reg.itmuch.com # 然后输入账号和密码
docker pull reg.itmuch.com/test-pull-registry
在很多场景下,我们有这样的需求,例如执行mvn clean package
时,自动地为我们构建docker镜像,可以吗?答案是肯定的。我们只需要将插件的goal
绑定在某个phase即可。
所谓的phase和goal,可以这样理解:maven命令格式是:mvn phase:goal
,例如mvn package docker:build
那么,package
和docker
都是phase,build
则是goal 。
下面是示例:
首先配置属性:
.image.prefix>reg.itmuch.com .image.prefix>
插件配置:
<build>
<plugins>
<plugin>
<groupId>com.spotifygroupId>
<artifactId>docker-maven-pluginartifactId>
<executions>
<execution>
<id>build-imageid>
<phase>packagephase>
<goals>
<goal>buildgoal>
goals>
execution>
executions>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}imageName>
<baseImage>javabaseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]entryPoint>
<resources>
<resource>
<targetPath>/targetPath>
<directory>${project.build.directory}directory>
<include>${project.build.finalName}.jarinclude>
resource>
resources>
configuration>
plugin>
plugins>
build>
如上,我们只需要添加:
<executions>
<execution>
<id>build-imageid>
<phase>packagephase>
<goals>
<goal>buildgoal>
goals>
execution>
executions>
即可。本例指的是讲docker的build目标,绑定在package这个phase上。也就是说,用户只需要执行mvn package
,就自动执行了mvn docker:build
。
Connect to localhost:2375 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect -> [Help 1]
解决步骤:
docker-machine env
$Env:DOCKER_TLS_VERIFY = "1"
$Env:DOCKER_HOST = "tcp://192.168.99.100:2376"
$Env:DOCKER_CERT_PATH = "C:\Users\Administrator\.docker\machine\machines\default
<dockerHost>https://192.168.99.100:2376dockerHost> <dockerCertPath>C:\Users\Administrator\.docker\machine\machines\defaultdockerCertPath>
修改后插件配置变为:
<plugin>
<groupId>com.spotifygroupId>
<artifactId>docker-maven-pluginartifactId>
<version>0.4.12version>
<configuration>
<imageName>eacdy/testimageName>
<dockerDirectory>${project.basedir}/src/main/dockerdockerDirectory>
<dockerHost>https://192.168.99.100:2376dockerHost>
<dockerCertPath>C:\Users\Administrator\.docker\machine\machines\defaultdockerCertPath>
<resources>
<resource>
<targetPath>/targetPath>
<directory>${project.build.directory}directory>
<include>${project.build.finalName}.jarinclude>
resource>
resources>
<serverId>docker-hubserverId>
<registryUrl>https://index.docker.io/v1/registryUrl>
configuration>
plugin>
DOCKER_HOST=tcp://:2375
https://git.oschina.net/itmuch/spring-cloud-study/tree/master/docker/microservice-discovery-eureka
https://github.com/eacdy/spring-cloud-study/tree/master/docker/microservice-discovery-eureka
http://developer.51cto.com/art/201404/434879.htm
https://linux.cn/article-6131-rss.html