Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

feat: implement OpenRAG image management and cleanup functionality

Rico Furtadod95869c2de2c
diff --git a/Makefile b/Makefile
index 20d611226..122a020d6 100644
--- a/Makefile
+++ b/Makefile
@@ -19,6 +19,7 @@ REPO ?= https://github.com/langflow-ai/langflow.git
# Auto-detect container runtime: prefer docker, fall back to podman
CONTAINER_RUNTIME := $(shell command -v docker >/dev/null 2>&1 && echo "docker" || echo "podman")
+OPENRAG_IMAGE_REPOS := langflowai/openrag-backend langflowai/openrag-frontend langflowai/openrag-langflow langflowai/openrag-opensearch langflowai/openrag-dashboards langflow/langflow opensearchproject/opensearch opensearchproject/opensearch-dashboards
# Only pass --env-file if the file actually exists
ifneq (,$(wildcard $(ENV_FILE)))
COMPOSE_CMD := $(CONTAINER_RUNTIME) compose --env-file $(ENV_FILE)
@@ -74,6 +75,7 @@ endef
######################
.PHONY: help check_tools help_docker help_dev help_test help_local help_utils \
dev dev-cpu dev-local dev-local-cpu stop clean build logs \
+ remove-openrag-images \
shell-backend shell-frontend install \
test test-unit test-integration test-ci test-ci-local test-sdk test-os-jwt lint \
backend frontend docling docling-stop install-be install-fe build-be build-fe build-os build-lf logs-be logs-fe logs-lf logs-os \
@@ -447,10 +449,24 @@ stop: ## Stop and remove all OpenRAG containers
restart: stop dev ## Restart all containers
+remove-openrag-images: ## Remove OpenRAG images only (safe for other projects)
+ @echo "$(YELLOW)Removing OpenRAG images only...$(NC)"
+ @removed=0; total=0; \
+ for repo in $(OPENRAG_IMAGE_REPOS); do \
+ ids=$$($(CONTAINER_RUNTIME) images "$$repo" -q 2>/dev/null | sort -u); \
+ for id in $$ids; do \
+ total=$$((total+1)); \
+ if $(CONTAINER_RUNTIME) rmi -f "$$id" >/dev/null 2>&1; then \
+ removed=$$((removed+1)); \
+ fi; \
+ done; \
+ done; \
+ echo "$(PURPLE)Removed $$removed/$$total OpenRAG image(s).$(NC)"
+
clean: stop ## Stop containers and remove volumes
@echo "$(YELLOW)Cleaning up containers and volumes...$(NC)"
$(COMPOSE_CMD) down -v --remove-orphans
- $(CONTAINER_RUNTIME) system prune -f
+ @$(MAKE) remove-openrag-images
@echo "$(PURPLE)Cleanup complete!$(NC)"
factory-reset: ## Complete reset (stop, remove volumes, clear data, remove images)
@@ -461,7 +477,7 @@ factory-reset: ## Complete reset (stop, remove volumes, clear data, remove image
echo " - Delete opensearch-data directory"; \
echo " - Delete config directory"; \
echo " - Delete JWT keys (private_key.pem, public_key.pem)"; \
- echo " - Remove local OpenRAG images"; \
+ echo " - Remove OpenRAG images"; \
echo ""; \
echo ""; \
if [ "$(FORCE)" != "true" ]; then \
@@ -492,8 +508,8 @@ factory-reset: ## Complete reset (stop, remove volumes, clear data, remove image
rm -f keys/private_key.pem keys/public_key.pem; \
echo "$(PURPLE)JWT keys removed$(NC)"; \
fi; \
- echo "$(YELLOW)Cleaning up system...$(NC)"; \
- $(CONTAINER_RUNTIME) system prune -f; \
+ echo "$(YELLOW)Removing OpenRAG images...$(NC)"; \
+ $(MAKE) remove-openrag-images; \
echo ""; \
echo "$(PURPLE)Factory reset complete!$(NC)"; \
echo "$(CYAN)Run 'make dev' or 'make dev-cpu' to start fresh.$(NC)";
diff --git a/src/tui/managers/container_manager.py b/src/tui/managers/container_manager.py
index 89c0c121d..91fccceac 100644
--- a/src/tui/managers/container_manager.py
+++ b/src/tui/managers/container_manager.py
@@ -74,6 +74,17 @@ def format_port_conflict_message(conflicts: List[tuple[str, int, str]], max_show
class ContainerManager:
"""Manages Docker/Podman container lifecycle for OpenRAG."""
+ OPENRAG_IMAGE_REPOS = {
+ "langflowai/openrag-backend",
+ "langflowai/openrag-frontend",
+ "langflowai/openrag-langflow",
+ "langflowai/openrag-opensearch",
+ "langflowai/openrag-dashboards",
+ "langflow/langflow",
+ "opensearchproject/opensearch",
+ "opensearchproject/opensearch-dashboards",
+ }
+
def __init__(self, compose_file: Optional[Path] = None):
self.platform_detector = PlatformDetector()
self.runtime_info = self.platform_detector.detect_runtime()
@@ -106,6 +117,16 @@ class ContainerManager:
"langflow": "langflow",
}
+ @staticmethod
+ def _extract_repository(image_tag: str) -> str:
+ """Extract repository name from <repository>:<tag> image reference."""
+ return image_tag.rsplit(":", 1)[0] if ":" in image_tag else image_tag
+
+ def _is_openrag_repository(self, repository: str) -> bool:
+ """Check whether repository is OpenRAG-related, with optional registry prefix."""
+ repo = repository.lower()
+ return any(repo == known or repo.endswith(f"/{known}") for known in self.OPENRAG_IMAGE_REPOS)
+
def _find_compose_file(self, filename: str) -> Path:
"""Find compose file in centralized TUI directory, current directory, or package resources."""
from utils.paths import get_tui_compose_file
@@ -556,6 +577,51 @@ class ContainerManager:
except Exception as e:
return False, "", f"Command execution failed: {e}"
+ async def _list_openrag_images(
+ self, include_created: bool = False
+ ) -> tuple[bool, List[Dict[str, str]], str]:
+ """List OpenRAG-related images available in the container runtime."""
+ format_parts = ["{{.Repository}}:{{.Tag}}", "{{.ID}}"]
+ if include_created:
+ format_parts.append("{{.CreatedAt}}")
+
+ success, stdout, stderr = await self._run_runtime_command(
+ ["images", "--format", "\t".join(format_parts)]
+ )
+ if not success:
+ return False, [], stderr
+
+ images: List[Dict[str, str]] = []
+ for raw_line in stdout.strip().splitlines():
+ if not raw_line.strip():
+ continue
+
+ parts = raw_line.split("\t")
+ if len(parts) < 2:
+ continue
+
+ image_tag = parts[0].strip()
+ image_id = parts[1].strip()
+
+ # Skip untagged entries to avoid broad cleanup touching unrelated images.
+ if "<none>" in image_tag:
+ continue
+
+ repository = self._extract_repository(image_tag)
+ if not self._is_openrag_repository(repository):
+ continue
+
+ image_data = {
+ "full_tag": image_tag,
+ "repo": repository,
+ "id": image_id,
+ }
+ if include_created and len(parts) >= 3:
+ image_data["created"] = parts[2].strip()
+ images.append(image_data)
+
+ return True, images, ""
+
def _process_service_json(
self, service: Dict, services: Dict[str, ServiceInfo]
) -> None:
@@ -1243,15 +1309,37 @@ class ContainerManager:
yield False, f"Failed to stop services: {stderr}"
return
- yield False, "Cleaning up container data..."
+ yield False, "Removing OpenRAG images..."
+ success, images, stderr = await self._list_openrag_images()
+ if not success:
+ yield False, f"Failed to list OpenRAG images: {stderr}"
+ return
+
+ if not images:
+ yield True, "System reset completed - OpenRAG containers and volumes removed"
+ return
- # Additional cleanup - remove any remaining containers/volumes
- # This is more thorough than just compose down
- await self._run_runtime_command(["system", "prune", "-f"])
+ # Deduplicate by image ID (same ID can have multiple tags)
+ image_ids = []
+ seen = set()
+ for image in images:
+ image_id = image["id"]
+ if image_id in seen:
+ continue
+ seen.add(image_id)
+ image_ids.append((image_id, image["full_tag"]))
+
+ removed = 0
+ for image_id, image_tag in image_ids:
+ success, _, stderr = await self._run_runtime_command(["rmi", image_id])
+ if success:
+ removed += 1
+ else:
+ yield False, f"Could not remove {image_tag}: {stderr.strip()}"
yield (
True,
- "System reset completed - all containers, volumes, and local images removed",
+ f"System reset completed - removed {removed} OpenRAG image(s)",
)
async def get_service_logs(
@@ -1391,7 +1479,6 @@ class ContainerManager:
2. Identifies OpenRAG-related images (openrag-backend, openrag-frontend, langflow, opensearch, dashboards)
3. For each repository, keeps only the latest/currently used image
4. Removes old images
- 5. Prunes dangling images
Yields:
Tuples of (success, message) for progress updates
@@ -1402,69 +1489,20 @@ class ContainerManager:
yield False, "Scanning for OpenRAG images..."
- # Get list of all images
- success, stdout, stderr = await self._run_runtime_command(
- ["images", "--format", "{{.Repository}}:{{.Tag}}\t{{.ID}}\t{{.CreatedAt}}"]
- )
-
+ success, images, stderr = await self._list_openrag_images(include_created=True)
if not success:
yield False, f"Failed to list images: {stderr}"
return
- # Parse images and group by repository
- openrag_repos = {
- "langflowai/openrag-backend",
- "langflowai/openrag-frontend",
- "langflowai/openrag-langflow",
- "langflowai/openrag-opensearch",
- "langflowai/openrag-dashboards",
- "langflow/langflow", # Also include base langflow images
- "opensearchproject/opensearch",
- "opensearchproject/opensearch-dashboards",
- }
-
images_by_repo = {}
- for line in stdout.strip().split("\n"):
- if not line.strip():
- continue
-
- parts = line.split("\t")
- if len(parts) < 3:
- continue
-
- image_tag, image_id, created_at = parts[0], parts[1], parts[2]
-
- # Skip <none> tags (dangling images will be handled separately)
- if "<none>" in image_tag:
- continue
-
- # Extract repository name (without tag)
- if ":" in image_tag:
- repo = image_tag.rsplit(":", 1)[0]
- else:
- repo = image_tag
-
- # Check if this is an OpenRAG-related image
- if any(openrag_repo in repo for openrag_repo in openrag_repos):
- if repo not in images_by_repo:
- images_by_repo[repo] = []
- images_by_repo[repo].append({
- "full_tag": image_tag,
- "id": image_id,
- "created": created_at,
- })
+ for image in images:
+ repo = image["repo"]
+ if repo not in images_by_repo:
+ images_by_repo[repo] = []
+ images_by_repo[repo].append(image)
if not images_by_repo:
yield True, "No OpenRAG images found to prune"
- # Still run dangling image cleanup
- yield False, "Cleaning up dangling images..."
- success, stdout, stderr = await self._run_runtime_command(
- ["image", "prune", "-f"]
- )
- if success:
- yield True, "Dangling images cleaned up"
- else:
- yield False, f"Failed to prune dangling images: {stderr}"
return
# Get currently used images (from running/stopped containers)
@@ -1522,21 +1560,6 @@ class ContainerManager:
else:
yield True, "No old images were removed"
- # Clean up dangling images (untagged images)
- yield False, "Cleaning up dangling images..."
- success, stdout, stderr = await self._run_runtime_command(
- ["image", "prune", "-f"]
- )
-
- if success:
- # Parse output to see if anything was removed
- if stdout.strip():
- yield True, f"Dangling images cleaned: {stdout.strip()}"
- else:
- yield True, "No dangling images to clean"
- else:
- yield False, f"Failed to prune dangling images: {stderr}"
-
yield True, "Image pruning completed"
async def prune_all_images(self) -> AsyncIterator[tuple[bool, str]]:
@@ -1545,7 +1568,6 @@ class ContainerManager:
This is a more aggressive pruning that:
1. Stops all running services
2. Removes ALL OpenRAG-related images (not just old versions)
- 3. Prunes dangling images
This frees up maximum disk space but requires re-downloading images on next start.
@@ -1570,55 +1592,11 @@ class ContainerManager:
yield False, "Scanning for OpenRAG images..."
- # Get list of all images
- success, stdout, stderr = await self._run_runtime_command(
- ["images", "--format", "{{.Repository}}:{{.Tag}}\t{{.ID}}"]
- )
-
+ success, images_to_remove, stderr = await self._list_openrag_images()
if not success:
yield False, f"Failed to list images: {stderr}"
return
- # Parse images and identify ALL OpenRAG-related images
- openrag_repos = {
- "langflowai/openrag-backend",
- "langflowai/openrag-frontend",
- "langflowai/openrag-langflow",
- "langflowai/openrag-opensearch",
- "langflowai/openrag-dashboards",
- "langflow/langflow",
- "opensearchproject/opensearch",
- "opensearchproject/opensearch-dashboards",
- }
-
- images_to_remove = []
- for line in stdout.strip().split("\n"):
- if not line.strip():
- continue
-
- parts = line.split("\t")
- if len(parts) < 2:
- continue
-
- image_tag, image_id = parts[0], parts[1]
-
- # Skip <none> tags (will be handled by prune)
- if "<none>" in image_tag:
- continue
-
- # Extract repository name (without tag)
- if ":" in image_tag:
- repo = image_tag.rsplit(":", 1)[0]
- else:
- repo = image_tag
-
- # Check if this is an OpenRAG-related image
- if any(openrag_repo in repo for openrag_repo in openrag_repos):
- images_to_remove.append({
- "full_tag": image_tag,
- "id": image_id,
- })
-
if not images_to_remove:
yield True, "No OpenRAG images found to remove"
else:
@@ -1642,19 +1620,4 @@ class ContainerManager:
else:
yield False, "No images were removed"
- # Clean up dangling images
- yield False, "Cleaning up dangling images..."
- success, stdout, stderr = await self._run_runtime_command(
- ["image", "prune", "-f"]
- )
-
- if success:
- if stdout.strip():
- yield True, f"Dangling images cleaned: {stdout.strip()}"
- else:
- yield True, "No dangling images to clean"
- else:
- yield False, f"Failed to prune dangling images: {stderr}"
-
yield True, "All OpenRAG images removed successfully"
-
diff --git a/src/tui/utils/startup_checks.py b/src/tui/utils/startup_checks.py
index 24aeb7da3..a7b3143f9 100644
--- a/src/tui/utils/startup_checks.py
+++ b/src/tui/utils/startup_checks.py
@@ -13,6 +13,16 @@ from typing import Tuple, Optional
from pathlib import Path
MIN_PODMAN_MEMORY_MB = 8192 # 8 GB minimum
+OPENRAG_IMAGE_REPOS = {
+ "langflowai/openrag-backend",
+ "langflowai/openrag-frontend",
+ "langflowai/openrag-langflow",
+ "langflowai/openrag-opensearch",
+ "langflowai/openrag-dashboards",
+ "langflow/langflow",
+ "opensearchproject/opensearch",
+ "opensearchproject/opensearch-dashboards",
+}
# =============================================================================
@@ -97,6 +107,63 @@ def docker_is_podman() -> bool:
return False
+def _extract_repository(image_tag: str) -> str:
+ """Extract repository name from <repository>:<tag> image reference."""
+ return image_tag.rsplit(":", 1)[0] if ":" in image_tag else image_tag
+
+
+def _is_openrag_repository(repository: str) -> bool:
+ """Check whether repository is OpenRAG-related, with optional registry prefix."""
+ repo = repository.lower()
+ return any(repo == known or repo.endswith(f"/{known}") for known in OPENRAG_IMAGE_REPOS)
+
+
+def remove_openrag_images(runtime: str) -> tuple[int, int]:
+ """Remove only OpenRAG-related images for the given runtime."""
+ result = subprocess.run(
+ [runtime, "images", "--format", "{{.Repository}}:{{.Tag}}\t{{.ID}}"],
+ capture_output=True,
+ text=True,
+ )
+ if result.returncode != 0:
+ return 0, 0
+
+ image_ids: list[tuple[str, str]] = []
+ seen_ids = set()
+ for raw_line in result.stdout.splitlines():
+ if not raw_line.strip():
+ continue
+ parts = raw_line.split("\t")
+ if len(parts) < 2:
+ continue
+
+ image_tag = parts[0].strip()
+ image_id = parts[1].strip()
+ if "<none>" in image_tag:
+ continue
+
+ repository = _extract_repository(image_tag)
+ if not _is_openrag_repository(repository):
+ continue
+
+ if image_id in seen_ids:
+ continue
+ seen_ids.add(image_id)
+ image_ids.append((image_id, image_tag))
+
+ removed = 0
+ for image_id, _ in image_ids:
+ delete_result = subprocess.run(
+ [runtime, "rmi", "-f", image_id],
+ capture_output=True,
+ text=True,
+ )
+ if delete_result.returncode == 0:
+ removed += 1
+
+ return removed, len(image_ids)
+
+
# =============================================================================
# Runtime Detection
# =============================================================================
@@ -432,7 +499,11 @@ def check_storage_corruption(runtime: str) -> Tuple[bool, Optional[str]]:
def fix_storage_corruption(runtime: str, version: str) -> bool:
"""Reset storage to fix corruption."""
say("Storage corruption detected.")
- if not ask_yes_no(f"Reset {runtime} storage? (WARNING: deletes all containers/images)"):
+ if runtime == "podman":
+ prompt = f"Reset {runtime} storage? (WARNING: deletes all containers/images)"
+ else:
+ prompt = "Remove OpenRAG Docker images? (OpenRAG images only)"
+ if not ask_yes_no(prompt):
return False
if runtime == "podman":
@@ -460,9 +531,9 @@ def fix_storage_corruption(runtime: str, version: str) -> bool:
say("Done.")
return True
else:
- say("Pruning Docker system...")
- subprocess.run(["docker", "system", "prune", "-af"], capture_output=True)
- say("Done.")
+ say("Removing OpenRAG Docker images...")
+ removed, total = remove_openrag_images("docker")
+ say(f"Removed {removed}/{total} OpenRAG image(s).")
return True
diff --git a/tests/unit/test_container_manager_cleanup.py b/tests/unit/test_container_manager_cleanup.py
new file mode 100644
index 000000000..0ca06097e
--- /dev/null
+++ b/tests/unit/test_container_manager_cleanup.py
@@ -0,0 +1,85 @@
+"""Tests for OpenRAG-only image cleanup behavior in ContainerManager."""
+
+from unittest.mock import AsyncMock
+
+import pytest
+
+from src.tui.managers.container_manager import ContainerManager
+
+
+async def _collect(async_iterable):
+ """Collect all items from an async iterator into a list."""
+ return [item async for item in async_iterable]
+
+
+def _make_manager() -> ContainerManager:
+ """Create a minimal ContainerManager instance for unit tests."""
+ manager = ContainerManager.__new__(ContainerManager)
+ return manager
+
+
+async def test_list_openrag_images_filters_non_openrag_and_dangling():
+ manager = _make_manager()
+ manager._run_runtime_command = AsyncMock(
+ return_value=(
+ True,
+ (
+ "langflowai/openrag-backend:latest\timg-openrag-1\n"
+ "docker.io/langflowai/openrag-frontend:v1\timg-openrag-2\n"
+ "library/ubuntu:latest\timg-ubuntu\n"
+ "<none>:<none>\timg-dangling\n"
+ ),
+ "",
+ )
+ )
+
+ success, images, error = await manager._list_openrag_images()
+
+ assert success is True
+ assert error == ""
+ assert [img["id"] for img in images] == ["img-openrag-1", "img-openrag-2"]
+ assert all("openrag" in img["full_tag"] for img in images)
+
+
+async def test_reset_services_removes_only_openrag_images_without_system_prune():
+ manager = _make_manager()
+ manager._run_compose_command = AsyncMock(return_value=(True, "", ""))
+ manager._list_openrag_images = AsyncMock(
+ return_value=(
+ True,
+ [
+ {"full_tag": "langflowai/openrag-backend:latest", "id": "img1"},
+ {"full_tag": "langflowai/openrag-frontend:latest", "id": "img2"},
+ ],
+ "",
+ )
+ )
+ manager._run_runtime_command = AsyncMock(return_value=(True, "", ""))
+
+ updates = await _collect(manager.reset_services())
+
+ assert updates[-1] == (
+ True,
+ "System reset completed - removed 2 OpenRAG image(s)",
+ )
+ runtime_calls = [call.args[0] for call in manager._run_runtime_command.call_args_list]
+ assert runtime_calls == [["rmi", "img1"], ["rmi", "img2"]]
+ assert all(call[:2] != ["system", "prune"] for call in runtime_calls)
+
+
+async def test_reset_services_handles_no_openrag_images():
+ manager = _make_manager()
+ manager._run_compose_command = AsyncMock(return_value=(True, "", ""))
+ manager._list_openrag_images = AsyncMock(return_value=(True, [], ""))
+ manager._run_runtime_command = AsyncMock()
+
+ updates = await _collect(manager.reset_services())
+
+ assert updates[-1] == (
+ True,
+ "System reset completed - OpenRAG containers and volumes removed",
+ )
+ manager._run_runtime_command.assert_not_called()
diff --git a/tests/unit/test_startup_checks_cleanup.py b/tests/unit/test_startup_checks_cleanup.py
new file mode 100644
index 000000000..327b76d1f
--- /dev/null
+++ b/tests/unit/test_startup_checks_cleanup.py
@@ -0,0 +1,50 @@
+"""Tests for OpenRAG-only image cleanup behavior in startup checks."""
+
+from unittest.mock import MagicMock, patch
+
+from src.tui.utils import startup_checks
+
+
+def _run_result(returncode: int = 0, stdout: str = "", stderr: str = "") -> MagicMock:
+ """Create a subprocess.run-like result mock."""
+ result = MagicMock()
+ result.returncode = returncode
+ result.stdout = stdout
+ result.stderr = stderr
+ return result
+
+
+def test_remove_openrag_images_filters_to_openrag_repos():
+ with patch("src.tui.utils.startup_checks.subprocess.run") as mock_run:
+ mock_run.side_effect = [
+ _run_result(
+ stdout=(
+ "langflowai/openrag-backend:latest\timg-openrag-1\n"
+ "docker.io/langflowai/openrag-frontend:v1\timg-openrag-2\n"
+ "library/ubuntu:latest\timg-ubuntu\n"
+ "<none>:<none>\timg-dangling\n"
+ )
+ ),
+ _run_result(returncode=0),
+ _run_result(returncode=0),
+ ]
+
+ removed, total = startup_checks.remove_openrag_images("docker")
+
+ assert (removed, total) == (2, 2)
+ calls = [call.args[0] for call in mock_run.call_args_list]
+ assert calls[0] == ["docker", "images", "--format", "{{.Repository}}:{{.Tag}}\t{{.ID}}"]
+ assert ["docker", "rmi", "-f", "img-openrag-1"] in calls
+ assert ["docker", "rmi", "-f", "img-openrag-2"] in calls
+ assert all("img-ubuntu" not in call for call in calls)
+
+
+def test_fix_storage_corruption_docker_avoids_system_prune():
+ with patch("src.tui.utils.startup_checks.ask_yes_no", return_value=True), patch(
+ "src.tui.utils.startup_checks.remove_openrag_images", return_value=(1, 1)
+ ) as mock_remove, patch("src.tui.utils.startup_checks.subprocess.run") as mock_run:
+ ok = startup_checks.fix_storage_corruption(runtime="docker", version="26.1.0")
+
+ assert ok is True
+ mock_remove.assert_called_once_with("docker")
+ mock_run.assert_not_called()