This oneliner will query for last 5 containers and removes them.
docker ps -a -n 5 -q | xargs docker rm
It is useful when you have dangling containers after failed builds
Afterward, you can launch image cleanup because you won't be tied with stopped containers.
docker rmi `docker images -qf dangling=true`