Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort my Docker images by size with `docker images` command?

To see all the images I have installed, I run docker images. I'd like to sort all my images by "SIZE". How can I do this? More generally, how does one sort the returned images by any parameter, such as "CREATED"

like image 301
yalpsid eman Avatar asked Sep 23 '19 15:09

yalpsid eman


People also ask

How do I determine the size of a docker image?

To view the approximate size of a running container, you can use the command docker container ls -s . Running docker image ls shows the sizes of your images. To see the size of the intermediate images that make up your image use docker image history my_image:my_tag .

What command will list all docker images?

The easiest way to list Docker images is to use the “docker images” with no arguments. When using this command, you will be presented with the complete list of Docker images on your system. Alternatively, you can use the “docker image” command with the “ls” argument.


4 Answers

docker images supports --format flag to customize output -> https://docs.docker.com/engine/reference/commandline/images/#format-the-output

Adding custom formatting and using sort does the trick:

docker images --format "{{.ID}}\t{{.Size}}\t{{.Repository}}" | sort -k 2 -h
like image 108
Gonzalo Matheu Avatar answered Sep 26 '22 06:09

Gonzalo Matheu


None of the answers correctly show how to sort the actual docker images command.

Here is a bona fide solution:

docker images | sort -k7 -h
like image 37
Theodore R. Smith Avatar answered Sep 26 '22 06:09

Theodore R. Smith


Logical solution should be

docker images | sort -k5 -h

but this doesn't work, because docker emits spaces instead of tabs.

Created issue for that(https://github.com/docker/cli/issues/2406).

If you have time and know go, please contribute the fix :)

Till fixed, you can use some wrapper script like https://github.com/pixelastic/oroshi/blob/master/scripts/bin/docker-image-list

like image 38
Michel Samia Avatar answered Sep 26 '22 06:09

Michel Samia


If using PowerShell, you can sort by arbitrary fields such as repo/image name as follows:

docker images --format="{{json .}}" |
    ConvertFrom-Json |
    Sort-Object Repository |
    Select-Object Repository,Tag,ID,CreatedSince,Size |
    Format-Table

though sadly it doesn't work well on human-readable fields such as Size so it unfortunately doesn't answer the more specific first part of your question.

like image 36
sparrowt Avatar answered Sep 22 '22 06:09

sparrowt