使用Shell脚本批量启停Docker服务

 更新时间:2019年11月10日 08:13:46   作者:Surpassme  
最近日常测试中经常需要手动启动或停止docker,于是决定写一个Shell脚本来代替人工操作,下面小编把我实现过程分享到脚本之家平台,需要的朋友参考下

最近日常测试中经常需要手动启动或停止docker,于是决定写一个Shell脚本来代替人工操作,另外该脚本,也可以通过Python脚本实行远程调用,详细如下所示:

目前该脚本是将Container ID写死在脚本中,当然也可以通过传参给脚本来进行控制,大家可以改造一下。

启动docker

启动脚本详细如下所示:

#!/bin/bash
containerIDs="ad3e4d7fc407 a228730a915f ad3e4d7fc4099"
statusLived="live"
statusdead="Dead"
notExistContainer="None"
retryCount=3
function GetContainerStatus(){
 containerExist=$(sudo docker ps -a | grep -i $1 | wc -l ) 
 if [ ${containerExist} -gt 0 ]
  then
  pid=$(sudo docker stats --format "{{.PIDs}}" --no-stream $1 )
  if [ "${pid}" != "0" ]
   then 
   echo "${statusLived}"
  else
   echo "${statusdead}"
  fi
 else
  echo "${notExistContainer}" 
 fi
}
function StartContainer(){
 sudo docker restart $1
}
for containerID in ${containerIDs}
 do
 for((i=1;i<=${retryCount};i++))
 do
 status=$(GetContainerStatus ${containerID} )
 echo "Container ${containerID} status is ${status}"
 if [ "${status}" == ${statusLived} ]
  then
  echo "Container ${containerID} already running"
  break
 fi
 if [ "${status}" == ${notExistContainer} ]
  then
  echo "Container ${containerID} not existed"
  break
 fi
 if [ "${status}" == ${statusdead} ]
  then
  echo "Container ${containerID} stopped ,start container"
  StartContainer ${containerID}
  verifyStatus=$(GetContainerStatus ${containerID} )
  if [ "${verifyStatus}" == ${statusLived} ]
   then
    echo "start container ${containerID} success "
    break
  else
   echo "${i} retry start container"
   StartContainer ${containerID}
  fi
 fi
 done
done

停止docker

停止脚本详细如下所示:

#!/bin/bash
containerIDs="589bda1309cd ad3e4d7fc407 a228730a915f ad3e4d7fc4099"
statusLived="live"
statusdead="Dead"
notExistContainer="None"
retryCount=3
function GetContainerStatus(){
 containerExist=$(sudo docker ps -a | grep -i $1 | wc -l ) 
 if [ ${containerExist} -gt 0 ]
  then
  pid=$(sudo docker stats --format "{{.PIDs}}" --no-stream $1 )
  if [ "${pid}" != "0" ]
   then 
   echo "${statusLived}"
  else
   echo "${statusdead}"
  fi
 else
  echo "${notExistContainer}" 
 fi
}
function StopContainer(){
 sudo docker stop $1
}
for containerID in ${containerIDs}
 do
 for ((i=1;i<=${retryCount};i++))
 do
  status=$(GetContainerStatus ${containerID} )
  echo "Container ${containerID} status is ${status}"
  if [ "${status}" == ${statusdead} ]
  then
  echo "Container ${containerID} already stopped"
  break
  fi
  if [ "${status}" == ${notExistContainer} ]
  then
  echo "Container ${containerID} not existed"
  break
  fi
  if [ "${status}" == ${statusLived} ]
  then
   echo "Container ${containerID} is lived ,stop container"
   StopContainer ${containerID}
   verifyStatus=$(GetContainerStatus ${containerID} )
   if [ "${verifyStatus}" == ${statusdead} ]
   then
    echo "stop container ${containerID} success "
    break
   else
   echo "${i} retry stop container"
   StopContainer ${containerID}
   fi
  fi
 done
done

Python调用脚本

Python示例脚本如下所示:

import paramiko
def StartContainer(svr,port,user,pwd):
 client = paramiko.SSHClient()
 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 client.connect(svr,port=port, username=user, password=pwd,timeout=5)
 client.exec_command("cd /home/TestCode/ && bash startContainer.sh")
def StopContainer(svr,port,user,pwd):
 client = paramiko.SSHClient()
 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 client.connect(svr, port=port, username=user, password=pwd, timeout=5)
 client.exec_command("cd /home/TestCode/ && bash stopContainer.sh ")

总结

以上所述是小编给大家介绍的使用Shell脚本批量启停Docker服务,希望对大家有所帮助!

相关文章

  • docker如何查询镜像版本信息

    docker如何查询镜像版本信息

    这篇文章主要介绍了docker如何查询镜像版本信息问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • 详解SpringBoot项目docker环境运行时无限重启问题

    详解SpringBoot项目docker环境运行时无限重启问题

    这篇文章主要介绍了详解SpringBoot项目docker环境运行时无限重启问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • docker安装openwrt immortalwrt全过程

    docker安装openwrt immortalwrt全过程

    本文主要介绍了如何通过OpenWrt和Docker结合使用,以提供路由器的功能并接管无法安装软件的Switch的流量,首先,通过阿里镜像加速下载并安装Docker,然后配置网络,包括开启网卡混杂模式和创建虚拟网络MACVLAN,接着,在OpenWrt中配置网络,最后拉取OpenWrt镜像并启动
    2024-10-10
  • 使用Docker打包和运行Java镜像的完整指南

    使用Docker打包和运行Java镜像的完整指南

    在这篇文章中,我们将详细讲解如何使用Docker打包和运行一个Java应用镜像,通过此教程,即使你是一个小白,也能轻松学会如何构建Docker镜像并运行Java应用,感兴趣的小伙伴跟着小编一起来看看吧
    2025-01-01
  • 使用Docker Compose部快速署ELK(亲测有效)

    使用Docker Compose部快速署ELK(亲测有效)

    这篇文章主要介绍了Docker Compose部署ELK的详细过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-08-08
  • Docker中Mysql容器无法停止无法删除问题

    Docker中Mysql容器无法停止无法删除问题

    这篇文章主要介绍了Docker中Mysql容器无法停止无法删除问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • docker如何在外部指定参数变量

    docker如何在外部指定参数变量

    这篇文章主要介绍了docker如何在外部指定参数变量,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • 如何监控docker容器运行状态 shell 脚本

    如何监控docker容器运行状态 shell 脚本

    这篇文章主要介绍了如何监控docker容器运行状态 shell 脚本的操作方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • 创建支持SSH服务的Docker镜像的方法

    创建支持SSH服务的Docker镜像的方法

    这篇文章主要介绍了创建支持SSH服务的Docker镜像的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • 修改已有docker容器中的内容方法

    修改已有docker容器中的内容方法

    这篇文章主要介绍了修改已有docker容器中的内容方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11

最新评论