欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

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

程序员文章站 2024-01-03 17:54:34
最近日常测试中经常需要手动启动或停止docker,于是决定写一个shell脚本来代替人工操作,另外该脚本,也可以通过python脚本实行远程调用,详细如下所示: 目前该脚本是将con...

最近日常测试中经常需要手动启动或停止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服务,希望对大家有所帮助!

上一篇:

下一篇: