1.准备好环境和远程镜像仓库
2.编写shell脚本
#!/bin/bash -il
#操作/项目路径(Dockerfile存放的路劲)
BASE_PATH=/var/jenkins_home/workspace/xrt
#源jar路径
SOURCE_PATH=/var/jenkins_home/workspace/xrt/target
#容器id
CID=$(docker ps | grep "app" | awk '{print $1}')
DATE=`date +%Y%m%d%H%M`
#构建docker镜像
function build(){
echo "开始构建镜像..."
cd $BASE_PATH
docker build -t app .
}
#运行docker容器
function run(){
backup
transfer
build
if [ -n "$CID" ]; then
echo "存在demo容器,CID=$CID,重启docker容器 ..."
docker stop $CID
docker rm $CID
docker run -d -p 8783:8783 app
echo "demo容器重启完成"
else
echo "不存在demo容器,docker run创建容器..."
docker run --name app -d -p 8783:8783 app
echo "demo容器创建完成"
fi
delnone
echo "push镜像到docker镜像仓库"
docker login http://ip:6001 -u admin -p xxx
docker tag app ip:6001/xrt/app:$DATE
docker push ip:6001/xrt/app:$DATE
echo "成功push镜像到docker镜像仓库"
}
#删除none镜像
function delnone(){
docker rmi $(docker images -f "dangling=true" -q)
echo "清除none镜像"
}
#入口
run
3.推送成功后在harbor查看