使用Shell脚本批量启停Docker服务-创新互联

最近日常测试中经常需要手动启动或停止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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

当前标题:使用Shell脚本批量启停Docker服务-创新互联
URL分享:https://www.cdcxhl.com/article24/csgpce.html

成都网站建设公司_创新互联,为您提供商城网站App开发网站设计外贸建站电子商务虚拟主机

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站制作