Skip to content

Instantly share code, notes, and snippets.

@vkoori
Last active April 7, 2024 07:07
Show Gist options
  • Save vkoori/fd0b4bd40aaa01a3c4209c5c1f1095b2 to your computer and use it in GitHub Desktop.
Save vkoori/fd0b4bd40aaa01a3c4209c5c1f1095b2 to your computer and use it in GitHub Desktop.
Get docker containers with minimal info
#!/bin/bash
flag=$1
if [ -z "$flag" ]
then
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}" | (read -r; printf "%s\n" "$REPLY"; sort -k 3)
exit
fi
case $flag in
"-a"|"--all")
docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Status}}" | (read -r; printf "%s\n" "$REPLY"; sort -k 3)
exit
;;
"-s"|"--stop")
running_containers=$(docker ps --format "{{.Names}}")
if [ -z "$running_containers" ]; then
echo "There are no running containers to stop"
else
docker stop $running_containers
echo "Stopped the running containers"
fi
exit
;;
"-h"|"--help")
printf "%-10s | %s\n" "Flag" "Description"
printf "%s\n" "-----------|----------------------"
printf "%-10s | %s\n" "-a, --all" "Display all running and stopped docker containers."
printf "%-10s | %s\n" "-s, --stop" "Stop all running docker containers."
printf "%-10s | %s\n" "-h, --help" "Show this help message."
exit
;;
*)
echo "Invalid flag specified. Please use -h or --help for help."
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment