Skip to content

Instantly share code, notes, and snippets.

@tueda
Created August 21, 2024 05:27
Show Gist options
  • Save tueda/2c5f340a18050b6886108b3578e63529 to your computer and use it in GitHub Desktop.
Save tueda/2c5f340a18050b6886108b3578e63529 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -eu
set -o pipefail
show_containers() {
local RED='\033[0;31m'
local GREEN='\033[0;32m'
local YELLOW='\033[1;33m'
local NOCOLOR='\033[0m'
local container_count
container_count=$(docker ps -a -q | wc -l)
if [ "$container_count" -eq 0 ]; then
return
fi
echo
if [ "$container_count" -eq 1 ]; then
echo -e "${YELLOW}There is a running container:${NOCOLOR}"
else
echo -e "${YELLOW}There are running containers:${NOCOLOR}"
fi
local line
docker ps -a | while IFS= read -r line; do
if echo "$line" | grep -q -E '\<Up\>'; then
echo -e "${GREEN}${line}${NOCOLOR}"
elif echo "$line" | grep -q -E '\<Created|Paused\>'; then
echo -e "${YELLOW}${line}${NOCOLOR}"
elif echo "$line" | grep -q -E '\<Exited\>'; then
echo -e "${RED}${line}${NOCOLOR}"
else
echo "${line}"
fi
done
echo
}
if shopt -q login_shell; then
show_containers
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment