1. 插件
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.39.1</version>
2. 根目录pom的插件管理配置
<build>
<pluginManagement>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.39.1</version>
<configuration>
<!--配置远程docker守护进程url-->
<dockerHost>tcp://192.168.207.109:2375</dockerHost>
<!-- <dockerHost>tcp://127.0.0.1:2375</dockerHost>-->
<images>
<image>
<!-- 指定image的名字(包含registry地址)-->
<name>ybss-cloud/${project.artifactId}:${project.version}</name>
<alias>${project.artifactId}</alias>
<build>
<!-- 指定dockerfile文件的位置-->
<dockerFile>${project.basedir}/Dockerfile</dockerFile>
<cleanup>true</cleanup>
<noCache>true</noCache>
<buildOptions>
<!-- 网络的配置,与宿主主机共端口号-->
<network>host</network>
<forceRm>true</forceRm>
<noCache>true</noCache>
</buildOptions>
</build>
<run>
<namingStrategy>alias</namingStrategy>
<network>
<name>ybss_default</name>
<alias>${project.artifactId}-net</alias>
</network>
<ports>
<port>${docker.port}</port>
</ports>
<autoRemove>true</autoRemove>
</run>
</image>
</images>
</configuration>
<executions>
<execution>
<id>docker-exec</id>
<!-- 绑定mvn install阶段,当执行mvn install时 就会执行docker build 和docker push-->
<phase>install</phase>
<goals>
<goal>build</goal>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</pluginManagement>
</build>
3. 子模块的pom配置
<properties>
<mainClass>com.xxx.gateway.GatewayApplication</mainClass>
<docker.port>18080:8080</docker.port>
</properties>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
4. Dockerfile内容
Dockerfile文件位置与pom.xml同级
FROM openjdk:8-jre
MAINTAINER xxx
WORKDIR /opt
VOLUME /opt/logs
ENV LANG en_US.UTF-8
ENV LC_ALL en_US.UTF-8
ENV TZ Asia/Shanghai
ADD target/*-gateway-1.0.jar app.jar
ADD target/lib lib
EXPOSE 8080
ENTRYPOINT ["java", "-Dfile.encoding=utf-8", "-Xms1200m", "-Xmx1200m", "-XX:MetaspaceSize=128m", "-XX:MaxMetaspaceSize=128m", "-Xss512K", "-jar", "/opt/app.jar"]
5. 其他子模块类似
6. 部署
mvn clean
mvn package
mvn docker:build
mvn docker:run