Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sort docker images by size desc
#!/bin/sh
docker images --format '{{.Size}}\t{{.Repository}}\t{{.Tag}}\t{{.ID}}' | sed 's/ //' | sort -h -r | column -t
@Aracki

This comment has been minimized.

Copy link

@Aracki Aracki commented Jun 15, 2018

nice alternative:

alias dis='docker images --format '{{.Size}}\t{{.Repository}}' | sort -r'

@jthomerson

This comment has been minimized.

Copy link

@jthomerson jthomerson commented Jul 31, 2018

@Aracki yours needs to be sort -hr (added h) to sort things correctly when you have different scales of sizes reported (e.g. some are 4MB, 95.3MB, and others are 1.02GB.

@cuongtransc

This comment has been minimized.

Copy link

@cuongtransc cuongtransc commented Aug 6, 2018

More pretty:

docker images --format '{{.Size}}\t{{.Repository}}:{{.Tag}}\t{{.ID}}' | sort -h | column -t
@andyrbell

This comment has been minimized.

Copy link
Owner Author

@andyrbell andyrbell commented Sep 9, 2018

@cuongtransc thanks. have updated with the column -t

@quantonganh

This comment has been minimized.

Copy link

@quantonganh quantonganh commented Mar 7, 2019

docker images | sort -k7 -h
@MrBuBBLs

This comment has been minimized.

Copy link

@MrBuBBLs MrBuBBLs commented Jun 27, 2019

Showing & keeping the leading column title where they are:

docker images | awk 'NR<2{print $0;next}{print $0| "sort -k7 -hr"}'

Add | head to get only the biggest ones

@emsi

This comment has been minimized.

Copy link

@emsi emsi commented Aug 20, 2019

docker images | sort -k7 -h

@quantonganh Like a pro! :) Kudos.

@ciarancourtney

This comment has been minimized.

Copy link

@ciarancourtney ciarancourtney commented Aug 22, 2019

To be clear, its -k7, not -k5, because there are generally two spaces in the preceding CREATED column

@ddefrancesco

This comment has been minimized.

Copy link

@ddefrancesco ddefrancesco commented Sep 21, 2019

Schermata 2019-09-21 alle 13 50 06

Nice, very clear, could used by default...unuseful to me though because it's not a real sort by size that I looked for, I'll keep searching, thx anyway...

@ksilz

This comment has been minimized.

Copy link

@ksilz ksilz commented Nov 27, 2019

docker images | sort -k7 -h

@quantonganh Great one! I just added an -r to show the biggest image first:

docker images | sort -k7 -h -r

@ksilz

This comment has been minimized.

Copy link

@ksilz ksilz commented Nov 27, 2019

docker images | sort -k7 -h

@quantonganh I referenced your command in another post.

@simne7

This comment has been minimized.

Copy link

@simne7 simne7 commented Feb 26, 2020

I had to replace the decimal separator in dockers {{.Size}} which seems to be always a dot, with the one of my locale to make sort -h work properly.

docker images --format '{{.Size}}\t{{.Repository}}:{{.Tag}}\t{{.ID}}' | sed 's/\./,/' | sort -hr | column -t

Note: the sed command blindly replaces the first "." per line.

@emsi

This comment has been minimized.

Copy link

@emsi emsi commented Feb 27, 2020

I had to replace the decimal separator in dockers {{.Size}} which seems to be always a dot, with the one of my locale to make sort -h work properly.

Setting locale collate would do the trick as well. Something like this should work too:
docker images | LC_COLLATE=C.UTF-8 sort -k7 -hr

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