服务器
最近日常测试中经常需要手动启动或停止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 donePython调用脚本
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服务,希望对大家有所帮助!
文章题目:使用Shell脚本批量启停Docker服务
网页网址:https://www.cdcxhl.com/article40/cjiheo.html
成都网站建设公司_创新互联,为您提供服务器托管、网站建设、面包屑导航、标签优化、网站策划、虚拟主机
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联