Skip to content

Commit 0efe7fe

Browse files
committed
fix: report remote-node containers correctly in stack monitoring
Free monitoring resolved containers via a local Dockerode listContainers() call, which only sees containers scheduled on the host running Dokploy. For Stack services with tasks on other Swarm nodes, this always fell into the "Container not running" branch even when the task was healthy elsewhere, since local docker.listContainers() can't see remote-node containers. Now falls back to `docker service ps` (Swarm-aggregated, works from any manager regardless of task placement) to tell a genuinely stopped container apart from one running on another node, and reports that distinction in the WS close reason instead of the misleading message. Also fixes a client-side race where the stats websocket connected with an empty appName on first mount (before the container selector settled), and surfaces the close reason as a toast instead of only logging it.
1 parent 474081a commit 0efe7fe

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

‎apps/dokploy/components/dashboard/monitoring/free/container/show-free-container-monitoring.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { formatMb } from "@dokploy/server/monitoring/units";
22
import { useEffect, useState } from "react";
3+
import { toast } from "sonner";
34
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
45
import { Progress } from "@/components/ui/progress";
56
import { api } from "@/utils/api";
@@ -167,6 +168,8 @@ export const ContainerFreeMonitoring = ({
167168
}, [data]);
168169

169170
useEffect(() => {
171+
if (!appName) return;
172+
170173
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
171174
const wsUrl = `${protocol}//${window.location.host}/listen-docker-stats-monitoring?appName=${appName}&appType=${appType}`;
172175
const ws = new WebSocket(wsUrl);
@@ -196,7 +199,9 @@ export const ContainerFreeMonitoring = ({
196199
};
197200

198201
ws.onclose = (e) => {
199-
console.log(e.reason);
202+
if (e.reason) {
203+
toast.error(e.reason);
204+
}
200205
};
201206

202207
return () => ws.close();

‎apps/dokploy/server/wss/docker-stats.ts‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,57 @@ import {
88
recordAdvancedStats,
99
validateRequest,
1010
} from "@dokploy/server";
11+
import { quote } from "shell-quote";
1112
import { WebSocketServer } from "ws";
1213
import { canAccessDockerOverWss } from "./authorize";
1314

15+
type AppType = "application" | "stack" | "docker-compose";
16+
17+
// Swarm task names are "<service>.<slot>.<taskId>"; the manager's local
18+
// `docker ps` only sees containers scheduled on this host, so a task running
19+
// on another node looks identical to a stopped one. `docker service ps` is
20+
// swarm-aggregated and answers from the manager regardless of which node the
21+
// task landed on, so we use it here to tell the two cases apart.
22+
const findRemoteSwarmNode = async (
23+
appName: string,
24+
appType: AppType,
25+
): Promise<string | null> => {
26+
if (appType === "docker-compose") {
27+
return null;
28+
}
29+
30+
// `docker service ps --format {{.Name}}` only prints "<service>.<slot>",
31+
// never the taskId, so we match on the task ID instead — the last segment
32+
// of the stack task name — rather than trying to reconstruct the full name.
33+
const [serviceName, taskId] =
34+
appType === "stack"
35+
? [appName.split(".").slice(0, -2).join("."), appName.split(".").pop()]
36+
: [appName, null];
37+
38+
if (!serviceName) {
39+
return null;
40+
}
41+
42+
try {
43+
const { stdout } = await execAsync(
44+
`docker service ps ${quote([serviceName])} --filter "desired-state=running" --no-trunc --format '{"ID":"{{.ID}}","Node":"{{.Node}}","CurrentState":"{{.CurrentState}}"}'`,
45+
);
46+
47+
for (const line of stdout.trim().split("\n")) {
48+
if (!line) continue;
49+
const task = JSON.parse(line);
50+
const isMatch = taskId ? task.ID === taskId : true;
51+
if (isMatch && task.CurrentState?.startsWith("Running")) {
52+
return task.Node;
53+
}
54+
}
55+
} catch {
56+
// Not a swarm service (or the swarm CLI isn't available) — fall back to "not running".
57+
}
58+
59+
return null;
60+
};
61+
1462
export const setupDockerStatsMonitoringSocketServer = (
1563
server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>,
1664
) => {
@@ -98,7 +146,13 @@ export const setupDockerStatsMonitoringSocketServer = (
98146

99147
const container = containers[0];
100148
if (!container || container?.State !== "running") {
101-
ws.close(4000, "Container not running");
149+
const remoteNode = await findRemoteSwarmNode(appName, appType);
150+
ws.close(
151+
4000,
152+
remoteNode
153+
? `Container running on remote node "${remoteNode}"`.slice(0, 123)
154+
: "Container not running",
155+
);
102156
return;
103157
}
104158
const { stdout, stderr } = await execAsync(

0 commit comments

Comments
 (0)