Clean up Docker images, containers and volumes with single commands



List Docker processes and data docker container ls list containers, also can be shown with docker ps docker image ls list images, there is also the command docker images docker volume ls list volumes docker network ls lists networks docker info lists the number of containers and image Just clean out unused data and processes
docker system prune

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all build cache
docker system prune --all --force --volumes

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all volumes not used by at least one container
        - all images without at least one container associated to them
        - all build cache
prune can also be used on just one aspect: docker container prune # Remove all stopped containers docker volume prune # Remove all unused volumes docker image prune # Remove unused images Destroy all for a complete Docker refresh
docker container ls -aq
The full command for stopping all docker containers is now:
docker container stop $(docker container ls -a -q)
A complete Docker system clean can now be achieved by linking this with the full prune command covered earlier in this post.
docker container stop $(docker container ls -a -q) && docker system prune -a -f --volumes
Similarly, you now know how to combine docker commands to just remove one aspect if you prefer. Pass a list of all the IDs to the associated remove command. Containers docker container rm $(docker container ls -a -q) Images docker image rm $(docker image ls -a -q) Volumes docker volume rm $(docker volume ls -q) Networks docker network rm $(docker network ls -q) How to reuse them easily with shell aliases
alias docker-clean-unused='docker system prune --all --force --volumes'
alias docker-clean-all='docker stop $(docker container ls -a -q) && docker system prune -a -f --volumes'
Selim Reza

Site Footer