Last active
January 23, 2026 13:22
-
-
Save MaaxGr/0d6fc6c1fc1b7a345802f9c33b55df27 to your computer and use it in GitHub Desktop.
Python Wrapper Script for "docker ps"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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() | |
| output.reverse() | |
| if len(output) == 0: | |
| print("No running docker containers ๐") | |
| else: | |
| 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("") |
Author
Good idea. Thanks for your contribution @KrappRamiro :)
I updated the gist.
@MaaxGr Thank YOU! I really love this script, its beautiful!
Awesome script!
I've made the following changes to show at a glanced if each container is running or not:
# line 20, added `-a` to the docker ps command
# (optional: also added my local timezone for convinance since my server is in UTC)
+ command = "TZ='Europe/Paris' docker ps -a --format '{\"ID\":\"{{ .ID }}\", \"Image\": \"{{ .Image }}\", \"Names\":\"{{ .Names }}\", \"Status\":\"{{ .Status }}\", \"CreatedAt\":\"{{ .CreatedAt }}\", \"Ports\":\"{{ .Ports }}\"}'"
# line 36, added a way to crudely set color if `Up` or `Exited` is present in the string
+ status = entry_json_object["Status"].replace("Up", f"{bcolors.OKGREEN}Up{bcolors.OKDARKGRAY}").replace("Exited", f"{bcolors.FAIL}Exited{bcolors.OKDARKGRAY}")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Hi! I think i have an improvement for when no docker containers are running.
Adding this
ifbefore thefor entry in output:tells the user if no docker containers are runningBefore
After