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.
This commit is contained in:
Yury Kossakovsky
2025-05-22 13:24:46 -06:00
parent 16e8b9a059
commit 0c24dd07f8

View File

@@ -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():