最近日常测试中经常需要手动启动或停止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. MATLAB常见的学习率下降策略

    MATLAB常见的学习率下降策略 凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1. 几种常见的学习率下降策略(learning rate decay st ...

  2. 一,java框架学习

    一,java框架学习 Hibernate概述Hibernate是一个开放源代码的ORM(对象关系映射)框架,对jdbc进行了轻量级的封装,是的java开发人员可以使用面向对象编程思想操作数据库,简化操 ...

  3. python3.5.3rc1学习十:网络请求

    #sys模块import sys sys.stderr.write('This is stderr text\n')# 因为从定向有缓冲区,所以需要以下这行代码sys.stderr.flush()sy ...

  4. GitHub 上优秀的开源SLAM repo (更新中)

    GitHub 上优秀的开源SLAM repo (更新中) 欢迎 watch/star/fork 我们的 GitHub 仓库: Awesome-SLAM, 或者follow 项目的主页:Awesome- ...

  5. MySQL 字符集与比较规则

    MySQL 字符集与比较规则 由于 MySQL 客户端与服务端之间通信时需要将字符串编码传输,所以不可避免会产生编码转换 字符集 MySQL 中 utf8 就是 utf8mb3,只使用 1-3 个字节 ...

  6. SQL Server 修改数据库

    1. 可视化界面修改数据库 (1)右击数据库,然后选择属性. (2)在工具选项卡中,选择[文件]页,可以更改所有者,文件大小,自增量等参数. 2.  使用ALTER Database修改数据库 (1) ...

  7. 记录错误or日记(更新中)

    前言: 从2018.8-17开始记录 本篇随笔记录做题时的小错误(大多数),考试总结(懒得总结了),做过的每个题的错误 2019.12.7 傻逼学校,给我三个小时假期给你们做题挣工资 2019.11. ...

  8. 1130不允许连接到MySql server

    连接远程服务器mysql时,报错: 1130-host ... is not allowed to connect to this MySql server 这是因为默认只让localhost的主机连 ...

  9. Ubuntu 安装git及git命令

    1.检查git是否已经安装,输入git version命令即可,如果没有显示版本号表示没有安装git 2.安装git sudo apt-get install git 3.配置git全局环境git c ...

  10. mysql批量更新数据(性能优化)--第二种方式

    Spring+Mybatis 手动控制事务 参考: https://blog.csdn.net/qq_41750175/article/details/87621170 public boolean ...