Actually this is not too hard to achieve. We can just use docker ps to list all containers and use this list to stop each item in it. Before we can remove all containers we need to stop them first:
docker stop $(docker ps -a -q)
To remove all containers we just need to do the same with the rm command.
docker rm $(docker ps -a -q)
If you would like to remove all images just use the rmi command. But instead of ps (to list all containers) we use the images command:
docker rmi $(docker images -q)
That's it. Simple!