DEV Community

Max
Max

Posted on

4 1

Better "docker ps" for small monitors

The output of "docker ps" often becomes really confusing on small screens (1080p or smaller).

So i created a wrapper python script that solves that problem.

Before

Image description

After

Image description

Script

/root/bin/docker-ps:

#!/usr/bin/python3

import os
import subprocess
import json

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKDARKGRAY = '\033[90m'
    OKCYAN = '\033[96m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'


command = "docker ps --format '{\"ID\":\"{{ .ID }}\", \"Image\": \"{{ .Image }}\", \"Names\":\"{{ .Names }}\", \"Status\":\"{{ .Status }}\", \"CreatedAt\":\"{{ .CreatedAt }}\", \"Ports\":\"{{ .Ports }}\"}'"

output = subprocess.check_output(command, shell=True).splitlines()
print("")

for entry in output:
    entry_json = entry.decode("utf-8")
    entry_json_object = json.loads(entry_json)

    id = entry_json_object["ID"]
    names = entry_json_object["Names"]
    status = entry_json_object["Status"]
    created = entry_json_object["CreatedAt"]
    image = entry_json_object["Image"]
    ports = entry_json_object["Ports"]
    print(f"{id}\t{bcolors.WARNING}{names}{bcolors.ENDC}\t ({bcolors.OKDARKGRAY}Status: {status}, Created At: {created}{bcolors.ENDC})")
    print(f"  {bcolors.BOLD}Image:{bcolors.ENDC} {image}")
    print(f"  {bcolors.BOLD}Ports:{bcolors.ENDC} {ports}")
    print("")

Enter fullscreen mode Exit fullscreen mode

The Script is also available as a GitHub Gist.

In .bashrc i override the "docker ps" command with the script:

function docker() {
  case $* in
    ps ) shift 1; command /root/bin/docker-ps ;;
    * ) command docker "$@" ;;
  esac
}
Enter fullscreen mode Exit fullscreen mode

Alternative Idea

Just display the columns that are important to you:

e.g.:

docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Ports}} "
Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay