docker rm $(docker ps -a -q)
This will remove all stopped containers by getting a list of all containers with docker ps -a -q and passing their ids to docker rm. This should not remove any running containers, and it will tell you it can’t remove a running image.
docker rmi $(docker images -q --filter "dangling=true")