Recently, I often need to manually start or stop docker in daily testing, so I decided to write a Shell script to replace manual operation. In addition, the script can also be called remotely through a Python script, as shown below: Currently, the script hard-codes the Container ID in the script. Of course, you can also control it by passing parameters to the script. You can modify it. Start DockerThe startup script is as follows: #!/bin/bash containerIDs="ad3e4d7fc407 a228730a915f ad3e4d7fc4099" 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} did not exist" 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 Stop DockerThe stop script is as follows: #!/bin/bash containerIDs="589bda1309cd ad3e4d7fc407 a228730a915f ad3e4d7fc4099" 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} did not exist" 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 Python calling scriptThe Python sample script is as follows: import paramiko def StartContainer(svr,port,user,pwd): client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(svr,port=port, username=user, password=pwd,timeout=5) client.exec_command("cd /home/TestCode/ && bash startContainer.sh") def StopContainer(svr,port,user,pwd): client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(svr, port=port, username=user, password=pwd, timeout=5) client.exec_command("cd /home/TestCode/ && bash stopContainer.sh ") Summarize The above is what I introduced to you about using Shell scripts to batch start and stop Docker services. I hope it will be helpful to you! You may also be interested in:
|
<<: MySQL index cardinality concept and usage examples
>>: Vue implements student management function
Table of contents Deploy httpd with docker contai...
Recently I saw a barrage effect on B station call...
I've been using redis recently and I find it ...
Nginx supports three ways to configure virtual ho...
In the previous article https://www.jb51.net/arti...
With the right settings, you can force Linux user...
Table of contents Deploy tomcat 1. Download and d...
1. Introduction The main value that SELinux bring...
When we write pages, we sometimes find that the C...
Table of contents Updatable Views Performance of ...
This article shares the installation tutorial of ...
Table of contents 1. Introduction 2. Ideas Two wa...
Relationship between MySQL and MariaDB MariaDB da...
What to do if you forget your Linux/Mac MySQL pas...
As the Internet era becomes more mature, the deve...