deploy.sh(部署文件)
#!/bin/bash
# 定义要查找的镜像名称
image_name="bat_detection:xxx"
# 使用 docker ps 命令查找正在运行的容器 ID,基于指定的镜像名称
container_0_id=$(docker ps -qf "ancestor=$image_name")
# 检查容器 ID 是否为空
if [ -z "$container_0_id" ]; then
echo "没有基于镜像 $image_name 运行的容器。"
else
# 停止指定容器
docker stop "$container_0_id"
docker rm "$container_0_id"
echo "成功停止基于镜像 $image_name 运行的容器(ID: $container_0_id)."
fi
# 定义要查找的镜像基本名称,不包含版本号
image_base_name="platformized-bat_detection"
# 使用 docker ps 命令获取所有容器的信息,然后过滤出包含指定基本镜像名称的容器
containers_info=$(docker ps -a --format "{{.ID}} {{.Image}}" | grep "$image_base_name")
# 逐行遍历容器信息
while IFS= read -r container_info; do
# 提取容器 ID 和完整镜像名称
container_id=$(echo "$container_info" | awk '{print $1}')
full_image_name=$(echo "$container_info" | awk '{print $2}')
container_name=$(docker inspect --format='{{.Name}}' "$container_id" | cut -c2-)
# 输出容器名称、完整镜像名称和容器 ID
echo "已部署容器名称: $container_name, 已部署容器完整镜像名称: $full_image_name, 已部署容器ID: $container_id"
docker stop $container_id
docker rm $container_id
done <<< "$containers_info"
echo "请输入镜像拉取密码(阿里云)"
docker login --username=acr@xxxxxxxxxxx registry.cn-hangzhou.aliyuncs.com
echo "镜像拉取中......"
docker pull xxxxxxxxplatformized-bat_detection:xxx
#docker pull $image_name
# 定义要检查的镜像名称
image_name_2="xxxxxxxxplatformized-bat_detection"
# 定义要检查的镜像版本
image_version="xxx"
# 使用 docker images 命令列出所有镜像,然后使用 grep 命令过滤指定的镜像名称
if docker images --format "{{.Repository}}:{{.Tag}}" | grep -q "^$image_name_2:$image_version$"; then
echo "xxx版本算法模块镜像下载完成."
docker run -d --restart=always --network=host -it xxxxxxxxplatformized-bat_detection:xxx nohup python3 manage.py runserver &
echo "xxx版本服务启动"
else
echo "xxx版本模块镜像下载失败."
fi
check.sh(验证文件)
#!/bin/bash
# 定义要检查的端口号
port="xxxx"
# 检查端口是否被监听
if ss -tuln | grep -E "LISTEN.*:$port\s" &>/dev/null; then
# echo "端口 $port 已经启用."
# 定义要查找的镜像名称
image_name="xxxxxxxxplatformized-battery_detection"
image_version="xxxx"
# 检查指定镜像的容器是否在运行中
if docker ps -f "ancestor=$image_name:$image_version" --format '{{.Names}}' | grep -q .; then
echo "xxxx版本算法检测模块服务启动成功."
else
echo "xxxx版本算法检测模块服务启动失败."
fi
else
echo "xxxx版本算法检测模块服务启动失败."
fi