Last active
April 7, 2024 07:07
-
-
Save vkoori/fd0b4bd40aaa01a3c4209c5c1f1095b2 to your computer and use it in GitHub Desktop.
Get docker containers with minimal info
This file contains 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 | |
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