docker mvn
1、配置Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>JttDataTransfer.server</groupId>
<artifactId>JttDataTransfer.server</artifactId>
<version>0.0.2-SNAPSHOT</version>
<packaging>war</packaging>
<name>JttDataTransfer.server</name>
<description/>
<properties>
<webVersion>2.5</webVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<docker.repostory>10.0.2.4:5000</docker.repostory>
<docker.dockerhost>http://10.0.2.4:5000</docker.dockerhost>
<docker.registry.name>dszhang</docker.registry.name>
<docker.artifactId>jttdatatransfer.server</docker.artifactId>
<!-- 指定docker image 的版本号 -->
<docker.version>0.0.1</docker.version>
<docker.active>test</docker.active>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
<executions>
<execution>
<id>copy-spring-boot-resources</id>
<!-- 根据-Ddocker.active 参数复制相应目录的配置文件 -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>utf-8</encoding>
<outputDirectory>${basedir}/WebRoot/WEB-INF/conf</outputDirectory>
<resources>
<resource>
<directory>${basedir}/docker/${docker.active}/conf</directory>
<includes>
<include>settings.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
<!--<compilerArguments>
<extdirs>lib</extdirs>
</compilerArguments> -->
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<webResources>
<resource>
<!--配置本地jar包在项目中的存放路径-->
<directory>${basedir}/WebRoot/</directory>
<!--配置打包时jar包的存放路径-->
<targetPath>/</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
<!-- 打包Docker -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>tag-image</id>
<phase>package</phase>
<goals>
<goal>tag</goal>
</goals>
<configuration>
<image> ${docker.repostory}/${docker.registry.name}/${docker.artifactId}:${docker.version}</image>
<newName>${docker.repostory}/${docker.registry.name}/${docker.artifactId}:${docker.version}</newName>
</configuration>
</execution>
<execution>
<id>push-image</id>
<phase>deploy</phase>
<goals>
<goal>push</goal>
</goals>
<configuration>
<imageName>${docker.repostory}/${docker.registry.name}/${docker.artifactId}:${docker.version}</imageName>
</configuration>
</execution>
</executions>
<configuration>
<serverId>docker-local</serverId>
<registryUrl>${docker.dockerhost}</registryUrl>
<pushImage>true</pushImage>
<dockerDirectory>${project.basedir}/docker/${docker.active}/build</dockerDirectory>
<imageName>${docker.repostory}/${docker.registry.name}/${docker.artifactId}</imageName>
<imageTags>
<imageTag>${docker.version}</imageTag>
</imageTags>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${basedir}/target/</directory>
<include>${project.artifactId}.war</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
<finalName>JttDataTransfer.server</finalName>
</build>
</project>
二、创建Dockerfile
1、在project目录下 mkdir docker
2、vi ./docker/prod/build/Dockerfile 文件
vi ./docker/test/build/Dockerfile 文件
FROM 10.0.2.4:5000/dszhang/tomcat
MAINTAINER dszhang "1307020440@qq.com"
ADD JttDataTransfer.server.war /usr/tomcat/apache-tomcat-8.5.43/webapps/
ENTRYPOINT ["/usr/tomcat/apache-tomcat-8.5.43/bin/catalina.sh","run"]
#CMD ["catalina.sh", "run"]
三、修改 /.m2/settings.xml
mvn -v
Apache Maven 3.0.5 (Red Hat 3.0.5-17)
Maven home: /usr/share/maven
Java version: 1.8.0_212, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.12.2.el7.x86_64", arch: "amd64", family: "unix"
修改settings.xml
vi /usr/share/maven/conf/settings.xml
#增加docker registry服务器配置
<server>
<id>docker-local</id>
<username>dszhang@vip.qq.com</username>
<password>123456</password>
<configuration>
<email>dszhang@vip.qq.com</email>
</configuration>
</server>
四、打包到docker
打包测试环境docker
mvn clean install -Ddocker.active=test
打包正式环境docker
mvn clean install -Ddocker.active=prod