Skip to content

Instantly share code, notes, and snippets.

@tanandy
Forked from michabbb/docker-ps-small.sh
Created August 1, 2024 14:07
Show Gist options
  • Save tanandy/136b79be6a16a63a11c897f43aa0fa4b to your computer and use it in GitHub Desktop.
Save tanandy/136b79be6a16a63a11c897f43aa0fa4b to your computer and use it in GitHub Desktop.
an example of how to reformat the output of "docker ps" for smaller screens
#!/bin/bash
# Color codes
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Header in green
echo -e "${GREEN}IMAGE\t\t\t\t\t\t\tSTATUS${NC}"
echo -e "${GREEN}==================================================================================${NC}"
# Execute docker ps and extract the desired information
docker ps --no-trunc --format "{{.Image}}\t{{.Status}}\t{{.Ports}}" | while IFS=$'\t' read -r image status ports; do
# Calculate the width of the IMAGE column to create proper spacing for STATUS
image_width=50
image_padded=$(printf "%-${image_width}.${image_width}s" "$image")
# First line: Image and Status in yellow
echo -e "${YELLOW}${image_padded}\t${status}${NC}"
# Ports, each on a new line in blue
if [ -n "$ports" ]; then
# Here we remove any spaces after commas
ports=$(echo "$ports" | sed 's/, /,/g')
IFS=',' read -ra ADDR <<< "$ports"
for port in "${ADDR[@]}"; do
# Here we remove any leading whitespace, if present
port=$(echo $port | sed 's/^[[:space:]]*//')
echo -e "${BLUE} ${port}${NC}"
done
fi
# Separator between containers in yellow
echo -e "${YELLOW}----------------------------------------------------------------------------------${NC}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment