In this step, you will learn how to display the size of containers and format the output of the docker ps
command. Knowing the size of your containers can be helpful for managing disk space.
By default, docker ps
does not show the container size. To include the size information, you can use the --size
flag.
docker ps -a --size
This command will list all containers (running and stopped) and include two columns related to size: SIZE
(the size of the container's writable layer) and VIRTUAL SIZE
(the total size of the container, including the image layers).
Sometimes, the default output format of docker ps
might not be exactly what you need. You can format the output using the --format
flag. This flag takes a Go template string.
For example, to display only the container ID, image name, and status, you can use the following command:
docker ps -a --format "{{.ID}}\t{{.Image}}\t{{.Status}}"
In this format string:
{{.ID}}
refers to the container ID.
{{.Image}}
refers to the image name.
{{.Status}}
refers to the container status.
\t
is used for a tab character to separate the columns.
You can also include the size information in the formatted output.
docker ps -a --format "{{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Size}}"
This command will display the container ID, image, status, and size. The {{.Size}}
field includes both the writable layer size and the virtual size.
Experiment with different fields in the format string to customize the output according to your needs. Some other useful fields include {{.Names}}
, {{.CreatedAt}}
, and {{.Ports}}
.