经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Linux/Shell » 查看文章
Shell脚本启停Docker
来源:cnblogs  作者:Surpassme  时间:2019/11/8 20:06:37  对本文有异议

? ? 最近日常测试中经常需要手动启动或停止docker,于是决定写一个Shell脚本来代替人工操作,另外该脚本,也可以通过Python脚本实行远程调用,详细如下所示:

目前该脚本是将Container ID写死在脚本中,当然也可以通过传参给脚本来进行控制,大家可以改造一下。

启动docker

启动脚本详细如下所示:

  1. #!/bin/bash
  2. containerID="589bda1309cd"
  3. statusLived="live"
  4. statusdead="Dead"
  5. notExistContainer="None"
  6. retryCount=3
  7. function GetContainerStatus(){
  8. containerExist=$(sudo docker ps -a | grep -i $1 | wc -l )
  9. if [ ${containerExist} -gt 0 ]
  10. then
  11. pid=$(sudo docker stats --format "{{.PIDs}}" --no-stream $1 )
  12. if [ "${pid}" != "0" ]
  13. then
  14. echo "${statusLived}"
  15. else
  16. echo "${statusdead}"
  17. fi
  18. else
  19. echo "${notExistContainer}"
  20. fi
  21. }
  22. function StartContainer(){
  23. sudo docker restart $1
  24. }
  25. for ((i=1;i<=${retryCount};i++))
  26. do
  27. status=$(GetContainerStatus ${containerID} )
  28. echo "Container ${containerID} status is ${status}"
  29. if [ "${status}" == ${statusLived} ]
  30. then
  31. echo "Container ${containerID} already running"
  32. break
  33. fi
  34. if [ "${status}" == ${notExistContainer} ]
  35. then
  36. echo "Container ${containerID} not existed"
  37. break
  38. fi
  39. if [ "${status}" == ${statusdead} ]
  40. then
  41. echo "Container ${containerID} stopped ,start container"
  42. StartContainer ${containerID}
  43. verifyStatus=$(GetContainerStatus ${containerID} )
  44. if [ "${verifyStatus}" == ${statusLived} ]
  45. then
  46. echo "start container ${containerID} success "
  47. break
  48. else
  49. echo "${i} retry start container"
  50. StartContainer ${containerID}
  51. fi
  52. fi
  53. done

停止docker

停止脚本详细如下所示:

  1. #!/bin/bash
  2. containerID="589bda1309cd"
  3. statusLived="live"
  4. statusdead="Dead"
  5. notExistContainer="None"
  6. retryCount=3
  7. function GetContainerStatus(){
  8. containerExist=$(sudo docker ps -a | grep -i $1 | wc -l )
  9. if [ ${containerExist} -gt 0 ]
  10. then
  11. pid=$(sudo docker stats --format "{{.PIDs}}" --no-stream $1 )
  12. if [ "${pid}" != "0" ]
  13. then
  14. echo "${statusLived}"
  15. else
  16. echo "${statusdead}"
  17. fi
  18. else
  19. echo "${notExistContainer}"
  20. fi
  21. }
  22. function StopContainer(){
  23. sudo docker stop $1
  24. }
  25. for ((i=1;i<=${retryCount};i++))
  26. do
  27. status=$(GetContainerStatus ${containerID} )
  28. echo "Container ${containerID} status is ${status}"
  29. if [ "${status}" == ${statusdead} ]
  30. then
  31. echo "Container ${containerID} already stopped"
  32. break
  33. fi
  34. if [ "${status}" == ${notExistContainer} ]
  35. then
  36. echo "Container ${containerID} not existed"
  37. break
  38. fi
  39. if [ "${status}" == ${statusLived} ]
  40. then
  41. echo "Container ${containerID} is lived ,stop container"
  42. StopContainer ${containerID}
  43. verifyStatus=$(GetContainerStatus ${containerID} )
  44. if [ "${verifyStatus}" == ${statusdead} ]
  45. then
  46. echo "stop container ${containerID} success "
  47. break
  48. else
  49. echo "${i} retry stop container"
  50. StopContainer ${containerID}
  51. fi
  52. fi
  53. done

Python调用脚本

Python示例脚本如下所示:

  1. import paramiko
  2. def StartContainer(svr,port,user,pwd):
  3. client = paramiko.SSHClient()
  4. client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  5. client.connect(svr,port=port, username=user, password=pwd,timeout=5)
  6. client.exec_command("cd /home/TestCode/ && bash startContainer.sh")
  7. def StopContainer(svr,port,user,pwd):
  8. client = paramiko.SSHClient()
  9. client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  10. client.connect(svr, port=port, username=user, password=pwd, timeout=5)
  11. client.exec_command("cd /home/TestCode/ && bash stopContainer.sh ")

原文链接:http://www.cnblogs.com/surpassme/p/11822978.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号