From 4693177e4b91d3a4c32ad178a106b817e942e750 Mon Sep 17 00:00:00 2001 From: gorkem-bwl Date: Fri, 29 Aug 2025 00:27:01 -0400 Subject: [PATCH 1/4] feat: add Docker container monitoring by name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Update Docker monitoring to support both container names and IDs - Maintain backward compatibility with existing container ID monitoring - Add logic to find containers by name (with or without "/" prefix) - Update frontend UI labels and placeholders to reflect name/ID support - Update instructions to clarify both name and ID are supported Resolves user request: "It would be great if we could monitor docker containers by name and not just ID - given that the ID changes when containers are taken down for any reason" 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- client/src/locales/en.json | 6 +++--- .../service/infrastructure/networkService.js | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/client/src/locales/en.json b/client/src/locales/en.json index 991f57d72..79a5ce4e8 100644 --- a/client/src/locales/en.json +++ b/client/src/locales/en.json @@ -440,7 +440,7 @@ "uptimeGeneralInstructions": { "http": "Enter the URL or IP to monitor (e.g., https://example.com/ or 192.168.1.100) and add a clear display name that appears on the dashboard.", "ping": "Enter the IP address or hostname to ping (e.g., 192.168.1.100 or example.com) and add a clear display name that appears on the dashboard.", - "docker": "Enter the Docker ID of your container. Docker IDs must be the full 64 char Docker ID. You can run docker inspect to get the full container ID.", + "docker": "Enter the Docker container name or ID. You can use either the container name (e.g., my-app) or the container ID (full 64-char ID or short ID).", "port": "Enter the URL or IP of the server, the port number and a clear display name that appears on the dashboard.", "game": "Enter the IP address or hostname and the port number to ping (e.g., 192.168.1.100 or example.com) and choose game type." }, @@ -1014,9 +1014,9 @@ }, "monitorType": { "docker": { - "label": "Container ID", + "label": "Container Name/ID", "namePlaceholder": "My Container", - "placeholder": "abcd1234" + "placeholder": "my-app or abcd1234" }, "http": { "label": "URL to monitor", diff --git a/server/src/service/infrastructure/networkService.js b/server/src/service/infrastructure/networkService.js index 72a117e74..1d8125be9 100644 --- a/server/src/service/infrastructure/networkService.js +++ b/server/src/service/infrastructure/networkService.js @@ -254,12 +254,23 @@ class NetworkService { }); const containers = await docker.listContainers({ all: true }); - const containerExists = containers.some((c) => c.Id.startsWith(monitor.url)); - if (!containerExists) { + + // Find container by ID (original behavior) or by name (new feature) + let targetContainer = containers.find((c) => { + // Check if monitor.url matches container ID (partial match) + if (c.Id.startsWith(monitor.url)) { + return true; + } + // Check if monitor.url matches any container name + // Container names are stored as ["/container-name"] in the Names array + return c.Names.some((name) => name === `/${monitor.url}` || name === monitor.url); + }); + + if (!targetContainer) { throw new Error(this.stringService.dockerNotFound); } - const container = docker.getContainer(monitor.url); + const container = docker.getContainer(targetContainer.Id); const { response, responseTime, error } = await this.timeRequest(() => container.inspect()); const dockerResponse = { From c445463c0ce584668bcb9ae8250900f03af427f3 Mon Sep 17 00:00:00 2001 From: gorkem-bwl Date: Fri, 29 Aug 2025 00:53:40 -0400 Subject: [PATCH 2/4] refactor: improve Docker container matching with deterministic priority-based logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Address code review feedback to eliminate ambiguity issues and improve reliability: **Backend Improvements:** - Implement priority-based matching: exact ID → exact name → partial ID - Add input normalization (strip leading slashes, case-insensitive) - Add ambiguity detection with warning logs for debugging - Maintain full backwards compatibility with existing monitors **Frontend Improvements:** - Update validation regex to support leading slashes (/container-name) - Improve error message clarity for user guidance **Resolves Ambiguity Issues:** - Container name "abc" vs Container ID "abc123..." now deterministic - Multiple containers with similar names handled predictably - Case-insensitive matching for better user experience **Priority Order:** 1. Exact full ID match (64-char) - highest priority 2. Exact container name match - case-insensitive 3. Partial ID match - backwards compatibility fallback 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- client/src/Validation/validation.js | 4 +- .../service/infrastructure/networkService.js | 44 +++++++++++++++---- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/client/src/Validation/validation.js b/client/src/Validation/validation.js index 26e390ff4..b6a40914f 100644 --- a/client/src/Validation/validation.js +++ b/client/src/Validation/validation.js @@ -130,10 +130,10 @@ const monitorValidation = joi.object({ then: joi .string() .trim() - .regex(/^[a-z0-9]{64}$/) + .regex(/^(\/+)?([a-zA-Z0-9][a-zA-Z0-9_.-]*[a-zA-Z0-9]|[a-zA-Z0-9]+|[a-f0-9]{12,64})$/) .messages({ "string.empty": "This field is required.", - "string.pattern.base": "Please enter a valid 64-character Docker container ID.", + "string.pattern.base": "Please enter a valid container name or ID.", }), otherwise: joi .string() diff --git a/server/src/service/infrastructure/networkService.js b/server/src/service/infrastructure/networkService.js index 1d8125be9..6e9daacf4 100644 --- a/server/src/service/infrastructure/networkService.js +++ b/server/src/service/infrastructure/networkService.js @@ -255,16 +255,42 @@ class NetworkService { const containers = await docker.listContainers({ all: true }); - // Find container by ID (original behavior) or by name (new feature) - let targetContainer = containers.find((c) => { - // Check if monitor.url matches container ID (partial match) - if (c.Id.startsWith(monitor.url)) { - return true; + // Normalize input: strip leading slashes and convert to lowercase for comparison + const normalizedInput = monitor.url.replace(/^\/+/, "").toLowerCase(); + + // Priority-based matching to avoid ambiguity: + // 1. Exact full ID match (64-char) + let exactIdMatch = containers.find((c) => c.Id.toLowerCase() === normalizedInput); + + // 2. Exact container name match (case-insensitive) + let exactNameMatch = containers.find((c) => + c.Names.some((name) => { + const cleanName = name.replace(/^\/+/, "").toLowerCase(); + return cleanName === normalizedInput; + }) + ); + + // 3. Partial ID match (fallback for backwards compatibility) + let partialIdMatch = containers.find((c) => c.Id.toLowerCase().startsWith(normalizedInput)); + + // Select container based on priority + let targetContainer = exactIdMatch || exactNameMatch || partialIdMatch; + + // Log warning if ambiguous matches exist + if (targetContainer && this.logger) { + const matchTypes = []; + if (exactIdMatch) matchTypes.push("exact ID"); + if (exactNameMatch) matchTypes.push("exact name"); + if (partialIdMatch && !exactIdMatch) matchTypes.push("partial ID"); + + if (matchTypes.length > 1) { + this.logger.warn({ + message: `Ambiguous container match for "${monitor.url}". Matched by: ${matchTypes.join(", ")}. Using ${exactIdMatch ? "exact ID" : exactNameMatch ? "exact name" : "partial ID"} match.`, + service: this.SERVICE_NAME, + method: "requestDocker", + }); } - // Check if monitor.url matches any container name - // Container names are stored as ["/container-name"] in the Names array - return c.Names.some((name) => name === `/${monitor.url}` || name === monitor.url); - }); + } if (!targetContainer) { throw new Error(this.stringService.dockerNotFound); From ba757b9aa4493ecb0324260616fcefec02714bad Mon Sep 17 00:00:00 2001 From: gorkem-bwl Date: Fri, 29 Aug 2025 00:58:03 -0400 Subject: [PATCH 3/4] style: fix prettier formatting in validation.js --- client/src/Validation/validation.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/client/src/Validation/validation.js b/client/src/Validation/validation.js index b6a40914f..55141be3e 100644 --- a/client/src/Validation/validation.js +++ b/client/src/Validation/validation.js @@ -130,7 +130,9 @@ const monitorValidation = joi.object({ then: joi .string() .trim() - .regex(/^(\/+)?([a-zA-Z0-9][a-zA-Z0-9_.-]*[a-zA-Z0-9]|[a-zA-Z0-9]+|[a-f0-9]{12,64})$/) + .regex( + /^(\/+)?([a-zA-Z0-9][a-zA-Z0-9_.-]*[a-zA-Z0-9]|[a-zA-Z0-9]+|[a-f0-9]{12,64})$/ + ) .messages({ "string.empty": "This field is required.", "string.pattern.base": "Please enter a valid container name or ID.", From 44ba6b4d0f6690fc89987f640022131cf3a81224 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 29 Aug 2025 09:22:02 -0700 Subject: [PATCH 4/4] early returns --- .../service/infrastructure/networkService.js | 61 ++++++++++++------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/server/src/service/infrastructure/networkService.js b/server/src/service/infrastructure/networkService.js index 6e9daacf4..d1a1ff108 100644 --- a/server/src/service/infrastructure/networkService.js +++ b/server/src/service/infrastructure/networkService.js @@ -253,6 +253,11 @@ class NetworkService { handleError: true, // Enable error handling }); + const dockerResponse = { + monitorId: monitor._id, + type: monitor.type, + }; + const containers = await docker.listContainers({ all: true }); // Normalize input: strip leading slashes and convert to lowercase for comparison @@ -276,37 +281,47 @@ class NetworkService { // Select container based on priority let targetContainer = exactIdMatch || exactNameMatch || partialIdMatch; - // Log warning if ambiguous matches exist - if (targetContainer && this.logger) { - const matchTypes = []; - if (exactIdMatch) matchTypes.push("exact ID"); - if (exactNameMatch) matchTypes.push("exact name"); - if (partialIdMatch && !exactIdMatch) matchTypes.push("partial ID"); + // Return negative response if no container + if (!targetContainer) { + this.logger.warn({ + message: `No container found for "${monitor.url}".`, + service: this.SERVICE_NAME, + method: "requestDocker", + details: { url: monitor.url }, + }); - if (matchTypes.length > 1) { - this.logger.warn({ - message: `Ambiguous container match for "${monitor.url}". Matched by: ${matchTypes.join(", ")}. Using ${exactIdMatch ? "exact ID" : exactNameMatch ? "exact name" : "partial ID"} match.`, - service: this.SERVICE_NAME, - method: "requestDocker", - }); - } + dockerResponse.code = 404; + dockerResponse.status = false; + dockerResponse.message = this.stringService.dockerNotFound; + return dockerResponse; } - if (!targetContainer) { - throw new Error(this.stringService.dockerNotFound); + // Return negative response if ambiguous matches exist + const matchTypes = []; + if (exactIdMatch) matchTypes.push("exact ID"); + if (exactNameMatch) matchTypes.push("exact name"); + if (partialIdMatch && !exactIdMatch) matchTypes.push("partial ID"); + + if (matchTypes.length > 1) { + this.logger.warn({ + message: `Ambiguous container match for "${monitor.url}". Matched by: ${matchTypes.join(", ")}. Using ${exactIdMatch ? "exact ID" : exactNameMatch ? "exact name" : "partial ID"} match.`, + service: this.SERVICE_NAME, + method: "requestDocker", + details: { url: monitor.url }, + }); + dockerResponse.status = 404; + dockerResponse.status = false; + dockerResponse.message = `Ambiguous container match for "${monitor.url}". Matched by: ${matchTypes.join(", ")}. Using ${exactIdMatch ? "exact ID" : exactNameMatch ? "exact name" : "partial ID"} match.`; + return dockerResponse; } const container = docker.getContainer(targetContainer.Id); const { response, responseTime, error } = await this.timeRequest(() => container.inspect()); - const dockerResponse = { - monitorId: monitor._id, - type: monitor.type, - responseTime, - status: response?.State?.Status === "running" ? true : false, - code: 200, - message: "Docker container status fetched successfully", - }; + dockerResponse.responseTime = responseTime; + dockerResponse.status = response?.State?.Status === "running" ? true : false; + dockerResponse.code = 200; + dockerResponse.message = "Docker container status fetched successfully"; if (error) { dockerResponse.status = false;