Pruning containers

In this section we want to regain unused system resources by pruning containers. Let's start with this command:

$ docker container prune

The preceding command will remove all containers from the system that are not in running status. Docker will ask for confirmation before deleting the containers that are currently in  exited or created status. If you want to skip this confirmation step you can use the -f (or --force) flag:

$ docker container prune -f 

Under certain circumstances, we might want to remove all containers from our system, even the running ones. We cannot use the prune command for this. Instead we should use a command, such as the following combined expression:

$ docker container rm -f $(docker container ls -aq) 

Please be careful with the preceding command. It removes all containers without warning, even the running ones! Please, before you proceed look at the preceding command again in detail and try to explain what exactly happens and why.