主頁 > 知識(shí)庫 > docker批量啟動(dòng)關(guān)閉所有容器的操作

docker批量啟動(dòng)關(guān)閉所有容器的操作

熱門標(biāo)簽:麗江真人語音電話外呼系統(tǒng) 河南防封號電銷機(jī)器人是什么 打400電話怎么辦理收費(fèi) 10086外包用的什么外呼系統(tǒng) 上海申請高400開頭的電話 400電話辦理介紹信 福州企業(yè)電銷機(jī)器人排名 怎么找到?jīng)]有地圖標(biāo)注的店 宿城區(qū)電話機(jī)器人找哪家

docker中

啟動(dòng)所有的容器命令

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

docker中

關(guān)閉所有的容器命令

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

補(bǔ)充:shell批量啟動(dòng)和刪除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里面是你已經(jīng)建好的,docker容器這里為nginx

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

過程稍微慢了點(diǎn),耐心等待。

停止容器成功。

啟動(dòng)容器

#!/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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽:朝陽 遵義 荊門 雞西 隴南 連云港 面試通知 運(yùn)城

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《docker批量啟動(dòng)關(guān)閉所有容器的操作》,本文關(guān)鍵詞  docker,批量,啟動(dòng),關(guān)閉,所有,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《docker批量啟動(dòng)關(guān)閉所有容器的操作》相關(guān)的同類信息!
  • 本頁收集關(guān)于docker批量啟動(dòng)關(guān)閉所有容器的操作的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章