I want to delete non latest intermediate image with the label from docker. I used the below command, it works if there is more than 1 image, but if there is exactly 1 image, then it is deleting that image and again caching will happen from the next build. I'm new to Bash Scripting, any help would be highly appreciated.
What I want
if image_ids_with_label>1:
delete_nonlatest_images
else:
pass
Bash Command
docker images --filter "label=ImageName="$LabelName --format '{{.CreatedAt}}\t{{.ID}}' | sort -nr | tail -n -1 | cut -f2 | xargs --no-run-if-empty sudo docker rmi