Docker batch start and close all containers

Docker batch start and close all containers

In Docker

Start all container commands

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

In Docker

Close all container commands

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

Supplement: Shell batch start and delete docker containers, based on docker toolbox

Stop the container

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} 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

Note: The containerIDs are what you have already built. The docker container here is nginx

Put stop.sh under d:/nginx.

The process is a little slow, please be patient.

Stopping the container successfully.

Start the container

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

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Each time Docker starts a container, the IP and hosts specified operations
  • Detailed explanation of Shell script control docker container startup order
  • Docker modifies the configuration information of an unstarted container
  • Dockerfile implementation code when starting two processes in a docker container
  • Docker file storage path, get container startup command operation
  • Solve the problem of docker container exiting immediately after starting

<<:  MySQL query redundant indexes and unused index operations

>>:  How to represent various MOUSE shapes

Recommend

36 principles of MySQL database development (summary)

Preface These principles are summarized from actu...

How to solve the margin collapse problem in CSS

First, let's look at three situations where m...

Summary of CSS front-end knowledge points (must read)

1. The concept of css: (Cascading Style Sheet) Ad...

React and Redux array processing explanation

This article will introduce some commonly used ar...

Integration practice of Vue+Element background management framework

Table of contents Vue+ElementUI background manage...

4 ways to implement routing transition effects in Vue

Vue router transitions are a quick and easy way t...

Detailed explanation of location and rewrite usage in nginx

1. Summary of location usage Location can locate ...

Detailed explanation of the usage and difference between nohup and & in Linux

Example: We use the Python code loop_hello.py as ...

MySQL functional index optimization solution

When using MySQL, many developers often perform f...

IE6/7 is going to be a mess: empty text node height issue

Preface: Use debugbar to view document code in iet...

Solution for adding iptables firewall policy to MySQL service

If your MySQL database is installed on a centos7 ...

Using Docker Enterprise Edition to build your own private registry server

Docker is really cool, especially because it'...