Useful Docker commands
Stop all Docker containers
To stop all Docker containers from the command line, you can use the following command:
docker stop $(docker ps -q)
Explanation:
- docker ps -q: This command lists the container IDs of all running containers.
- docker stop: This stops the containers provided as arguments.
So, $(docker ps -q) will pass all running container IDs to docker stop, effectively stopping all of them.
Remove ALL Docker containers Networks
WARNING (USED / UNUSED Networks) This will remove all Networks from ALL container even the one you use.
docker network rm $(docker network ls -q)
Explanation:
- docker network ls -q: Lists all network IDs (including those that are in use).
- docker network rm: Removes networks by their IDs.
This will attempt to remove all networks, including the default ones if they are not in use by containers. However, if a network is in use by a container, Docker won't allow you to remove it without first disconnecting the containers.
If you really want to remove all networks including the ones in use, you'd have to stop and remove the containers using those networks first.
Remove all Docker volumes (including unused ones)
To remove all Docker volumes (including unused ones), you can use the following command:
docker volume prune -fa
Explanation:
- docker volume prune: Removes all unused volumes (volumes that are not currently associated with any container).
- -f or --force: This flag forces the operation without asking for confirmation.
If you want to remove all volumes — even the ones in use by containers (which is dangerous and typically not recommended) — you'd need to remove the containers and then delete the volumes. Here's how you can do it: