#!/bin/sh

source /etc/profile

## 应用名称
APP_NAME=cctp-atu

## 镜像版本
VERSION=2.1

## 公司名称
corporation=northking

## 镜像仓库配置
DOCKER_REGISTRY=192.168.0.165:8889
USERNAME=admin
PASSWORD=admin123




## 获取运行 容器ID
RUN_ID=$(docker ps |grep ${APP_NAME}|awk '{print $1}')

if [ "${RUN_ID}" = "" ]; then
  echo "Container ${APP_NAME} is not starting "
else
  ## 停止 docker容器
  docker stop ${RUN_ID}
  echo "Container ${APP_NAME} , ID: ${RUN_ID} is stopped   "
fi

## 获取  docker 容器ID
STOP_ID=$(docker ps -a |grep ${APP_NAME}|awk '{print $1}')

if [ "${STOP_ID}" = "" ]; then
  echo "Container ${APP_NAME} is not exist"
else
  ## 移除 docker容器
  docker rm ${STOP_ID}
  echo "Remove container ${APP_NAME} , ID: ${STOP_ID}  "
fi

## 获取  镜像ID
IMG_ID=$(docker images |grep ${APP_NAME}|awk '{print $3}')

if [ "${IMG_ID}" = "" ]; then
  echo "Image ${APP_NAME} is not exist "
else
  ## 移除 docker镜像
  docker rmi ${IMG_ID}
  echo "Remove image ${APP_NAME} , ID: ${IMG_ID}  "
fi

## 创建镜像
docker build -t ${corporation}/${APP_NAME}:${VERSION} .

## 登录镜像仓库
docker login -u ${USERNAME} -p ${PASSWORD} ${DOCKER_REGISTRY}

## 重新tag
docker tag ${corporation}/${APP_NAME}:${VERSION} ${DOCKER_REGISTRY}/${corporation}/${APP_NAME}:${VERSION}

## 上传镜像
docker push ${DOCKER_REGISTRY}/${corporation}/${APP_NAME}:${VERSION}

## 删除镜像
docker rmi ${DOCKER_REGISTRY}/${corporation}/${APP_NAME}:${VERSION}
docker rmi ${corporation}/${APP_NAME}:${VERSION}