Show docker container size

If you build docker containers, ensure that you don’t write any data within the containers. Therefore you can use mapped volumes or data containers. Basically your docker containers that hosts the application or service should be immutable. I won’t go into details why, but how to check that a docker container does not grow.

Docker provided the ps command with the option -s or --size.

The command

sudo docker ps -s

will give some output. Depending how many containers you have, it can be a little bit unreadable. See example output:

tan@epsilon:~> sudo docker ps -s
CONTAINER ID        IMAGE                                   COMMAND                  CREATED             STATUS              PORTS                                                                                                                                                                                                                        NAMES                         SIZE
f68a2ba2b243        6fd8f0170256                            "/bin/sh -c '$JAVA_HO"   3 days ago          Up 3 days >2251/tcp,>8080/tcp                                                                                                                                                                             trusting_meitner              32.77 kB (virtual 410.3 MB)
390b7f2e559f        mongo:3.2.9                             "/entrypoint.sh mongo"   3 days ago          Up 3 days           27017/tcp                                                                                                                                                                                                                    kickass_yalow                 8 B (virtual 366.3 MB)
8e64707ad052        elasticsearch:2.4.0         "/docker-entrypoint.s"   7 days ago          Up 7 days                                                                                                                                                                                                                                        elasticsearch                 32.77 kB (virtual 413.4 MB)
a428d4a08dc6        14b6487a15bf                            "/bin/sh -c '$JBOSS_H"   9 days ago          Up 9 days >8080/tcp    

Therefore docker provides the formatting option: Pretty-print containers using a Go template

sudo docker ps --format '{{.Names}}\n{{.Image}}:{{.Size}}\n' -s

The placeholders are described at Docker Reference ps formatting.

This will give us some more readable output:

elasticsearch:2.4.0:32.77 kB (virtual 413.4 MB)

kibana-test:4.6.1:121.5 kB (virtual 661.7 MB)

716ee716f7bf:864.7 kB (virtual 664.3 MB)

8e28731dcc86:71.51 MB (virtual 1.425 GB)

c18ea635a8ed:864.6 kB (virtual 664.3 MB)