Shorten & Alias 'docker ps' output

Problem:

The default 'docker ps' command has a rather verbose output. This (especially for me) is due to the sheer number of ports associated to containers.

As a result, I need a super wide terminal to actually accommodate for this, which I don't like. If you don't have a short terminal it ends up a right mess:

root@docker:/home/david# docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                                                                                                                                           NAMES
e111963440ee        nginx:alpine            "nginx -g 'daemon ..."   2 days ago          Up 2 days           80/tcp                                                                                                                                                          webapps_webserver_1
77dd8191c167        ghost:alpine            "docker-entrypoint..."   2 days ago          Up 2 days           2368/tcp                                                                                                                                                        webapps_ghost_1
5b798c3a43e2        nginx:alpine            "nginx -g 'daemon ..."   2 days ago          Up 2 days           0.0.0.0:**->**/tcp                                                                                                                                              webapps_loadbalancer_1
2f53df4c3cf4        php:fpm-alpine          "docker-php-entryp..."   2 days ago          Up 2 days           ***/tcp                                                                                                                                                        webapps_php-fpm_1
4971f1e4eb52        linuxserver/plexpy      "/init"                  2 days ago          Up 2 days           0.0.0.0:****->****/tcp                                                                                                                                          plexpy
f7ddaab86005        linuxserver/plex        "/init"                  2 days ago          Up 2 days                                                                                                                                                                           plex
5062001e7f1c        linuxserver/syncthing   "/init"                  2 days ago          Up 2 days           0.0.0.0:****->****/tcp, 0.0.0.0:****->****/tcp, 0.0.0.0:****->****/udp                                                                                      syncthing
308bb0afad01        splunk/splunk           "/sbin/entrypoint...."   2 days ago          Up 2 days           0.0.0.0:****->****/tcp, 0.0.0.0:****->****/tcp, 1514/tcp, 8089/tcp, 8191/tcp, 9997/tcp, 0.0.0.0:****->****/udp, 0.0.0.0:****->****/tcp, 0.0.0.0:****->****/udp   splunk
261f8de36c40        f2cd9eb9cab9            "/bin/tini -- /usr..."   2 weeks ago         Up 2 weeks          0.0.0.0:****->****/tcp, 0.0.0.0:****->****/tcp                                                                                                                jenkins
root@docker:/home/david# 

This actually looks better than it normally does....but check out that scroll bar that ghost has put in! If you're in a terminal window, the wrapping is awful.

Solution:

There's been many proposed solutions, however now the new --format parameter has been introduced we can format things rather more nicely:

root@docker:~$ docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Names}}"
CONTAINER ID        IMAGE                   STATUS              NAMES
e111963440ee        nginx:alpine            Up 2 days           webapps_webserver_1
77dd8191c167        ghost:alpine            Up 2 days           webapps_ghost_1
5b798c3a43e2        nginx:alpine            Up 2 days           webapps_loadbalancer_1
2f53df4c3cf4        php:fpm-alpine          Up 2 days           webapps_php-fpm_1
4971f1e4eb52        linuxserver/plexpy      Up 2 days           plexpy
f7ddaab86005        linuxserver/plex        Up 2 days           plex
5062001e7f1c        linuxserver/syncthing   Up 2 days           syncthing
308bb0afad01        splunk/splunk           Up 2 days           splunk
261f8de36c40        f2cd9eb9cab9            Up 2 weeks          jenkins

But we don't want to have to be tying all that junk out every time we just want to see a quick list of containers! So, aliases to the rescue!

...or not, if you want to alias docker ps to the above output, it's best to use a bash function.

I've used the following in my .bashrc file:

docker() {
 if [[ $@ == "ps" ]]; then
  command docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Names}}"
 else
  command docker "$@"
 fi
} 

Essentially you're redefining the docker command and if you have a specific set of parameters to run another command instead. Easy huh!

Reload your session or re-read the .bashrc file and you're good to go:

root@docker:/home/david# source .bashrc
root@docker:/home/david# docker ps
CONTAINER ID        IMAGE                   STATUS              NAMES
e111963440ee        nginx:alpine            Up 2 days           webapps_webserver_1
77dd8191c167        ghost:alpine            Up 2 days           webapps_ghost_1
5b798c3a43e2        nginx:alpine            Up 2 days           webapps_loadbalancer_1
2f53df4c3cf4        php:fpm-alpine          Up 2 days           webapps_php-fpm_1
4971f1e4eb52        linuxserver/plexpy      Up 2 days           plexpy
f7ddaab86005        linuxserver/plex        Up 2 days           plex
5062001e7f1c        linuxserver/syncthing   Up 2 days           syncthing
308bb0afad01        splunk/splunk           Up 2 days           splunk
261f8de36c40        f2cd9eb9cab9            Up 2 weeks          jenkins
root@docker:/home/david# 

You can of course customise this easily by fiddling with the formatting options!