Shell脚本批量启停Docker
程序员文章站
2022-05-09 15:57:52
[TOC] 最近日常测试中经常需要手动启动或停止docker,于是决定写一个Shell脚本来代替人工操作,另外该脚本,也可以通过Python脚本实行远程调用,详细如下所示: 目前该脚本是将Container ID写死在脚本中,当然也可以通过传参给脚本来进行控制,大家可以改 ......
目录
最近日常测试中经常需要手动启动或停止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 ")
上一篇: 程序猿的基本素质
下一篇: MySQL 时间日期类型和相关函数