最近日常测试中经常需要手动启动或停止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 ")

本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注:

Shell脚本批量启停Docker的更多相关文章

  1. shell脚本批量ssh登陆主机并执行命令

    shell脚本批量ssh登陆主机并执行命令 今天在客户现场遇到了这个问题,客户没有管理工具,无法批量登陆主机下发命令,几个个C段啊,让我一个一个登陆,.................. 所以写了个s ...

  2. Linux shell脚本 批量创建多个用户

    Linux shell脚本 批量创建多个用户 #!/bin/bash groupadd charlesgroup for username in charles1 charles2 charles3 ...

  3. Shell 脚本批量创建数据库表

    使用 Shell 脚本批量创建数据表 系统:Centos6.5 64位 MySQL版本:5.1.73 比如下面这个脚本: #!/bin/bash #批量新建数据表 for y in {0..199}; ...

  4. shell脚本批量执行命令----必需判断上一步执行结果--没有捷径

    # 注意:shell脚本批量执行命令,不能只写一个函数,然后把所有命令复制进去,之前试过这样是不行的.必须要有一个判断命令执行成功与否的语句 # 简单的命令可以不加结果判断符号,但是遇到解压包.sed ...

  5. shell脚本批量开启防火墙端口

    # 注意:shell脚本批量执行命令,不能只写一个函数,然后把所有命令复制进去,之前试过这样是不行的.必须要有一个判断命令执行成功与否的语句 # 简单的命令可以不加结果判断符号,但是遇到解压包.sed ...

  6. linux系统配置文件或shell脚本批量注释

    1. 配置文件批量注释 1.1 批量注释 ① 进入命令行模式,按ctrl + v进入 visual block模式,键盘上下箭头选中多行,把需要注释的行标记起来 ② 按大写字母I,再输入注释符:# ③ ...

  7. 用shell脚本批量修改文件后缀名

    早上本想将一些照片上传到相册中,但是由于所有照片的扩展名都是JPG而不是小写的jpg,因此造成了“格式不正确”而不能上传照片.此刻就产生了这样一个问题:使用shell脚本如何批量将所有文件的扩展名JP ...

  8. shell脚本批量生成配置文件

    如果管理的站点和服务器较多的情况下,每次修改配置文件都相当痛苦.因而想到了用shell脚本来批量生成配置文件和配置数据.下面这个脚本是为了批量生成nagios监控配置文件的一个shell脚本程序.其原 ...

  9. shell脚本批量部署ssh

    日常运维工作中,需要给几十上百台服务器批量部署软件或者是重启服务器等操作, 这样大量重复性的工作一定很苦恼,本文给大家提供了最基本的批量操作的方法,虽然效率不高,对于初学者来说还是好理解.由于刚开始学 ...

随机推荐

  1. 6.2 DataFrame

    一.DataFrame概述 在Spark SQL中,DataFrame就是它的数据抽象,对DataFrame进行转换操作. DataFrame的推出,让Spark具备了处理大规模结构化数据的能力,不仅 ...

  2. index-css-添加类-移除类-toggleClass-attr

    1=>index()会返回当前元素在所有的兄弟元素里面的索引值用法:$("a").click(function(){ console.log($(this).index()) ...

  3. 01day-webpack

    <!-- .sass后缀的文件名 比较老了 现在它的后缀名是.scss 其实他们是同一个东西 只是 后缀名发生了变化 以 .sass写的文件的内容是 他没有括号 没有分号 有点怪 它跟新为了.s ...

  4. template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869)

    org.thymeleaf.exceptions.TemplateInputException: Error resolving template [code/leading], template m ...

  5. Oracle SQL日期及日期格式获取命令

    日期及日期格式: 获取系统日期: sysdate() 格式化日期 to_char(sysdate(),'yyyy-mm-dd,hh24:mi:ss') to_date(sysdate(),'yyyy- ...

  6. Python字符串内置方法使用及年龄游戏深入探究

    目录 作业 ==程序代码自上往下运行,建议自上而下的完成下列任务== 作业 使用代码实现以下业务逻辑: 写代码,有如下变量name = " aleX",请按照要求实现每个功能: 移 ...

  7. Centos 7+KVM(Windows Server 2008 r2 )

    KVM虚拟机 Kernel-based Virtual Machine的简称,是一个开源的系统虚拟化模块,自Linux 2.6.20之后集成在Linux的各个主要发行版本中.它使用Linux自身的调度 ...

  8. 详解C++ STL set 容器

    详解C++ STL set 容器 本篇随笔简单介绍一下\(C++STL\)中\(set\)容器的使用方法及常见使用技巧. set容器的概念和性质 \(set\)在英文中的意义是:集合.\(set\)容 ...

  9. wafer2-nodejs 本地部署服务器

    友情提示:假设你已经部署好了腾讯云微信小程序服务,如果没有,就不用往下看了,果断选云开发. ------------------------------------------------------ ...

  10. split task

    和印度的team合作的时候,他们经常有些要求,然后我们这边有时候需要改代码来满足他们的需求. 最近一次,他们要求在我们的一个工具中为他们加入2个asp.net的注册命令,不知道什么原因,system ...