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

Detailed explanation of the use of nohup /dev/null 2>&1

nohup command: If you are running a process and y...

MySQL table return causes index invalidation case explanation

Introduction When the MySQL InnoDB engine queries...

Understanding innerHTML

<br />Related articles: innerHTML HTML DOM i...

How to solve the problem of MySQL query character set mismatch

Find the problem I recently encountered a problem...

Detailed explanation of MySQL 8.0 atomic DDL syntax

Table of contents 01 Introduction to Atomic DDL 0...

Mysql | Detailed explanation of fuzzy query using wildcards (like,%,_)

Wildcard categories: %Percent wildcard: indicates...

Detailed explanation of basic concepts of HTML

What is HTML? HTML is a language used to describe...

How to configure multiple tomcats with Nginx load balancing under Linux

The methods of installing nginx and multiple tomc...

How to realize vertical arrangement of text using CSS3

In a recent project, I wanted to align text verti...

Super detailed basic JavaScript syntax rules

Table of contents 01 JavaScript (abbreviated as: ...

Simple example of limit parameter of mysql paging

Two parameters of Mysql paging select * from user...

Detailed explanation of how to connect Java to Mysql version 8.0.18

Regarding the connection method between Java and ...

How to automatically execute SQL statements when MySQL in Docker starts

When creating a MySQL container with Docker, some...

How to use explain to query SQL execution plan in MySql

The explain command is the primary way to see how...