Created
September 27, 2025 11:19
-
-
Save donny-son/21c82d555d6cfbb708da9b3094111209 to your computer and use it in GitHub Desktop.
`docker ps` into a markdown table
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
| #!/bin/bash | |
| # Function to create markdown table from docker ps output using JSON format | |
| docker_ps_to_markdown() { | |
| echo "# Docker Containers" | |
| echo "" | |
| # Print header | |
| echo "| Name | Image | Status | Ports | Container ID |" | |
| echo "|------|-------|--------|-------|--------------|" | |
| # Get docker ps output in JSON format and process with proper field handling | |
| docker ps --format "{{.Names}}|{{.Image}}|{{.Status}}|{{.Ports}}|{{.ID}}" | \ | |
| while IFS='|' read -r name image status ports id; do | |
| # Escape pipe characters in ports field (common in port mappings) | |
| ports=$(echo "$ports" | sed 's/|/\\|/g') | |
| echo "| $name | $image | $status | $ports | $id |" | |
| done | |
| } | |
| # Full version with more fields | |
| docker_ps_to_markdown_full() { | |
| echo "# Docker Containers Status (Detailed)" | |
| echo "" | |
| # Print header | |
| echo "| Name | Image | Command | Created | Status | Ports | Container ID |" | |
| echo "|------|-------|---------|---------|--------|-------|--------------|" | |
| # Process each container | |
| docker ps --format "{{.Names}}§{{.Image}}§{{.Command}}§{{.CreatedAt}}§{{.Status}}§{{.Ports}}§{{.ID}}" | \ | |
| while IFS='§' read -r name image command created status ports id; do | |
| # Escape pipe characters and clean up quotes in command | |
| command=$(echo "$command" | sed 's/|/\\|/g' | sed 's/"//g') | |
| ports=$(echo "$ports" | sed 's/|/\\|/g') | |
| echo "| $name | $image | $command | $created | $status | $ports | $id |" | |
| done | |
| } | |
| # Compact version with essential fields | |
| docker_ps_compact() { | |
| echo "# Docker Containers (Compact)" | |
| echo "" | |
| echo "| Name | Image | Status | Container ID |" | |
| echo "|------|-------|--------|--------------|" | |
| docker ps --format "{{.Names}}|{{.Image}}|{{.Status}}|{{.ID}}" | \ | |
| while IFS='|' read -r name image status id; do | |
| echo "| $name | $image | $status | $id |" | |
| done | |
| } | |
| # JSON-based version for maximum reliability | |
| docker_ps_json() { | |
| echo "# Docker Containers (JSON-parsed)" | |
| echo "" | |
| # Check if jq is installed | |
| if ! command -v jq &> /dev/null; then | |
| echo "Note: jq is not installed. Falling back to basic version." | |
| docker_ps_to_markdown | |
| return | |
| fi | |
| echo "| Name | Image | Status | Ports | Container ID |" | |
| echo "|------|-------|--------|-------|--------------|" | |
| # Parse JSON output with jq | |
| docker ps --format json | jq -r ' | |
| "\(.Names) | \(.Image) | \(.Status) | \(.Ports // "none") | \(.ID)" | |
| ' | while IFS='|' read -r name image status ports id; do | |
| # Trim whitespace and escape pipes in ports | |
| name=$(echo "$name" | xargs) | |
| image=$(echo "$image" | xargs) | |
| status=$(echo "$status" | xargs) | |
| ports=$(echo "$ports" | xargs | sed 's/|/\\|/g') | |
| id=$(echo "$id" | xargs) | |
| echo "| $name | $image | $status | $ports | $id |" | |
| done | |
| } | |
| # Function to include stopped containers | |
| docker_ps_all() { | |
| echo "# All Docker Containers (Including Stopped)" | |
| echo "" | |
| echo "| Name | Image | Status | Ports | Container ID |" | |
| echo "|------|-------|--------|-------|--------------|" | |
| docker ps -a --format "{{.Names}}|{{.Image}}|{{.Status}}|{{.Ports}}|{{.ID}}" | \ | |
| while IFS='|' read -r name image status ports id; do | |
| # Escape pipe characters in ports field | |
| ports=$(echo "$ports" | sed 's/|/\\|/g') | |
| # Add color coding for stopped containers (as comment) | |
| if [[ "$status" == *"Exited"* ]]; then | |
| echo "| $name | $image | ⚠️ $status | $ports | $id |" | |
| else | |
| echo "| $name | $image | ✅ $status | $ports | $id |" | |
| fi | |
| done | |
| } | |
| # Export to file function | |
| docker_ps_export() { | |
| local output_file="${1:-docker_containers.md}" | |
| echo "Exporting to $output_file..." | |
| docker_ps_to_markdown_full > "$output_file" | |
| echo "Export complete! Check $output_file" | |
| } | |
| # Display menu | |
| show_menu() { | |
| echo "Docker PS to Markdown Converter" | |
| echo "================================" | |
| echo "1. Basic table (Name, Image, Status, Ports, ID)" | |
| echo "2. Full table (All fields)" | |
| echo "3. Compact table (Essential fields only)" | |
| echo "4. JSON-parsed table (requires jq)" | |
| echo "5. All containers (including stopped)" | |
| echo "6. Export to file (docker_containers.md)" | |
| echo "" | |
| echo -n "Choose an option (1-6) [default: 1]: " | |
| } | |
| # Main execution | |
| if [[ "$1" == "--help" || "$1" == "-h" ]]; then | |
| echo "Usage: $0 [option]" | |
| echo "Options:" | |
| echo " 1 - Basic markdown table" | |
| echo " 2 - Full markdown table with all fields" | |
| echo " 3 - Compact table" | |
| echo " 4 - JSON-parsed table (requires jq)" | |
| echo " 5 - All containers including stopped" | |
| echo " 6 - Export to file" | |
| echo " --no-menu - Run option 1 without showing menu" | |
| exit 0 | |
| fi | |
| # If --no-menu flag is passed, just run the basic version | |
| if [[ "$1" == "--no-menu" ]]; then | |
| docker_ps_to_markdown | |
| exit 0 | |
| fi | |
| # Show menu if no argument or interactive mode | |
| if [[ -z "$1" ]]; then | |
| show_menu | |
| read -r choice | |
| else | |
| choice="$1" | |
| fi | |
| # Execute based on choice | |
| case ${choice:-1} in | |
| 1) docker_ps_to_markdown ;; | |
| 2) docker_ps_to_markdown_full ;; | |
| 3) docker_ps_compact ;; | |
| 4) docker_ps_json ;; | |
| 5) docker_ps_all ;; | |
| 6) docker_ps_export ;; | |
| *) | |
| echo "Invalid option. Using default (Basic table)." | |
| docker_ps_to_markdown | |
| ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment