Use the docker ps command with the -a flag to locate the name or ID of the containers you want to remove:
To list the containers:
docker ps -a
To remove the containers:
docker rm ID_or_Name
If you know when you're creating a container that you won't want to keep it around once you're done, you can run docker run –rm to automatically delete it when it exits.
Run and Remove:
docker run --rm image_name
You can locate containers using docker ps -a and filter them by their status: created, restarting, running, paused, or exited.
To review the list of exited containers, use the -f flag to filter based on status.
When you've verified you want to remove those containers, using -q to pass the IDs to the docker rm command.
To list the containers:
docker ps -a -f status=exited
To remove the containers:
docker rm $(docker ps -a -f status=exited -q)
docker rm $(docker ps -aq)
Docker filters can be combined by repeating the filter flag with an additional value.
This results in a list of containers that meet either condition.
For example, if you want to delete all containers marked as either Created (a state which can result when you run a container with an invalid command) or Exited, you can use two filters:
To list the containers:
docker ps -a -f status=exited -f status=created
To remove the containers:
docker rm $(docker ps -a -f status=exited -f status=created -q)
You can find all the containers that match a pattern using a combination of docker ps and grep.
When you're satisfied that you have the list you want to delete, you can use awk and xargs to supply the ID to docker rmi.
NOTE: These utilities are not supplied by Docker and not necessarily available on all systems.
To list the containers:
docker ps -a | grep "pattern”
To remove the containers:
docker ps -a | grep "pattern" | awk '{print $3}' | xargs docker rmi
You can review the containers on your system with docker ps.
Adding the -a flag will show all containers.
When you're sure you want to delete them, you can add the -q flag to supply the IDs to the docker stop and docker rm commands:
To list the containers:
docker ps -a
To remove the containers:
docker stop $(docker ps -a -q) docker rm $(docker ps -a -q)