Created
February 10, 2022 13:26
-
-
Save bodsch/407fdf460005e237473de652dae34944 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# delete dead containers | |
docker container prune -f | |
# keep last 2 builds for each image from the repository | |
for diru in $(docker images \ | |
--format "{{.Repository}}" | sort | uniq) | |
do | |
for dimr in $(docker images \ | |
--format "{{.ID}};{{.Repository}}:{{.Tag}};'{{.CreatedAt}}'" \ | |
--filter reference="$diru" | sed -r "s/\s+/~/g" | tail -n+3) | |
do | |
img_tag=$(echo $dimr | cut -d";" -f2); | |
docker rmi $img_tag; | |
done; | |
done | |
# clean dangling images if any | |
docker image prune -f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment