This Windows batch script iterates through a list of all Docker container IDs and forcefully removes each container.
npm run import -- "Delete containers in cmd"
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
bash
#!/bin/bash
# Function to remove all stopped containers
remove_stopped_containers() {
# Use docker ps with -a and -q flags to get a list of stopped container IDs
stopped_containers=$(docker ps -aq)
# Iterate over each stopped container and remove it
for container in $stopped_containers; do
# Use docker rm with -f flag to force removal of the container
docker rm -f "$container"
done
}
# Call the function to remove stopped containers
remove_stopped_containers
This code snippet forcefully removes all Docker containers, both running and stopped.
Here's a breakdown:
FOR /f "tokens=*" %i IN ('docker ps -a -q')
:
FOR
) to iterate over the output of a command.docker ps -a -q
: Lists all Docker containers (both running and stopped) and outputs only their IDs (container names are not included).tokens=*
: Tells the loop to capture all tokens (words) from the output.%i
: Represents a variable that will hold each container ID during each iteration of the loop.DO docker rm %i
:
docker rm %i
: Removes the Docker container specified by the %i
variable.In essence, the code does the following:
docker rm
command.Let me know if you have any other code snippets you'd like me to explain!