Skip to content

Instantly share code, notes, and snippets.

@iradul
Last active August 22, 2024 10:00
Show Gist options
  • Select an option

  • Save iradul/af167a00505f12a6cf92add7ee21d7c3 to your computer and use it in GitHub Desktop.

Select an option

Save iradul/af167a00505f12a6cf92add7ee21d7c3 to your computer and use it in GitHub Desktop.
Remove all Docker images filtered by `pattern` except latest `n`
#some grep pattern
pattern=192.168
# save latest n images
n=1
# check first (?):
# docker inspect -f '{{ .Created }} {{ .Id }} {{ .RepoTags }}' $(docker images | tail -n+2 | sed 's/^\([^ ]*\) *\([^ ]*\) *\([^ ]*\).*/ \1:\2 \3 /' | grep $pattern | cut -d' ' -f3) | sort -r | tail -n+$((n+1))
docker rmi $(docker inspect -f '{{ .Created }} {{ .Id }}' $(docker images | tail -n+2 | sed 's/^\([^ ]*\) *\([^ ]*\) *\([^ ]*\).*/ \1:\2 \3 /' | grep $pattern | cut -d' ' -f3) | sort -r | tail -n+$((n+1)) | cut -d' ' -f2 | cut -c8-)
@leozilla
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment