This code removes all Docker containers, regardless of their running status, by first listing their IDs and then using those IDs to target the docker rm
command.
npm run import -- "Delete all containers"
docker rm $(docker ps -a -q)
#!/bin/bash
# Remove stopped and running containers
# Usage: remove_containers.sh
# Get a list of all container IDs
container_ids=$(docker ps -a -q)
# Check if any containers were found
if [ -n "$container_ids" ]; then
# Remove the containers one by one to avoid errors
for id in $container_ids; do
docker rm -f "$id"
done
else
# If no containers were found, print a message
echo "No running or stopped containers found."
fi
This code snippet removes all Docker containers, both running and stopped.
Here's a breakdown:
docker ps -a -q
:
-q
flag).$(...)
:
docker ps -a -q
) and passes the output (the list of container IDs) to the next command.docker rm
:
This command removes Docker containers.
By combining it with the output of docker ps -a -q
, it effectively removes all containers listed.
In essence, this command provides a concise way to clean up all Docker containers on your system.