springboot工程docker -maven-plugin构建docker镜像并push到docker-hub
1. 环境、软件准备
Docker:version 18.09.2
Maven:version 3.6.0
Java: version 1.8.0_91
docker-maven-plugin:1.8.0_191
没有Docker的自行安装
2. pom-docker.xml构建
将工程pom.xml复制重命名为pom-docker.xml,加入以下内容:
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- tag::plugin[] -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<!--docker镜像相关的配置信息-->
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<!--镜像名,这里用工程名 -->
<imageName>docker-hub的登录名/${project.artifactId}</imageName>
<!--Dockerfile文件所在目录-->
<dockerDirectory>${project.basedir}/src/main/resources</dockerDirectory>
<!--TAG,这里用工程版本号-->
<imageTags>
<imageTag>${project.version}</imageTag>
</imageTags>
<forceTags>true</forceTags>
<!--<baseImage>java</baseImage>-->
<!--<entryPoint>["java","-jar","/${project.build.finalName}.jar"]</entryPoint>-->
<!--构建镜像的配置信息-->
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.artifactId}‐${project.version}.jar</include>
</resource>
</resources>
<!-- 以下两行是为了docker push到DockerHub使用的。 -->
<serverId>docker-hub</serverId>
<registryUrl>https://index.docker.io/v1/</registryUrl>
</configuration>
</plugin>
</plugins>
</build>
3. dockerfile配置
FROM java:8
EXPOSE 9005
VOLUME /tmp
COPY system-service-1.0-SNAPSHOT.jar system-service.jar
RUN sh -c 'touch /system-service.jar'
ENTRYPOINT [ "sh", "-c", "java -Djava.security.egd=file:/dev/./urandom -jar /system-service.jar" ]
system-service-1.0-SNAPSHOT.jar跟dockerfile都放置到resources下面
4. maven settings.xml设置
<server>
<id>docker-hub</id>
<username>你的docker-hub的用户名</username>
<password>你的docker-hub的密码</password>
<configuration>
<email>你的docker-hub的邮箱</email>
</configuration>
</server>
<pluginGroup>com.spotify</pluginGroup>
5. DpushImage
mvn -f pom-docker.xml clean package -DpushImage
成功有如下信息:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:16 min
[INFO] Finished at: 2019-07-23T17:43:34+08:00
[INFO] ------------------------------------------------------------------------
在docker-hub仓库中也能看到镜像
6. 注意事项
(1)将pom-docker.xml中的docker-hub的用户名替换成自己的
(2)先maven生成jar包,保证jar的可用
(3)settings.xml 两条配置信息
(4)这是在mac上测试的,window上没有测试