|
| 1 | +/** |
| 2 | + * @fileoverview Docker environment detection and container management. |
| 3 | + * @module rag/setup/DockerDetector |
| 4 | + * |
| 5 | + * Utility class for detecting whether Docker is available, checking |
| 6 | + * container state, starting/stopping containers, and pulling images. |
| 7 | + * Used by QdrantSetup and PostgresSetup for auto-provisioning. |
| 8 | + */ |
| 9 | + |
| 10 | +import { execSync } from 'node:child_process'; |
| 11 | + |
| 12 | +export class DockerDetector { |
| 13 | + /** |
| 14 | + * Check if Docker is installed and the daemon is running. |
| 15 | + * Runs `docker info` with a 5-second timeout. |
| 16 | + * |
| 17 | + * @returns True if Docker is available and responsive. |
| 18 | + */ |
| 19 | + static isDockerAvailable(): boolean { |
| 20 | + try { |
| 21 | + execSync('docker info', { stdio: 'pipe', timeout: 5000 }); |
| 22 | + return true; |
| 23 | + } catch { |
| 24 | + return false; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Check the state of a named Docker container. |
| 30 | + * |
| 31 | + * @param name - Container name to inspect. |
| 32 | + * @returns 'running' if active, 'stopped' if exists but not running, |
| 33 | + * 'not_found' if the container doesn't exist. |
| 34 | + */ |
| 35 | + static getContainerState(name: string): 'running' | 'stopped' | 'not_found' { |
| 36 | + try { |
| 37 | + const output = execSync( |
| 38 | + `docker inspect --format='{{.State.Running}}' "${name}"`, |
| 39 | + { stdio: 'pipe', timeout: 5000 }, |
| 40 | + ).toString().trim(); |
| 41 | + // docker inspect returns 'true' or 'false' for .State.Running |
| 42 | + return output === 'true' ? 'running' : 'stopped'; |
| 43 | + } catch { |
| 44 | + // Container doesn't exist or docker not available. |
| 45 | + return 'not_found'; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Start a stopped container by name. |
| 51 | + * |
| 52 | + * @param name - Container name to start. |
| 53 | + * @throws If the container cannot be started. |
| 54 | + */ |
| 55 | + static startContainer(name: string): void { |
| 56 | + execSync(`docker start "${name}"`, { stdio: 'pipe', timeout: 15000 }); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Pull a Docker image and run a new container. |
| 61 | + * |
| 62 | + * @param opts.name - Container name. |
| 63 | + * @param opts.image - Docker image (e.g. 'qdrant/qdrant:latest'). |
| 64 | + * @param opts.ports - Port mappings (e.g. ['6333:6333', '6334:6334']). |
| 65 | + * @param opts.volumes - Volume mounts (e.g. ['data-vol:/data']). |
| 66 | + * @param opts.env - Environment variables (e.g. { POSTGRES_PASSWORD: 'pw' }). |
| 67 | + */ |
| 68 | + static pullAndRun(opts: { |
| 69 | + name: string; |
| 70 | + image: string; |
| 71 | + ports: string[]; |
| 72 | + volumes: string[]; |
| 73 | + env?: Record<string, string>; |
| 74 | + }): void { |
| 75 | + // Pull the image first (allows progress output via pipe). |
| 76 | + execSync(`docker pull "${opts.image}"`, { stdio: 'pipe', timeout: 120000 }); |
| 77 | + |
| 78 | + // Build the docker run command. |
| 79 | + const portFlags = opts.ports.map(p => `-p ${p}`).join(' '); |
| 80 | + const volFlags = opts.volumes.map(v => `-v ${v}`).join(' '); |
| 81 | + const envFlags = opts.env |
| 82 | + ? Object.entries(opts.env).map(([k, v]) => `-e "${k}=${v}"`).join(' ') |
| 83 | + : ''; |
| 84 | + |
| 85 | + const cmd = [ |
| 86 | + 'docker run -d', |
| 87 | + `--name "${opts.name}"`, |
| 88 | + portFlags, |
| 89 | + volFlags, |
| 90 | + envFlags, |
| 91 | + `"${opts.image}"`, |
| 92 | + ].filter(Boolean).join(' '); |
| 93 | + |
| 94 | + execSync(cmd, { stdio: 'pipe', timeout: 30000 }); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Poll a health check URL until it returns 200 or timeout is reached. |
| 99 | + * Checks every 500ms. |
| 100 | + * |
| 101 | + * @param url - Health check endpoint (e.g. 'http://localhost:6333/healthz'). |
| 102 | + * @param timeoutMs - Maximum time to wait in milliseconds. @default 15000 |
| 103 | + * @returns True if the endpoint became healthy within the timeout. |
| 104 | + */ |
| 105 | + static async waitForHealthy(url: string, timeoutMs = 15000): Promise<boolean> { |
| 106 | + const start = Date.now(); |
| 107 | + while (Date.now() - start < timeoutMs) { |
| 108 | + try { |
| 109 | + const res = await fetch(url); |
| 110 | + if (res.ok) return true; |
| 111 | + } catch { |
| 112 | + // Not ready yet — keep polling. |
| 113 | + } |
| 114 | + await new Promise(r => setTimeout(r, 500)); |
| 115 | + } |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Get the mapped host port for a container's internal port. |
| 121 | + * Useful when the host port was dynamically assigned. |
| 122 | + * |
| 123 | + * @param name - Container name. |
| 124 | + * @param internalPort - The container-internal port to look up. |
| 125 | + * @returns The host port number, or null if not found. |
| 126 | + */ |
| 127 | + static getHostPort(name: string, internalPort: number): number | null { |
| 128 | + try { |
| 129 | + const output = execSync( |
| 130 | + `docker port "${name}" ${internalPort}`, |
| 131 | + { stdio: 'pipe', timeout: 5000 }, |
| 132 | + ).toString().trim(); |
| 133 | + // Output format: "0.0.0.0:6333" or ":::6333" |
| 134 | + const parts = output.split(':'); |
| 135 | + const port = parseInt(parts[parts.length - 1], 10); |
| 136 | + return isNaN(port) ? null : port; |
| 137 | + } catch { |
| 138 | + return null; |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments