jenkins 自動構建docker 推送映象釋出映象
1、通過pom.xml 進行clean package docker:build
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fintech.platform</groupId>
<artifactId>bootmonitor</artifactId>
<version>1.0.0</version>
<repositories>
<repository>
<id>pt</id>
<name>pt</name>
<url>http://172.18.100.230:8080/nexus-2.5.1/content/groups/public</url>
</repository>
</repositories>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
<fork>true</fork>
</configuration>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.14</version>
<configuration>
<imageName>bootcsp/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<imageTags>
<imageTag>${version}</imageTag>
</imageTags>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、推送docker 映象至倉庫
echo '================開始向docker倉庫推送映象================'
project_id=bootcsp/bootmonitor
#version=latest
docker_registry=172.16.101.43:5000
image_tag=$docker_registry/$project_id:${POM_VERSION}
docker tag $project_id:${POM_VERSION} $image_tag
docker push $image_tag
docker rmi $image_tag
docker rmi $project_id:${POM_VERSION}
imageid=$(docker images |awk '{if($2=="<none>") print $3}')
if [ $imageid != "" ]; then
docker rmi $imageid
fi
curl http://$docker_registry/v2/_catalog
echo '================結束向docker倉庫推送映象================'
3、jenkins遠端執行shell命令,拉去映象,啟動docker
echo '================remote obatin image start================'
CONTAINER_NAME=bootmonitor
docker pull 172.16.101.43:5000/bootcsp/$CONTAINER_NAME:${POM_VERSION}
echo '================remote啟動容器start================'
#刪除同名docker容器
cid=$(docker ps -a | grep "$CONTAINER_NAME" | awk '{print $1}')
if [ "$cid" != "" ]; then
docker rm -f $cid
sleep 3s
fi
docker run -d -p 20000:20000 -m 1024m --name $CONTAINER_NAME 172.16.101.43:5000/bootcsp/$CONTAINER_NAME:${POM_VERSION} --cpu 1
docker exec $CONTAINER_NAME /bin/sh -c "echo 172.18.100.196 eureka1 >> /etc/hosts"
docker exec $CONTAINER_NAME /bin/sh -c "echo 172.18.100.196 eureka2 >> /etc/hosts"
docker exec $CONTAINER_NAME /bin/sh -c "echo 172.18.100.196 bootmonitor >> /etc/hosts"
echo '================remote啟動容器end================'
#docker logs -f $CONTAINER_NAME