Shell脚本启停Docker

Posted Surpassme

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Shell脚本启停Docker相关的知识,希望对你有一定的参考价值。

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

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

启动docker

启动脚本详细如下所示:

#!/bin/bash

containerID="589bda1309cd"
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 ((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

停止docker

停止脚本详细如下所示:

#!/bin/bash

containerID="589bda1309cd"
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 ((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

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 ")

以上是关于Shell脚本启停Docker的主要内容,如果未能解决你的问题,请参考以下文章

[shell]MySQL启停脚本

Shell ❀ Zookeeper和Kafka集群环境启停脚本

Linux自定义shell脚本一键启停微服务jar包

Linux自定义shell脚本一键启停微服务jar包

代码片段:Shell脚本实现重复执行和多进程

Linux下SpringBoot项目启停脚本