目录Dockerfile文件pom.xmlJenkins配置
spring boot项目通过Jenkins集成构建Docker镜像推送到harbor仓库并启动容器然后拉取镜像到本地运行容器
Dockerfile文件
位置:存在根目录下,与src同级
FROM java:8 # 作者 MAINTAINER zhaoyc VOLUME /tmp #ARG JAR_FILE #将jar包添加到容器中并更名为app.jar ADD target/*.jar app.jar RUN bash -c touch /app.jar ENV TZ=Asia/Shanghai #RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone EXPOSE 81 #CMD [“-jar”, “/app.jar”] “nohup” “&” ENTRYPOINT [“java”,”-Djava.security.egd=file:/dev/./urandom”,”-jar”,”/app.jar”]pom.xml
要指定maven版本号为1.4.2,其他版本未测试,否则找不到main-class
<plugins> <!– mavn jar包启动插件–> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.4.2.RELEASE</version> </plugin> </plugins>Jenkins配置
系统设置配置docker–需要安装Docker plugin
新建maven项目 配置参数构建
项目git仓库配置
mvn构建 buile配置
构建docke镜像推送至仓库配置
推送到仓库后执行脚本
#!/bin/bash #IMAGE=${module_filename} #TAG=${Tag} IMAGE=$1 TAG=$2 PULL_IMAGE=192.168.1.100:81/project/${IMAGE}:${TAG} # 计算减法,并输出带有小数点前面的0, 比如 0.3-0.1=0.2 CURRENT_IAMGE=`echo “$TAG – 0.1” | bc | awk {printf “%.1f”, $0}` echo ================开始登陆Harbor仓库================ docker login http://192.168.1.100:81 -u admin -p Harbor12345 if [ $? -eq 0 ]; then echo ================登陆成功================ else echo ================登陆失败================ return 1 fi echo ================开始拉取镜像================ docker pull ${PULL_IMAGE} echo ================结束拉取镜像================ # 更新容器操作 echo ================停止正在运行该镜像================ docker stop ${IMAGE}_${CURRENT_IAMGE} echo “等待10秒” sleep 10 echo ================开始启动镜像================ #if [ ${IMAGE} == “java-demo” ]; then # docker run -d –name ${IMAGE}_${TAG} -p 9001:9001 ${PULL_IMAGE} #elif [ ${IMAGE} == “spring-cloud-eureka-client-demo” ]; then # docker run -d –name ${IMAGE}_${TAG} -p 9012:9012 ${PULL_IMAGE} #fi docker run -d –name ${IMAGE}_${TAG} -p 9001:9001 ${PULL_IMAGE} if [ $? -eq 0 ]; then echo ================镜像启动成功================ else echo ================镜像启动失败================ return 1 fi© 版权声明
THE END
暂无评论内容