使用shell脚本批量启动或者停止docker容器

在工作中使用docker安装了mysql,redis,zookeeper,kafka,rabbitmq,rocketmq以及rocketmq的管理界面。但是每天早上上班启动这个个容器一个一个启,太麻烦,查阅了网上资料。用shell脚本来管理这些容器。

1.批量启动

#!/bin/bash
containerIDs="539f4c1943ad 8fe5f8b0a8cc 6c625b8fea54 34bf9ffded40 5a0d33e2a30a 66ccccce0d97 244064e14340 33d65841a7c9"
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

使用时将需要批量的容器id(ContainerIds)替换成自己需要批量启动的容器id即可。

2.批量停止

#!/bin/bash
containerIDs="539f4c1943ad 8fe5f8b0a8cc 6c625b8fea54 34bf9ffded40 5a0d33e2a30a 66ccccce0d97 244064e14340 33d65841a7c9"
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

参考文章:https://www.jb51.net/article/173912.htm
如有侵权,请联系删除。

你可能感兴趣的:(docker,shell)