Skip to content

Instantly share code, notes, and snippets.

@michabbb
Created November 8, 2023 00:13
Show Gist options
  • Save michabbb/7bcfdaf6cc4171b173aa3b4ca1f2adbd to your computer and use it in GitHub Desktop.
Save michabbb/7bcfdaf6cc4171b173aa3b4ca1f2adbd 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
@michabbb
Copy link
Author

michabbb commented Nov 8, 2023

@michabbb
Copy link
Author

michabbb commented Nov 8, 2023

image

if you are using docker-compose the first part of the container name shows the folder/project name, in case you want to see that you can use this little modified version:

#!/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}PROJECT\t\t\tIMAGE\t\t\t\tSTATUS${NC}"
echo -e "${GREEN}=======================================================================

# Execute docker ps and extract the desired information
docker ps --no-trunc --format "{{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}" | while
  # Extract the project name from the container name by taking the first word before a h
  project=$(echo $name | cut -d'-' -f1)

  # Calculate the width of the IMAGE column to create proper spacing for STATUS
  image_width=30
  image_padded=$(printf "%-${image_width}.${image_width}s" "$image")

  # First line: Project, Image, and Status in yellow
  echo -e "${YELLOW}${project}\t\t${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}--------------------------------------------------------------------
done

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