时间:2021-05-02
目录
最近日常测试中经常需要手动启动或停止docker,于是决定写一个Shell脚本来代替人工操作,另外该脚本,也可以通过Python脚本实行远程调用,详细如下所示:
目前该脚本是将Container ID写死在脚本中,当然也可以通过传参给脚本来进行控制,大家可以改造一下。
启动脚本详细如下所示:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 #!/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
停止脚本详细如下所示:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 #!/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示例脚本如下所示:
? 1 2 3 4 5 6 7 8 9 10 11 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服务,希望对大家有所帮助!
原文链接:https://www.cnblogs.com/surpassme/archive/2019/11/08/11822978.html
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
下面看下重启docker服务应用自启停命令,具体内容如下所述:?1234567#重启docker服务应用,不自动开启docker容器dockerupdate--
这篇文章主要介绍了编写shell脚本,使用iconv批量改变文件编码的脚本代码,需要的朋友可以参考下。用法示例:cd~/workspace/XXXProject
场景公司项目部署在docker中,由于未知原因容器偶尔会停止,需要写一个脚本监控容器运行状态,如果容器停止了,就再启动该容器shell脚本#!/bin/bash
Shell脚本Shell脚本(shellscript),是一种为shell编写的脚本程序。业界所说的shell通常都是指shell脚本,但读者朋友要知道,she
一个shell脚本,它本身的命令参数是什么样呢?首先,shell脚本编程和python等脚本编程一样可以使用参数;其次,shell参数分为位置参数和内部参数;其