This is a personal reference for Docker commands that are not used often but frequently need to be looked up. Instead of searching every time, this list provides direct access to those less common yet essential commands.
Remove All Stopped Containers
docker rm $(docker ps -a -q)
- Explanation: This command removes all stopped containers.
docker ps -a -q
: Lists the IDs of all containers (stopped and running).docker rm
: Removes the listed containers.
Remove All Docker Images Not In Use
docker rmi $(docker images -q)
- Explanation: This command removes all Docker images.
docker images -q
: Lists the IDs of all images.docker rmi
: Removes the listed images.
Check Docker Disk Usage
docker system df -v
- Explanation: Shows detailed information about Docker disk usage, including volumes, images, containers, and how much space they occupy.
Remove All Unused Volumes Except Specified Ones
Test Command (preview volumes to be deleted):
docker volume ls -qf dangling=true | grep -vE 'volume1|volume2'
Actual Removal Command:
docker volume ls -qf dangling=true | grep -vE 'volume1|volume2' | xargs -r docker volume rm
- Explanation: This command removes all unused (dangling) Docker volumes except the specified ones.
docker volume ls -qf dangling=true
: Lists all unused volumes.-q
: Shows only the volume names.-f dangling=true
: Filters to list only the volumes that are dangling (unused).
grep -vE 'volume1|volume2'
: Filters out the volumes you want to keep (replacevolume1
,volume2
, etc.).xargs -r docker volume rm
: Passes the remaining volume names todocker volume rm
and removes them. The-r
option ensures the command is only run if there are volumes to delete.