Docker: cleanup unused resources

Docker likes to eat up disk space, so having some scripts handy for occasional cleanup is useful.
I am storing here what I (currently) use for future reference.

All commands below delete unused/orphaned resources only, so should be safe to use. I run them while things I want to keep are running, that prevents unexpected deletions.

1
2
3
4
5
6
7
8
# delete volumes
docker volume ls -qf dangling=true | xargs docker volume rm

# delete images
docker images --filter "dangling=true" -q --no-trunc | xargs docker rmi

# delete containers
docker ps -qa --no-trunc --filter "status=exited" | xargs docker rm