Shell脚本启停Docker
2019-11-08 16:05:36来源:博客园 阅读 ()
Shell脚本启停Docker
目录
- 启动docker
- 停止docker
- Python调用脚本
? ? 最近日常测试中经常需要手动启动或停止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 ")
原文链接:https://www.cnblogs.com/surpassme/p/11822978.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Linux系统如何设置开机自动运行脚本? 2020-06-11
- Linux指令和shell脚本 2020-06-11
- Xshell如何配置并远程连接Linux服务器详解 2020-05-31
- Ubuntu下用脚本执行sql语句(sqlite) 2020-05-29
- 如何将目录下的脚本一次性全部执行 2020-05-28
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash