Last active
August 22, 2024 10:00
-
-
Save iradul/af167a00505f12a6cf92add7ee21d7c3 to your computer and use it in GitHub Desktop.
Remove all Docker images filtered by `pattern` except latest `n`
This file contains hidden or 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
| #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-) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried your script but it didnt behave as I expected so I came up with this:
https://stackoverflow.com/questions/58406115/print-first-n-rows-per-group-remove-all-docker-images-except-newest-n-for-eac