Here are some commands that I use for cleaning up unused or dangling docker images.

## New commands (for docker >= 1.13)
# clean up all unused containers, networks, images (both dangling and unreferenced)
$ sudo docker system prune

# remove images older than 4 months since their creation
$ sudo docker system prune -a --filter "until=3840h"

## Old ones
# remove exited containers
$ sudo docker ps -a | grep Exit | cut -d ' ' -f 1 | xargs sudo docker rm

# remove dangling images
$ sudo docker rmi $(docker images -f "dangling=true" -q)

# remove images older than 4 months since their creation
$ sudo docker system prune -a --filter "until=3840h"

References

  1. How to remove old and unused Docker images
  2. How to remove old Docker containers