Use Shell scripts to batch start and stop Docker services

Use Shell scripts to batch start and stop Docker services

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 Docker

The 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 Docker

The 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 script

The 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:
  • How to comment and uncomment in batches in shell
  • Example of using shell script to count file size and create users in batches
  • Detailed examples of shell script batch copying and command execution
  • Shell script implements ssh-copy-id to automatically send public keys to remote hosts in batches
  • How to delete es indexes in batches using shell script
  • Simple implementation method of shell batch curl interface script
  • Detailed explanation of the Shell command line batch processing of image file names
  • How to use shell to perform batch operations on multiple servers

<<:  MySQL index cardinality concept and usage examples

>>:  Vue implements student management function

Recommend

Example analysis of mysql shared lock and exclusive lock usage

This article uses examples to illustrate the usag...

MySQL REVOKE to delete user permissions

In MySQL, you can use the REVOKE statement to rem...

CSS Paint API: A CSS-like Drawing Board

1. Use Canvas images as CSS background images The...

Install Linux rhel7.3 operating system on virtual machine (specific steps)

Install virtualization software Before installing...

Detailed explanation of react setState

Table of contents Is setState synchronous or asyn...

Sending emails in html is easy with Mailto

Recently, I added a click-to-send email function t...

Detailed explanation of dynamic Christmas tree through JavaScript

Table of contents 1. Animated Christmas Tree Made...

Implementation of running SQL Server using Docker

Now .net core is cross-platform, and everyone is ...

The neglected special effects of META tags (page transition effects)

Using js in web design can achieve many page effec...

Docker container monitoring and log management implementation process analysis

When the scale of Docker deployment becomes large...

MySQL process control IF(), IFNULL(), NULLIF(), ISNULL() functions

In MySQL, you can use IF(), IFNULL(), NULLIF(), a...

React's method of realizing secondary linkage

This article shares the specific code of React to...

html option disable select select disable option example

Copy code The code is as follows: <select> ...