Docker documentation mentions removing all stopped containers using command:
$ docker rm $(docker ps -a -q)
but at times we want to first find existing containers and then remove them. This can be achieved when you filter and limit the result of docker ps
command.
Limit the result of docker ps
You can either use the --filter
, --last
or --latest
options or pipe the result of docker ps
to grep
command to filter the results for matching PATTERN.
Here is an example using grep
:
$ docker ps -a | grep PATTERN | \
awk '{print $1}' | xargs -r docker rm
where the PATTERN could be part of container name, image name or any other details present in the result of docker ps
command like mapped port number.
Feel free to share your feedback/opinion in comments.
Photo by Boxed Water Is Better on Unsplash
Top comments (0)