通过maven插件构建docker镜像:
1、在pom.xml 中添加maven的docker插件。
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>jingdata/microservice-discovery-eureka:0.0.1</imageName>
<baseImage>java</baseImage>
<entryPoint>["java","-jar","/{project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
2、构建docker镜像: mvn clean package docker:build
3、执行docker images 查看刚构建的镜像
4、启动镜像: docker run -d -p 8761:8761 jingdata/microservice-discovery-eureka:0.0.1
5、访问测试: 128.0.0.1:8761
插件读取dockerfile进行构建:
1、在src/main目录下,新建文件夹docker,在文件夹中新建一个Dockerfile文件。
FROM java:8
VOLUME /tmp
ADD microservice-discovery-erueka-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文件:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>jingdata/microservice-discovery-eureka:0.0.2</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
2.将插件绑定在没某个phase执行
<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>
</executions>
<configuration>
<imageName>jingdata/microservice-discovery-eureka:0.0.3</imageName>
<baseImage>java</baseImage>
<entryPoint>["java","-jar","/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
3.推送镜像
(1)、修改maven的全局配置文件settings.xml,在其中添加docker hub的用户信息。
<server>
<id>docker-hub</id>
<username>docker-hub的用户名</username>
<password>docker-hub的密码</password>
<configuration>
<email>docker-hub的邮箱</email>
</configuration>
</server>
(2)、修改pom.xml文件
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>jingdata/microservice-discovery-eureka:0.0.4</imageName>
<baseImage>java</baseImage>
<entryPoint>["java","-jar","/{project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<!-- 与maven配置文件settings中配置的server id一致,用于推送镜像--->
<serverId>docker-hub</serverId>
</configuration>
</plugin>
(3)、添加pushImage标示,推送镜像
mvn clean package docker:build -DpushImage
(4)、推送到私有仓库地址:
<imageName>localhost:5000/jingdata/microservice-discovery-eureka:0.0.4</imageName>