• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

docker批量启动关闭所有容器的操作

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

docker中

启动所有的容器命令

docker start $(docker ps -a | awk '{ print $1}' | tail -n +2)

docker中

关闭所有的容器命令

docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)

补充:shell批量启动和删除docker容器,基于docker toolbox

停止容器

stop.sh
#!/bin/bash
containerIDs="8f78b7e6995b beeae59c6f41 5305c3db3670 bffc551a6b96 61f0fc8b1dc4 8e85a3b7bfe3 d15c4d2dc546 2a3c4eb14584 5e98077acc3a 13bac087b83d"
statusLived="live"
statusdead="Dead"
notExistContainer="None"
retryCount=3
function GetContainerStatus(){
 containerExist=$(docker ps -a | grep -i $1 | wc -l ) 
 if [ ${containerExist} -gt 0 ]
 then
 pid=$(docker stats --format "{{.PIDs}}" --no-stream $1 )
 if [ "${pid}" != "0" ]
  then 
  echo "${statusLived}"
 else
  echo "${statusdead}"
 fi
 else
 echo "${notExistContainer}" 
 fi
}
function StopContainer(){
 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

说明:containerIDs里面是你已经建好的,docker容器这里为nginx

把stop.sh放在d:/nginx下面。

过程稍微慢了点,耐心等待。

停止容器成功。

启动容器

#!/bin/bash
containerIDs="8f78b7e6995b
 beeae59c6f41
 5305c3db3670
 bffc551a6b96
 61f0fc8b1dc4
 8e85a3b7bfe3
 d15c4d2dc546
 2a3c4eb14584
 5e98077acc3a
 13bac087b83d"
statusLived="live"
statusdead="Dead"
notExistContainer="None"
retryCount=3
function GetContainerStatus(){
 containerExist=$(docker ps -a | grep -i $1 | wc -l ) 
 if [ ${containerExist} -gt 0 ]
 then
 pid=$(docker stats --format "{{.PIDs}}" --no-stream $1 )
 if [ "${pid}" != "0" ]
  then 
  echo "${statusLived}"
 else
  echo "${statusdead}"
 fi
 else
 echo "${notExistContainer}" 
 fi
}
function StartContainer(){
 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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持极客世界。如有错误或未考虑完全的地方,望不吝赐教。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Docker 彻底删除私有库镜像的操作发布时间:2022-02-12
下一篇:
Docker 无法停止或删除容器服务问题的解决方案发布时间:2022-02-12
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap