服務(wù)器
最近日常測試中經(jīng)常需要手動(dòng)啟動(dòng)或停止docker,于是決定寫(xiě)一個(gè)Shell腳本來(lái)代替人工操作,另外該腳本,也可以通過(guò)Python腳本實(shí)行遠程調用,詳細如下所示:
目前該腳本是將Container ID寫(xiě)死在腳本中,當然也可以通過(guò)傳參給腳本來(lái)進(jìn)行控制,大家可以改造一下。
啟動(dòng)docker
啟動(dòng)腳本詳細如下所示:
#!/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服務(wù),希望對大家有所幫助!
更多關(guān)于云服務(wù)器,域名注冊,虛擬主機的問(wèn)題,請訪(fǎng)問(wèn)特網(wǎng)科技官網(wǎng):wap.friendlycc.com.cn
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站