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

Blog    

Recommend

HTML+CSS to achieve surround reflection loading effect

This article mainly introduces the implementation...

Briefly describe the difference between MySQL and Oracle

1. Oracle is a large database while MySQL is a sm...

A brief discussion on CSS cascading mechanism

Why does CSS have a cascading mechanism? Because ...

Notes on configuring multiple proxies using vue projects

In the development process of Vue project, for th...

Example code of how to create a collapsed header effect using only CSS

Collapsed headers are a great solution for displa...

Solution to VMware virtual machine no network

Table of contents 1. Problem Description 2. Probl...

Detailed explanation of JavaScript timers

Table of contents Brief Introduction setInterval ...

Linux common basic commands and usage

This article uses examples to illustrate common b...

When should a website place ads?

I recently discussed "advertising" with...

Vue implements verification whether the username is available

This article example shares the specific code of ...

A brief discussion on the design and optimization of MySQL tree structure tables

Preface In many management and office systems, tr...

Detailed examples of using JavaScript event delegation (proxy)

Table of contents Introduction Example: Event del...

Detailed explanation of making shooting games with CocosCreator

Table of contents Scene Setting Game Resources Tu...

Example code for implementing an Upload component using Vue3

Table of contents General upload component develo...

Detailed tutorial on installing Docker on Windows

Since my local MySQL version is relatively low, I...