使用Shell腳本批量啟停Docker服務(wù)
最近日常測(cè)試中經(jīng)常需要手動(dòng)啟動(dòng)或停止docker,于是決定寫(xiě)一個(gè)Shell腳本來(lái)代替人工操作,另外該腳本,也可以通過(guò)Python腳本實(shí)行遠(yuǎn)程調(diào)用,詳細(xì)如下所示:
目前該腳本是將Container ID寫(xiě)死在腳本中,當(dāng)然也可以通過(guò)傳參給腳本來(lái)進(jìn)行控制,大家可以改造一下。
啟動(dòng)docker
啟動(dòng)腳本詳細(xì)如下所示:
#!/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
停止腳本詳細(xì)如下所示:
#!/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調(diào)用腳本
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 ")
總結(jié)
以上所述是小編給大家介紹的使用Shell腳本批量啟停Docker服務(wù),希望對(duì)大家有所幫助!
相關(guān)文章
詳解SpringBoot項(xiàng)目docker環(huán)境運(yùn)行時(shí)無(wú)限重啟問(wèn)題
這篇文章主要介紹了詳解SpringBoot項(xiàng)目docker環(huán)境運(yùn)行時(shí)無(wú)限重啟問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
docker安裝openwrt immortalwrt全過(guò)程
本文主要介紹了如何通過(guò)OpenWrt和Docker結(jié)合使用,以提供路由器的功能并接管無(wú)法安裝軟件的Switch的流量,首先,通過(guò)阿里鏡像加速下載并安裝Docker,然后配置網(wǎng)絡(luò),包括開(kāi)啟網(wǎng)卡混雜模式和創(chuàng)建虛擬網(wǎng)絡(luò)MACVLAN,接著,在OpenWrt中配置網(wǎng)絡(luò),最后拉取OpenWrt鏡像并啟動(dòng)2024-10-10
使用Docker Compose部快速署ELK(親測(cè)有效)
這篇文章主要介紹了Docker Compose部署ELK的詳細(xì)過(guò)程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Docker中Mysql容器無(wú)法停止無(wú)法刪除問(wèn)題
這篇文章主要介紹了Docker中Mysql容器無(wú)法停止無(wú)法刪除問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
如何監(jiān)控docker容器運(yùn)行狀態(tài) shell 腳本
這篇文章主要介紹了如何監(jiān)控docker容器運(yùn)行狀態(tài) shell 腳本的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
創(chuàng)建支持SSH服務(wù)的Docker鏡像的方法
這篇文章主要介紹了創(chuàng)建支持SSH服務(wù)的Docker鏡像的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08

