docker批量啟動關(guān)閉所有容器的操作
docker中
啟動所有的容器命令
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批量啟動和刪除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),耐心等待。

停止容器成功。
啟動容器
#!/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



以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Docker容器中掛載NFS共享目錄的實(shí)現(xiàn)
這篇文章主要介紹了Docker容器中掛載NFS共享目錄的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
使用Docker快速運(yùn)行Firefox并實(shí)現(xiàn)遠(yuǎn)程訪問本地火狐瀏覽器的詳細(xì)過程
文章介紹了如何在Docker中運(yùn)行Firefox瀏覽器,并通過Cpolar內(nèi)網(wǎng)穿透工具實(shí)現(xiàn)遠(yuǎn)程訪問,步驟包括部署Firefox、本地訪問、安裝Cpolar、配置公網(wǎng)地址、設(shè)置固定二級子域名等,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-02-02
Docker 配置網(wǎng)絡(luò)使用bridge網(wǎng)絡(luò)的方法
本篇文章主要介紹了Docker 配置網(wǎng)絡(luò)使用 bridge 網(wǎng)絡(luò)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
docker容器狀態(tài)的轉(zhuǎn)換實(shí)現(xiàn)
這篇文章主要介紹了docker容器狀態(tài)的轉(zhuǎn)換實(shí)現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
詳解基于docker-swarm搭建持續(xù)集成集群服務(wù)
這篇文章主要介紹了詳解基于docker-swarm搭建持續(xù)集成集群服務(wù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10
docker搭建lnmp環(huán)境的實(shí)現(xiàn)步驟
DNMP(Docker + Nginx + MySQL + PHP7/5 + Redis)是一款全功能的LNMP一鍵安裝程序,本文就來介紹一下docker搭建lnmp環(huán)境的實(shí)現(xiàn)步驟,具有一定的參考價值,感興趣的可以了解一下2024-07-07
Docker?制作tomcat鏡像并部署項(xiàng)目的步驟
這篇文章主要介紹了Docker?制作tomcat鏡像并部署項(xiàng)目?,講解如何制作自己的tomcat鏡像,并使用tomcat部署項(xiàng)目,需要的朋友可以參考下2022-10-10
使用Dockerfile構(gòu)建java web環(huán)境
用dockerfile構(gòu)建一個Java的web環(huán)境,主要分為2步,第一步是在鏡像中安裝jdk并配置環(huán)境變量,第二步是安裝tomcat,下面分步驟給大家詳細(xì)介紹,一起看看吧2016-12-12

