1+ #! /bin/bash
2+ set -euo pipefail
3+
4+ # This script is a wrapper for 'podman compose down' or 'docker compose down'
5+ # It removes the containers/services defined in the docker-compose file using 'podman rm'
6+ # This is needed when running with kubedock, as 'compose down' cannot shut down pods directly
7+
8+ usage () {
9+ echo " Usage: $0 [OPTIONS] [SERVICES]"
10+ echo " OPTIONS: Options for docker-compose down (e.g., -f docker-compose.yml)"
11+ echo " SERVICES: Optional list of services to remove"
12+ exit 1
13+ }
14+
15+ # Parse options and service names
16+ COMPOSE_FILE=" docker-compose.yml"
17+ OPTIONS=()
18+ SERVICES=()
19+ while [[ $# -gt 0 ]]; do
20+ case " $1 " in
21+ -f|--file)
22+ if [[ -n " ${2:- } " ]]; then
23+ COMPOSE_FILE=" $2 "
24+ OPTIONS+=(" $1 " " $2 " )
25+ shift 2
26+ else
27+ echo " Error: Missing argument for $1 "
28+ usage
29+ fi
30+ ;;
31+ --file=* )
32+ COMPOSE_FILE=" ${1#* =} "
33+ OPTIONS+=(" $1 " )
34+ shift
35+ ;;
36+ -h|--help)
37+ usage
38+ ;;
39+ -* )
40+ OPTIONS+=(" $1 " )
41+ shift
42+ ;;
43+ * )
44+ SERVICES+=(" $1 " )
45+ shift
46+ ;;
47+ esac
48+ done
49+
50+ # If no -f/--file was provided, support both docker-compose.yml and docker-compose.yaml
51+ if [[ " ${COMPOSE_FILE} " == " docker-compose.yml" ]]; then
52+ if [[ -f " docker-compose.yml" ]]; then
53+ COMPOSE_FILE=" docker-compose.yml"
54+ elif [[ -f " docker-compose.yaml" ]]; then
55+ COMPOSE_FILE=" docker-compose.yaml"
56+ else
57+ echo " Error: No docker-compose.yml or docker-compose.yaml found in the current directory."
58+ exit 1
59+ fi
60+ fi
61+
62+ if [[ ! -f " $COMPOSE_FILE " ]]; then
63+ echo " Error: Compose file '$COMPOSE_FILE ' not found."
64+ exit 1
65+ fi
66+
67+ # Get service names from compose file if none specified
68+ if [[ ${# SERVICES[@]} -eq 0 ]]; then
69+ # Try to use yq if available, else fallback to grep/sed/awk
70+ if command -v yq > /dev/null 2>&1 ; then
71+ mapfile -t SERVICES < <( yq ' .services | keys | .[]' " $COMPOSE_FILE " )
72+ else
73+ # Fallback: extract service names by finding top-level keys under 'services:'
74+ mapfile -t SERVICES < <( awk ' /services:/ {flag=1; next} /^[^[:space:]]/ {flag=0} flag && /^[[:space:]]+[a-zA-Z0-9_-]+:/ {gsub(":",""); print $1}' " $COMPOSE_FILE " | sed ' s/^[[:space:]]*//' )
75+ fi
76+ fi
77+
78+ if [[ ${# SERVICES[@]} -eq 0 ]]; then
79+ echo " No services found in compose file '$COMPOSE_FILE '."
80+ exit 0
81+ fi
82+
83+ # Compose container name: <current-dir>-<service-name>-1
84+ PROJECT_NAME=" $( basename " $PWD " ) "
85+
86+ echo " Removing services: ${SERVICES[*]} "
87+ for svc in " ${SERVICES[@]} " ; do
88+ # Try to get container_name from compose file
89+ CONTAINER_NAME=" "
90+ if command -v yq > /dev/null 2>&1 ; then
91+ CONTAINER_NAME=$( yq " .services.${svc} .container_name // \"\" " " $COMPOSE_FILE " | tr -d ' "' )
92+ fi
93+ if [[ -z " $CONTAINER_NAME " ]]; then
94+ CONTAINER_NAME=" ${PROJECT_NAME} -${svc} -1"
95+ fi
96+ # Check if the container exists
97+ if ! podman ps -a --format ' {{.Names}}' | grep -Fxq " $CONTAINER_NAME " ; then
98+ echo " No container found for service '$svc ' (expected name: $CONTAINER_NAME )"
99+ continue
100+ fi
101+ echo " Removing container: $CONTAINER_NAME "
102+ podman rm -f " $CONTAINER_NAME "
103+ done
104+
105+ echo " All specified services have been removed."
0 commit comments