溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用Shell腳本批量啟停Docker服務

發(fā)布時間:2020-09-02 20:56:33 來源:腳本之家 閱讀:365 作者:Surpassme 欄目:服務器

最近日常測試中經常需要手動啟動或停止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服務,希望對大家有所幫助!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI