Cleaning up after Docker – Commands

Cleaning up after Docker – Commands

After playing around with Docker these last few months something that I find annoying is these unused containers and images taking up valuable disk space.

Here are some commands that will help you clean this all up:

Kill all running containers:
$ docker kill $(docker ps -q)

Delete all stopped containers (including data-only containers):
$ docker rm $(docker ps -a -q)

Alternative to previous: Delete old docker containers that have been running in the past (weeks, hours, months, etc…). Vary the word after ‘grep’ to target the containers you want to remove – example: use ‘months’, ‘weeks’, ‘hours’, ‘minutes’, etc… depending on the age of the container that are running. Use docker ps -a to check this first.

$ docker ps -a | grep 'hours' | awk '{print $1}' | xargs --no-run-if-empty docker rm

$ docker ps -a | grep 'weeks' | awk '{print $1}' | xargs --no-run-if-empty docker rm

Delete all ‘untagged/dangling’ (<none>) images:
$ docker rmi $(docker images -q -f dangling=true)

Delete ALL images (be careful with this):
$ docker rmi $(docker images -q)

Enjoy Docker!

2 thoughts on “Cleaning up after Docker – Commands”

Leave a Reply

Your email address will not be published. Required fields are marked *