代码配置及Dockerfile文件
- pom依赖
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--加入下面两项配置-->
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>springboot/${project.artifactId}</imageName>
<dockerDirectory>${project.basedir}</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
- Dockerfle文件(和pom文件同级)
# use java:8
FROM java:8
#引用pom中JAR_FILE参数(为jar包名称)
ARG JAR_FILE
# 添加target目录下的jar包 并命名为 app.jar
ADD ${JAR_FILE} /app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom", "-jar","/app.jar"]
jenkins配置(需安装maven和git插件)
- 配置git仓库
- 打包并打成镜像
- 打包成功后执行shell 运行镜像
itemId=`docker ps -a|grep demo|awk '{print $1}'`
if [ ! $itemId ];then
echo "服务已是停止状态"
else
echo '停止镜像 ID:'+$itemId
docker stop $itemId
docker rm $itemId
fi
echo "开始启动服务。。。。。。。"
docker run -d -p 8820:8080 -m 512m --name demo springboot/demo:latest
echo "服务启动成功。。。。。。。"