Better 'docker stats' output (with Name)

Problem:

The docker stats command isn't great - it gives you container ID vs name, which when you have a pretty small deployment like me (~20 containers) you usually know what services you're running, and want to see the actual names of these things.

Solution:

Like most docker commands, we can re-format the table output, I always forget the format and have to look it up every time.

I've already posted a similar article on docker ps here: GHOST_URL/2017/07/04/shorten-alias-docker-ps-output/
As a result, I'll be a little more brief here.

As a raw command, I'd use this:

docker stats --format "table {{.Name}}\t{{.Container}}\t{{.CPUPerc}}\t{{.MemPerc}}\t{{.NetIO}}\t{{.BlockIO}}"

This gives a nice tabled output and we can instantly see what we're using:

stats

If you want to alias this up, you can stick the following in your .bashrc file:

docker() {
 if [[ $@ == "stats" ]]; then
  command docker stats --format "table {{.Name}}\t{{.Container}}\t{{.CPUPerc}}\t{{.MemPerc}}\t{{.NetIO}}\t{{.BlockIO}}"
 else
  command docker "$@"
 fi
}

You can combine these functions as well, so if you want better stats and ps output you can do the following:

docker() {
 if [[ $@ == "ps" ]]; then
  command docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Names}}"
 elif [[ $@ == "stats" ]]; then
  command docker stats --format "table {{.Name}}\t{{.Container}}\t{{.CPUPerc}}\t{{.MemPerc}}\t{{.NetIO}}\t{{.BlockIO}}"
 else
  command docker "$@"
 fi
} 

Tada, easy!