docker_container_stats
Monitor real-time CPU, memory, and network statistics for Docker containers to track performance and resource usage.
Instructions
Get real-time CPU, memory, and network stats for a container
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | Yes | Container ID or name |
Implementation Reference
- src/tools/docker/containers.ts:89-119 (handler)Implementation of the containerStats function which fetches and formats Docker container metrics.
export async function containerStats(args: Record<string, unknown>): Promise<string> { const docker = getDockerClient(); const id = args.id as string || args.name as string; if (!id) throw new Error("Container ID or name is required"); const container = docker.getContainer(id); const stats = await container.stats({ stream: false }); const cpuDelta = stats.cpu_stats.cpu_usage.total_usage - stats.precpu_stats.cpu_usage.total_usage; const systemDelta = stats.cpu_stats.system_cpu_usage - stats.precpu_stats.system_cpu_usage; const cpuPercent = systemDelta > 0 ? (cpuDelta / systemDelta) * 100 : 0; const memUsage = stats.memory_stats.usage || 0; const memLimit = stats.memory_stats.limit || 1; const memPercent = (memUsage / memLimit) * 100; const lines = [ `Stats for container '${id}':`, "", ` CPU: ${cpuPercent.toFixed(2)}%`, ` Memory: ${Math.round(memUsage / 1024 / 1024)}Mi / ${Math.round(memLimit / 1024 / 1024)}Mi (${memPercent.toFixed(1)}%)`, ]; const networks = stats.networks || {}; for (const [iface, net] of Object.entries(networks)) { const n = net as { rx_bytes: number; tx_bytes: number }; lines.push(` Net ${iface}: RX ${Math.round(n.rx_bytes / 1024)}Ki / TX ${Math.round(n.tx_bytes / 1024)}Ki`); } return lines.join("\n"); } - src/tools/docker/index.ts:43-52 (registration)Registration of the 'docker_container_stats' tool in the Docker tools list.
name: "docker_container_stats", description: "Get real-time CPU, memory, and network stats for a container", inputSchema: { type: "object" as const, properties: { id: { type: "string", description: "Container ID or name" }, }, required: ["id"], }, },