Skip to content

Instantly share code, notes, and snippets.

@frodopwns
Created September 22, 2015 21:30
Show Gist options
  • Save frodopwns/19e1950ba643843f69be to your computer and use it in GitHub Desktop.
Save frodopwns/19e1950ba643843f69be to your computer and use it in GitHub Desktop.
pretty print docker ps commands
import re
import json
import sys
import os
from subprocess import Popen, PIPE
if __name__ == '__main__':
base_path = os.path.abspath(os.path.dirname(__file__))
args = sys.argv
# call tests
a = Popen("docker ps %s" % " ".join(args[1:]), stdout=PIPE, stderr=PIPE, shell=True)
# wait for them to finish
astdout, astderr = a.communicate()
data = astdout.split("\n")
data = filter(None, data)
header = re.split(r'\s{2,}', data[0])
rows = data[1:]
rows = [re.split(r'\s{2,}', x.strip()) for x in rows]
for row in rows:
print json.dumps(dict(zip(header, row)), indent=4)
@johncoleman83
Copy link

johncoleman83 commented Oct 21, 2018

I've been using this, it can be put into your .bash_profile or .bashrc file that works somewhat like an alias for docker ps. The output has color distinctions as well:

usage: $ docker ps -p

function docker () {
    if [[ "$@" == "ps -p" ]]; then
        command docker ps --all --format "{{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Ports}}\t{{.Status}}" \
            | (echo -e "CONTAINER_ID\tNAMES\tIMAGE\tPORTS\tSTATUS" && cat) \
            | awk '{printf "\033[1;32m%s\t\033[01;38;5;95;38;5;196m\%s\t\033[00m\033[1;34m%s\t\033[01;90m%s %s %s %s %s %s %s\033[00m\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10;}' \
            | column -s$'\t' -t
    else
        command docker "$@"
    fi
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment