使用Shell脚本批量启停Docker服务
发表于:2025-02-05 作者:千家信息网编辑
千家信息网最后更新 2025年02月05日,最近日常测试中经常需要手动启动或停止docker,于是决定写一个Shell脚本来代替人工操作,另外该脚本,也可以通过Python脚本实行远程调用,详细如下所示:目前该脚本是将Container ID写
千家信息网最后更新 2025年02月05日使用Shell脚本批量启停Docker服务
最近日常测试中经常需要手动启动或停止docker,于是决定写一个Shell脚本来代替人工操作,另外该脚本,也可以通过Python脚本实行远程调用,详细如下所示:
目前该脚本是将Container ID写死在脚本中,当然也可以通过传参给脚本来进行控制,大家可以改造一下。
启动docker
启动脚本详细如下所示:
#!/bin/bashcontainerIDs="ad3e4d7fc407 a228730a915f ad3e4d7fc4099"statusLived="live"statusdead="Dead"notExistContainer="None"retryCount=3function 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 donedone
停止docker
停止脚本详细如下所示:
#!/bin/bashcontainerIDs="589bda1309cd ad3e4d7fc407 a228730a915f ad3e4d7fc4099"statusLived="live"statusdead="Dead"notExistContainer="None"retryCount=3function 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 donedone
Python调用脚本
Python示例脚本如下所示:
import paramikodef 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服务,希望对大家有所帮助!
脚本
可以通过
服务
人工
手动
示例
帮助
控制
改造
测试
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件开发英语的要求
网络技术挑战赛作品易拉宝
网站配什么服务器
中科大网络安全6系
软件开发中心 都有哪些部门
花园战争服务器怎么连接
精灵宝可梦剑盾全球服务器
县委网信办 网络安全讲话
网络安全内容审查
中科大网络安全研究生招生目录
曲靖网络技术服务
网络技术等级证明
mysql 数据库改名
想学软件开发如何入手
大学生软件开发流程
白城冬阳网络技术
战地五搜不到服务器是什么问题
苏州 杨林 软件开发
专业软件开发人员外包中心
学生成绩管理系统数据库设计论文
怎么改图表上的数据库
魔兽世界最新版本的服务器
企业里的网关会用到数据库吗
网络安全有什么隐患
关于网络安全与执法专业发言
只狼怎么看服务器位置
服务器ip自动变成自动获取
广协网络技术有限公司怎么样
sinomed有多少中文数据库
计算机网络安全工作心得