To safely cleanup Docker resources like images, containers, and disk space, you can follow the steps below:
-
Stop and remove all running containers:
docker stop $(docker ps -a -q) docker rm $(docker ps -a -q)
-
Delete all unused Docker images:
docker image prune -a
This command will remove all images that are not associated with a container or tag and are not being used by any other images.
-
Remove all unused Docker volumes:
docker volume prune
This command will remove all volumes that are not associated with a container.
-
Clear the Docker build cache:
docker builder prune
This command will remove all unused build cache data.
-
Remove all unused Docker networks:
docker network prune
This command will remove all networks that are not associated with a container.
-
Remove all stopped Docker containers: (Be careful)
docker container prune
This command will remove all containers that are not running.
-
Check the amount of disk space used by Docker:
docker system df
This command will show you how much space is being used by Docker on your system.
-
Finally, to reclaim disk space used by Docker, you can run: (Be careful)
docker system prune -a --volumes
This command will remove all unused resources, including containers, images, networks, and volumes.
Note: Be careful when running these commands, as they will remove all unused resources. Make sure you do not delete any resources that are still being used by your running containers.