โ Docker provides several commands for managing containers and images. Below are some important commands that help you stop and delete containers and images effectively.
To stop all running containers, you can use the following command:
docker stop $(docker ps -a -q)
docker ps -a -q
: This command lists the container IDs of all containers (running and stopped).docker stop
: This stops the containers that are listed by the previous command.Purpose: This command stops all running containers in a single command, saving time and effort if you have multiple containers running. โณ
After stopping the containers, you might want to clean up by removing those containers that are no longer in use. The following command deletes all stopped containers:
docker rm $(docker ps -a -q)
docker ps -a -q
: Again, this lists the container IDs of all containers, including the stopped ones.docker rm
: This removes the containers from your system.Purpose: This command helps clean up your system by removing all containers that are not currently running. ๐งน
If you want to free up disk space by removing unused Docker images, use the following command:
docker rmi -f $(docker image -q)
docker image -q
: This lists the IDs of all Docker images present on your system.docker rmi -f
: This forcibly removes the images listed by the previous command.