jenkins springboot打包docker镜像并部署(本机)

代码配置及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 "服务启动成功。。。。。。。"

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值