小知识:docker批量启动关闭所有容器的操作

docker

启动所有的容器命令

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

docker中

关闭所有的容器命令

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

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

停止容器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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

%小知识:docker批量启动关闭所有容器的操作-猿站网-插图

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

%小知识:docker批量启动关闭所有容器的操作-1猿站网-插图

%小知识:docker批量启动关闭所有容器的操作-2猿站网-插图

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

%小知识:docker批量启动关闭所有容器的操作-3猿站网-插图

停止容器成功。

启动容器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/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

%小知识:docker批量启动关闭所有容器的操作-4猿站网-插图

%小知识:docker批量启动关闭所有容器的操作-5猿站网-插图

%小知识:docker批量启动关闭所有容器的操作-6猿站网-插图

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

原文链接:https://blog.csdn.net/qq_38474916/article/details/100534087

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:Docker 无法停止或删除容器服务问题的解决方案

2023-3-23 10:32:55

建站知识

小知识:Docker 彻底删除私有库镜像的操作

2023-3-23 10:47:34

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索