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"
11 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
-
16Including the tag if you have different versions of the same image and want to identify them:
docker images --format "{{.ID}}\t{{.Size}}\t{{.Repository}}:{{.Tag}}" | sort -k 2 -h
Commented May 20, 2020 at 19:26 -
Why pass a custom
--format
flag when size is already included as column 7 by default? See comments on @Michel Samia's answer below Commented Nov 12, 2020 at 18:58 -
1Hint:
sort -h
does not respect decimal point in numbers, or do I miss something?– ralphCommented Jan 12, 2023 at 10:41 -
As @ralph mantioned above this doesn't sort properly because of decimal points...– MoReCommented Jan 31, 2023 at 15:14
-
sort -h
here (on Rocky 9, e.g., same as RHEL 9:sort (GNU coreutils) 8.32
) only works if there is no space between the unit and the number. I.e., the unit of500MB
would get recognized, but500 MB
would be just a number (not a megabyte).– chutzCommented Apr 7, 2023 at 7:39
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
Note: it's the 7th, not 5th column, because we have to skip additional 2 spaces created by the time unit and "ago" (e.g. to sort by "1.8GB" rather than by "weeks" in: "name tag hash 7 weeks ago 1.8GB"). This trick was first posted in one of the comments under this answer.
-
This doesn't sort properly when there are numbers with decimal points.– MoReCommented Jan 31, 2023 at 15:15
docker image ls --format "{{.Size}} {{.ID}} {{.Repository}}:{{.Tag}}" | LANG=en_US sort -h | column -t
# sample output
1.24MB 5ba631491225 busybox:latest
206MB 8ce5afe3d430 registry.hub.docker.com/gentoo/portage:latest
974MB d06781d69b4c registry.hub.docker.com/gentoo/stage3:systemd
1GB d13df3eacaa7 gentoo-distrobox:latest
24GB e7512ebcf557 ghcr.io/fabceolin/windev:usb
-
Seems to be the only answer which sorts correctly when floating points are involved– KutziCommented Mar 29, 2023 at 7:05
-
Bonus track:
docker container ls -s -a --format "{{.Size}} {{.Names}}" | sed -r 's/\(virtual .*\)//' | LANG=en_US sort -h
Commented Jul 5, 2023 at 0:49 -
Actually these 2 seem to have issue sorting it correctly, too. E.g. 149MB a 2.14MB b 327MB c 64.8MB d 688MB e 71.7MB f– KutziCommented Jul 5, 2023 at 15:50
-
I ended up writing a python script:
docker images | python3 -c "import sys; from humanfriendly import parse_size; print('\n'.join(sorted(sys.stdin.read().strip().split('\n')[1:], key=lambda x: parse_size(x.split()[6]))))"
Commented Jan 9 at 17:23 -
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
-
3It's not because of tabs that do not work. It's because the 5 (fifth) column it is not size, it is a part of the CREATED column. Commented Apr 8, 2020 at 0:33
-
11
-
2A locale-independent command (because a floating-point char) is:
docker image ls | LANG=C sort -k7 -hr
Commented Jan 24, 2022 at 10:20 -
None of these works correctly, if floating points are involved. Not even Fabricio's one– KutziCommented Mar 29, 2023 at 7:04
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.
-
I think size is only human readable when printed. I was able to sort using it. See my answer: stackoverflow.com/a/71959947/1814970 (tried to suggest an edit here, but SO didn't allow). Commented Apr 21, 2022 at 19:30
-
1Ah that's good to know, perhaps an improvement in Docker since I posted this originally!– sparrowtCommented Apr 27, 2022 at 13:24
Tried to suggest an edit, but SO didn't allow me, saying that the queue was full.
Building on @sparrowt's answer for a PowerShell solution, I believe Size
is only human readable when printed. So if you convert them to integer first, you can sort:
(This is the same code from the other answer, changing one line.)
docker images --format="{{json .}}" |
ConvertFrom-Json |
# Sort-Object Repository | # ORIGINAL
Sort-Object Repository, { [int]$_.Size } | # SORTING BY SIZE
Select-Object Repository,Tag,ID,CreatedSince,Size |
Format-Table
To sort by any column set sort_column
to the number from 1
to 4
.
To sort descending set desc
to 1
sort_column=4 && \
desc=0 && \
delim="$(printf '\t')" && \
{ \
printf "REPOSITORY:TAG${delim}IMAGE ID${delim}CREATED${delim}SIZE\n" && \
docker images --format "{{.Repository}}:{{.Tag}}${delim}{{.ID}}${delim}{{.CreatedAt}}${delim}{{.Size}}" | \
LC_ALL=C sort -t"$delim" -k"$sort_column" -h \
$([ ${desc:-} -eq 0 ] || echo "-r"); \
} | \
column -t -s"$delim"
Same code as a one-liner:
sort_column=4 && desc=0 && delim="$(printf '\t')" && { printf "REPOSITORY:TAG${delim}IMAGE ID${delim}CREATED${delim}SIZE\n" && docker images --format "{{.Repository}}:{{.Tag}}${delim}{{.ID}}${delim}{{.CreatedAt}}${delim}{{.Size}}" | LC_ALL=C sort -t"$delim" -k"$sort_column" -h $([ ${desc:-} -eq 0 ] || echo "-r"); } | column -t -s"$delim"
Example sort by SIZE
Descending
sort_column=4
desc=1
REPOSITORY:TAG IMAGE ID CREATED SIZE
android-sdk:latest 08eeb5aa5e29 2022-01-22 21:14:23 1.1GB
node:13.8.0-buster f6de44b06e1f 2021-12-21 22:28:44 910MB
debian:buster 58075fe9ecce 2020-09-30 02:33:47 114MB
ubuntu:bionic d27b9ffc5667 2019-03-12 01:21:12 65MB
hello-world:latest 4ab4c602aa5e 2018-10-11 21:21:30 1.9kB
Example sort by REPOSITORY:TAG
Ascending
sort_column=1
desc=0
REPOSITORY:TAG IMAGE ID CREATED SIZE
android-sdk:latest 08eeb5aa5e29 2022-01-22 21:14:23 1.1GB
debian:buster 58075fe9ecce 2020-09-30 02:33:47 114MB
hello-world:latest 4ab4c602aa5e 2018-10-11 21:21:30 1.9kB
node:13.8.0-buster f6de44b06e1f 2021-12-21 22:28:44 910MB
ubuntu:bionic d27b9ffc5667 2019-03-12 01:21:12 65MB
Try the --format
table according to the docker documentation of docker-images
e.g. sort by repository name
docker images --format 'table {{.Repository}}\t{{.ID}}\t{{.Tag}}\t{{.Size}}' | (read -r; printf "%s\n" "$REPLY"; sort -h -k7)
Output:
REPOSITORY IMAGE ID TAG SIZE
danieltobon43/pcl-docker 06a35efdd8ec 1.0-alpine3.15-dev 1.56GB
danieltobon43/pcl-docker 06a35efdd8ec 1.12.1-alpine3.15-dev 1.56GB
danieltobon43/pcl-docker 39415115561c 1.9.1-ubuntu20.04 5.75GB
danieltobon43/pcl-docker 7369903b06b3 1.0-alpine3.15 1.27GB
danieltobon43/pcl-docker d412b4ff42d5 1.12.1-alpine3.15 531MB
dbscan 6416b9ed2fa1 1.0-ubuntu20.04 2.1GB
dbscan f3a4fd088d94 1.0-alpine3.15 1.32GB
ghcr.io/patrickhoefler/dockerfilegraph 93485687d9ab 0.7.3-alpine 44.9MB
github/super-linter 385b056df558 latest 6.35GB
myapp 8ccfe15f0ebd 1.0 71.9MB
pcl-docker d412b4ff42d5 1.12.1-alpine3.5 531MB
pcl-docker e4b28f82aab5 1.12.1-v1.2-alpine3.15 594MB
vtk-docker aacf6a38b5ff 8.1.0-alpine3.5 1.11GB
I have a hard time figuring out the keys, but have found jq
to be very helpful to quickly get this kind of stuff done. E.g.:
$ docker images --format json | jq -r 'unique | sort_by(-.Size) | .[] | [ .Id[:12], .Size, .CreatedAt, .Names[] ] | @tsv'
5cffd02133cb 915590610 2023-02-11T03:24:34Z docker.io/library/ruby:latest
9124e6ec78cc 886347105 2023-02-11T03:37:24Z docker.io/library/ruby:2.7
6a658e3ed635 513900464 2023-01-24T15:17:16Z docker.io/pipelinecomponents/ansible-lint:latest
1de5905a6164 416218073 2022-11-30T23:30:42Z docker.io/library/mariadb:latest
68f5d950dcd3 386595867 2022-11-15T06:38:54Z docker.io/library/postgres:latest
1403af3b6d4a 308156195 2021-08-06T18:32:44Z docker.io/library/golang:1.15-alpine
818ca3531f99 264457584 2023-04-04T18:26:39Z docker.io/library/golang:1.20-alpine
eeb6ee3f44bd 211690387 2021-09-15T18:20:23Z docker.io/library/centos:7
63f107ef819e 203491339 2022-12-19T23:20:28Z docker.io/library/rockylinux:8.7 docker.io/library/rockylinux:8
ce99dcf19c24 181691319 2022-12-22T02:41:17Z docker.io/library/rockylinux:9.1 docker.io/library/rockylinux:9
Instead of the above two approaches by @sparrowt and @marcelora what ended up working for me was
docker images --format="{{json .}}" |
ConvertFrom-Json |
Sort-Object {$_.Size/1KB} |
Select-Object Repository,Tag,ID,CreatedSince,Size |
Format-Table
It seems that the $_.Size/1KB part was of importance in my scenario
Without it I would receive errors like
Cannot convert value "xGB" to type "System.Int32". Error: "The input string 'xGB' was not in a correct format."
Do note that my answer is almost completely derived from the above two answers with small changes which ended up helping me out personally.
Other approach, using python
docker images | python3 -c "import sys; from humanfriendly import parse_size; print('\n'.join(sorted(sys.stdin.read().strip().split('\n')[1:], key=lambda x: parse_size(x.split()[6]))))"