<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<!--镜像名称-->
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<!--指定标签-->
<imageTags>
<imageTag>latest</imageTag>
</imageTags>
<!--基础镜像 jdk1.8-->
<baseImage>java:8</baseImage>
<!--制作者本人信息-->
<maintainer>alexyoung</maintainer>
<!--切换到/ROOT目录-->
<workdir>/ROOT</workdir>
<cmd>["java","-version"]</cmd>
<entryPoint>["java","-jar","${project.build.finalName}.jar"]</entryPoint>
<!--指定远程docker api地址-->
<dockerHost>http://xxx.xxx.xxx.xxx:2375</dockerHost>
<!--复制jar包到docker容器指定目录-->
<resources>
<resource>
<targetPath>/ROOT</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>
用以上docker-maven插件,会自动生成Dockerfile文件
对项目进行打包,并构建镜像到docker上,执行以下命令
mvn clean package docker:build
PS:
IDEA内置terminal:
Run Commands using IDE
Press Ctrl+Enter to run the highlighted action using the relevant IDE feature instead of the terminal.
Press Ctrl+Shift+Enter for debug. Press Enter to run the command in the terminal as usual.
集成docker扩展:一键打包构建镜像
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<!--镜像名称-->
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<!--指定标签-->
<imageTags>
<imageTag>latest</imageTag>
</imageTags>
<!--基础镜像 jdk1.8-->
<baseImage>java:8</baseImage>
<!--制作者本人信息-->
<maintainer>xxxx</maintainer>
<!--切换到/ROOT目录-->
<workdir>/ROOT</workdir>
<cmd>["java","-version"]</cmd>
<entryPoint>["java","-jar","${project.build.finalName}.jar"]</entryPoint>
<!--指定Dockerfile路径-->
<!--<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>-->
<!--指定远程docker api地址-->
<dockerHost>http://xxxx.xxx.xxx:2375</dockerHost>
<!--复制jar包到docker容器指定目录-->
<resources>
<resource>
<targetPath>/ROOT</targetPath>
<!--用于指定需要复制的根目录,${project.build.directory}表示target目录-->
<directory>${project.build.directory}</directory>
<!--用于指定需要复制的文件,${project.build.finalName}.jar指的是打包后的jar包文件-->
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
<executions>
<!--当执行mvn package时,执行mvn clean package docker:build-->
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
注意:在配置docker 远程连接使用ca验证后,记得在项目的pom文件中将原先
<!--指定远程 docker api地址-->
<dockerHost>http://docker所在服务器公网ip:端口</dockerHost>
改为:
<!--指定远程 docker api地址-->
<dockerHost>https://docker所在服务器公网ip:端口</dockerHost>
<!--指定客户端远程连接docker的pem证书文件路径地址-->
<dockerCertPath>本地ca文件所在路径</dockerCertPath>