From 0c24dd07f8701eda70a9781825f8d11245ff3aa9 Mon Sep 17 00:00:00 2001 From: Yury Kossakovsky Date: Thu, 22 May 2025 13:24:46 -0600 Subject: [PATCH] Add force stop functionality for lingering containers in start_services.py - Introduced a new function, force_stop_lingering_containers, to identify and force stop containers that may be running outside of the current Docker Compose management. - Implemented logic to check for specific container patterns and handle stopping and removing them, enhancing the shutdown process. - Improved error handling to manage potential issues during the force stop operation, ensuring robustness in service management. --- start_services.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/start_services.py b/start_services.py index 86a8746..4fc67a3 100755 --- a/start_services.py +++ b/start_services.py @@ -90,6 +90,51 @@ def stop_existing_containers(): print("Attempting to remove all stopped services...") run_command(rm_cmd, env=env_for_stop_rm) + # Force stop containers that might be running outside of compose management + force_stop_lingering_containers() + +def force_stop_lingering_containers(): + """Force stop and remove specific containers that might be lingering.""" + print("Force stopping and removing any lingering service containers...") + + # Define service container patterns that we want to ensure are stopped + # These are containers that might be running but not managed by the current compose setup + container_patterns = [ + "qdrant", "langfuse", "grafana", "crawl4ai", "letta", "clickhouse", + "node-exporter", "minio", "cadvisor", "open-webui", "ollama", + "prometheus", "searxng", "supabase-", "localai-langfuse-", + "localai-clickhouse-", "localai-minio-", "realtime-dev.supabase-" + ] + + try: + # Get list of all running containers + result = subprocess.run( + ["docker", "ps", "--format", "{{.Names}}"], + capture_output=True, text=True, check=True + ) + running_containers = result.stdout.strip().split('\n') + + containers_to_stop = [] + for container in running_containers: + if container and any(pattern in container for pattern in container_patterns): + containers_to_stop.append(container) + + if containers_to_stop: + print(f"Found lingering containers: {', '.join(containers_to_stop)}") + # Stop containers + subprocess.run(["docker", "stop"] + containers_to_stop, check=True) + print("Stopped lingering containers.") + # Remove containers + subprocess.run(["docker", "rm", "-f"] + containers_to_stop, check=True) + print("Removed lingering containers.") + else: + print("No lingering containers found.") + + except subprocess.CalledProcessError as e: + print(f"Error during force stop: {e}") + except Exception as e: + print(f"Unexpected error during force stop: {e}") + def start_supabase(): """Start the Supabase services (using its compose file).""" if not is_supabase_enabled():