#!/bin/bash # # DSS - Docker Discovery # Container status, images, networks, volumes # set -e PROJECT_PATH="${1:-.}" # Check if Docker is available if ! command -v docker &> /dev/null; then cat < /dev/null; then cat </dev/null) echo "${containers[@]}" } # Get images get_images() { local images=() while IFS= read -r line; do if [[ -n "$line" ]]; then local repo=$(echo "$line" | cut -d'|' -f1) local tag=$(echo "$line" | cut -d'|' -f2) local size=$(echo "$line" | cut -d'|' -f3) images+=("{\"repository\":\"$repo\",\"tag\":\"$tag\",\"size\":\"$size\"}") fi done < <(docker images --format '{{.Repository}}|{{.Tag}}|{{.Size}}' 2>/dev/null | head -20) echo "${images[@]}" } # Check for docker-compose files get_compose_info() { local compose_files=() for file in "docker-compose.yml" "docker-compose.yaml" "compose.yml" "compose.yaml"; do if [[ -f "$PROJECT_PATH/$file" ]]; then local services=$(grep -E "^ [a-zA-Z]" "$PROJECT_PATH/$file" 2>/dev/null | sed 's/://g' | tr -d ' ' | head -10) compose_files+=("{\"file\":\"$file\",\"services\":$(echo "$services" | jq -R -s 'split("\n") | map(select(. != ""))')}") fi done echo "${compose_files[@]}" } # Get resource usage get_stats() { local stats=$(docker stats --no-stream --format '{"name":"{{.Name}}","cpu":"{{.CPUPerc}}","memory":"{{.MemUsage}}"}' 2>/dev/null | head -10 | tr '\n' ',' | sed 's/,$//') echo "[$stats]" } # Build output containers=$(get_containers) images=$(get_images) compose=$(get_compose_info) stats=$(get_stats) containers_json=$(IFS=,; echo "${containers[*]}") images_json=$(IFS=,; echo "${images[*]}") compose_json=$(IFS=,; echo "${compose[*]}") cat </dev/null | wc -l), "total": $(docker ps -aq 2>/dev/null | wc -l), "list": [${containers_json:-}] }, "images": { "total": $(docker images -q 2>/dev/null | wc -l), "list": [${images_json:-}] }, "compose_files": [${compose_json:-}], "resource_usage": ${stats:-[]} } EOF