Skip to content

Instantly share code, notes, and snippets.

@bodsch
Created February 10, 2022 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bodsch/407fdf460005e237473de652dae34944 to your computer and use it in GitHub Desktop.
Save bodsch/407fdf460005e237473de652dae34944 to your computer and use it in GitHub Desktop.
#!/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