能够参考博客:https://blog.csdn.net/aixiaoyang168/article/details/77453974
java
docker-maven-plugin官网推荐在新项目中使用dockerfile-maven来构建镜像。git
docker-maven-plugin的Github地址:https://github.com/spotify/docker-maven-plugingithub
dockerfile-maven的Github地址:https://github.com/spotify/dockerfile-mavendocker
1、 使用docker-maven-plugin构建镜像maven
docker-maven-plugin有两种使用方式,一种是使用Dockerfile文件,一种是不使用Dockerfile文件。tcp
1.在 POM中指定构建信息(不使用Dockerfile文件)ui
在pom.xml中引入该插件spa
<!-- docker-maven-plugin插件(不带Dockerfile文件) -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<!--用于指定镜像名称-->
<imageName>${project.name}:${project.version}</imageName>
<!--用于指定基础镜像,至关于Dockerfile中的FROM指令-->
<baseImage>java</baseImage>
<!--至关于Dockerfile的ENTRYPOINT指令-->
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<!--是否跳过docker build-->
<skipDockerBuild>true</skipDockerBuild>
<resources>
<resource>
<targetPath>/</targetPath>
<!--用于指定须要复制的根目录,${project.build.directory}表示target目录-->
<directory>${project.build.directory}</directory>
<!--用于指定须要复制的文件。${project.build.finalName}.jar指的是打包后的jar包文件。-->
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
默认状况下,该插件经过访问localhost:2375来链接本地docker,能够经过设置DOCKER_HOST 环境变量来链接docker..net
DOCKER_HOST=tcp://<host>:2375
2.使用Dockerfile文件插件
若是使用Dockerfile文件,必须指定dockerDirectory元素,那么 baseImage, maintainer, cmd and entryPoint这些元素也会被忽略。dockerDirectory元素所指定的内容将被复制到${project.build.directory}/docker下,resources元素则会复制除此以外的其它文件,例如项目jar文件。
<!--docker-maven-plugin插件(带Dockerfile文件)--> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <configuration> <imageName>${project.name}:${project.version}</imageName> <!--Dockerfile文件位置--> <dockerDirectory>docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin>
2、使用
建立镜像
mvn clean package docker:build
推送镜像到Registry
mvn clean package docker:build -DpushImage
推送指定tag的镜像到Registry
mvn clean package docker:build -DpushImageTag
3、绑定Docker 命令到 Maven 各个阶段