Docker prettify docker ps output
  August 1, 2020

Everytime I run docker ps to list my containers, I find hard to understand where does the list start and where it ends:

What the! ... the port, which one? ... I almost feel like I'm seeing the matrix

I have to say, after some time your eyes get used to and begin to do weird movements but at the end you begin to understand the output, of course it shouldn't be this way, so I found there are server ways to improve the command output, let's take a look.

Use —format

Use the option --format, using this we can choose from a list of fields (see the table below) and display them in a ordered layout using the following syntax table {{.FieldName}}\t. The format option uses Go templates underneath, hey that looks familiar to me1.

table print the field name and \t adds space between columns.

 Example using --format


 docker ps --format 'table {{.Names}}\t
                           {{.Status}} : {{.RunningFor}}\t

And we got this :
Definitely a better command output so much easy to read and understand

Taking advantage of Go Templates

We can get creative with Go Templates and lets query our containers to get network information including ports (a clean output would be to show every port in his own line but only if ports are used):

 Network information


docker ps --format 'table {{.Names}}
                          {{if .Ports}}
                            {{with $p := split .Ports ", "}}
                              {{range $p}}\t\t{{println .}}{{end}}
                            \t\t{{println "No Ports"}}

Here you have, we can get all network data in a more readable way

All containers docker ps –a

Using the option ps -a will list all containers in any state. So it would be nice to add the disk space, a summary of the network use and the image from where the container derives.

 All containers list


docker ps -a -q --format 'table {{.Names}}\t
                                {{if .Ports}}
                                  {{with $p := split .Ports ", "}}\t
                                    {{len $p}} port(s) on {{end}}{{- .Networks}}
                                  {{else}}\tNo Ports on {{ .Networks }}

Look at this! An easier way to spot information about all containers.

Create a function to reuse the commands

Finally we can create a function to reuse these commands (if you are using WSL or Linux) inside our .bash_aliases file

 Create a bash function that allows parameters


# Docker PS Prettify Function
function dock() {
  if [[ "$@" == "ps" ]]; then
    command docker ps --format 'table {{.Names}}\t{{.Status}} : {{.RunningFor}}\t{{.ID}}\t{{.Image}}'
  elif [[ "$@" == "psa" ]]; then
    # docker ps -a includes all containers
    command docker ps -a --format 'table {{.Names}}\t{{.Status}}\t{{.Size}}\n{{.ID}}\t{{.Image}}{{if .Ports}}{{with $p := split .Ports ", "}}\t{{len $p}} port(s) on {{end}}{{- .Networks}}{{else}}\tNo Ports on {{ .Networks }}{{end}}\n'
  elif [[ "$@" == "psnet" ]]; then
    # docker ps with network information
    command docker ps -a --format 'table {{.Names}}\t{{.Status}}\t{{.Networks}}\n{{.ID}}{{if .Ports}}{{with $p := split .Ports ", "}}{{range $p}}\t\t{{println .}}{{end}}{{end}}{{else}}\t\t{{println "No Ports"}}{{end}}'
    command docker "$@"

To use the function just type:

dock ps
List running containers and its image name
dock psa
List all containers no matter what state and includes disk space and network information
dock psnet
List running containers with detailed network information

Available fields in docker

Container ID
Image ID
Quoted command
Time when the container was created.
Elapsed time since the container was started.
Exposed ports.
Container status.
Container disk size.
Container names.
All labels assigned to the container.
Value of a specific label for this container. For example ‘{{.Label “com.docker.swarm.cpu”}}’
Names of the volumes mounted in this container.
Names of the networks attached to this container.

  1. This blog uses HUGO, and HUGO uses GoLang html and text templates. ↩︎