Merge pull request #5024 from Dokploy/fix/stack-containers-worker-nodes-4520

fix: show stack containers running on worker nodes
This commit is contained in:
Mauricio Siu
2026-08-09 11:38:00 -06:00
committed by GitHub
3 changed files with 165 additions and 87 deletions
@@ -114,6 +114,7 @@ export const ShowComposeContainers = ({
<TableHead>Name</TableHead> <TableHead>Name</TableHead>
<TableHead>State</TableHead> <TableHead>State</TableHead>
<TableHead>Status</TableHead> <TableHead>Status</TableHead>
{appType === "stack" && <TableHead>Node</TableHead>}
<TableHead>Container ID</TableHead> <TableHead>Container ID</TableHead>
<TableHead className="text-right" /> <TableHead className="text-right" />
</TableRow> </TableRow>
@@ -121,8 +122,9 @@ export const ShowComposeContainers = ({
<TableBody> <TableBody>
{data.map((container) => ( {data.map((container) => (
<ContainerRow <ContainerRow
key={container.containerId} key={container.name}
container={container} container={container}
appType={appType}
serverId={serverId} serverId={serverId}
serviceId={serviceId} serviceId={serviceId}
onActionComplete={() => refetch()} onActionComplete={() => refetch()}
@@ -143,7 +145,9 @@ interface ContainerRowProps {
name: string; name: string;
state: string; state: string;
status: string; status: string;
node?: string;
}; };
appType: "stack" | "docker-compose";
serverId?: string; serverId?: string;
serviceId?: string; serviceId?: string;
onActionComplete: () => void; onActionComplete: () => void;
@@ -151,6 +155,7 @@ interface ContainerRowProps {
const ContainerRow = ({ const ContainerRow = ({
container, container,
appType,
serverId, serverId,
serviceId, serviceId,
onActionComplete, onActionComplete,
@@ -192,7 +197,13 @@ const ContainerRow = ({
variant={ variant={
container.state === "running" container.state === "running"
? "default" ? "default"
: container.state === "exited" : [
"exited",
"pending",
"preparing",
"starting",
"ready",
].includes(container.state)
? "secondary" ? "secondary"
: "destructive" : "destructive"
} }
@@ -201,10 +212,12 @@ const ContainerRow = ({
</Badge> </Badge>
</TableCell> </TableCell>
<TableCell>{container.status}</TableCell> <TableCell>{container.status}</TableCell>
{appType === "stack" && <TableCell>{container.node || "-"}</TableCell>}
<TableCell className="font-mono text-sm text-muted-foreground"> <TableCell className="font-mono text-sm text-muted-foreground">
{container.containerId} {container.containerId || "-"}
</TableCell> </TableCell>
<TableCell className="text-right"> <TableCell className="text-right">
{!container.containerId ? null : (
<Dialog open={logsOpen} onOpenChange={setLogsOpen}> <Dialog open={logsOpen} onOpenChange={setLogsOpen}>
<DropdownMenu> <DropdownMenu>
<DropdownMenuTrigger asChild> <DropdownMenuTrigger asChild>
@@ -291,6 +304,7 @@ const ContainerRow = ({
</div> </div>
</DialogContent> </DialogContent>
</Dialog> </Dialog>
)}
</TableCell> </TableCell>
</TableRow> </TableRow>
); );
@@ -57,7 +57,7 @@ export const ShowDockerLogsStack = ({
}, },
); );
const { data: containers, isPending: containersLoading } = const { data, isPending: containersLoading } =
api.docker.getContainersByAppNameMatch.useQuery( api.docker.getContainersByAppNameMatch.useQuery(
{ {
appName, appName,
@@ -69,6 +69,8 @@ export const ShowDockerLogsStack = ({
}, },
); );
const containers = data?.filter((container) => container.containerId);
useEffect(() => { useEffect(() => {
if (option === "native") { if (option === "native") {
if (containers && containers?.length > 0) { if (containers && containers?.length > 0) {
+63 -1
View File
@@ -108,6 +108,9 @@ export const getContainersByAppNameMatch = async (
serverId?: string, serverId?: string,
) => { ) => {
try { try {
if (appType === "stack") {
return await getStackTaskContainers(appName, serverId);
}
let result: string[] = []; let result: string[] = [];
const cmd = const cmd =
"docker ps -a --format 'CONTAINER ID : {{.ID}} | Name: {{.Names}} | State: {{.State}} | Status: {{.Status}}'"; "docker ps -a --format 'CONTAINER ID : {{.ID}} | Name: {{.Names}} | State: {{.State}} | Status: {{.Status}}'";
@@ -166,6 +169,66 @@ export const getContainersByAppNameMatch = async (
return []; return [];
}; };
const getStackTaskContainers = async (appName: string, serverId?: string) => {
try {
const divider = "__DOKPLOY_DIVIDER__";
const tasksCommand = `docker stack ps ${appName} --no-trunc --filter "desired-state=running" --format 'TASK : {{.ID}} | Name: {{.Name}} | Node: {{.Node}} | CurrentState: {{.CurrentState}} | Error: {{.Error}}'`;
const inspectCommand = `docker stack ps ${appName} -q --no-trunc --filter "desired-state=running" | xargs -r docker inspect --format '{{if .Status.ContainerStatus}}TASK : {{.ID}} | ContainerId: {{.Status.ContainerStatus.ContainerID}}{{end}}' 2>/dev/null`;
const command = `${tasksCommand} && echo "${divider}" && (${inspectCommand} || true)`;
let stdout = "";
if (serverId) {
const result = await execAsyncRemote(serverId, command);
stdout = result.stdout;
} else {
const result = await execAsync(command);
stdout = result.stdout;
}
if (!stdout) return [];
const [tasksSection = "", inspectSection = ""] = stdout.split(divider);
const containerIdByTask = new Map<string, string>();
for (const line of inspectSection.trim().split("\n")) {
const parts = line.split(" | ");
const taskId = parts[0]?.replace("TASK : ", "").trim();
const containerId = parts[1]?.replace("ContainerId: ", "").trim();
if (taskId && containerId) {
containerIdByTask.set(taskId, containerId);
}
}
const containers = [];
for (const line of tasksSection.trim().split("\n")) {
if (!line) continue;
const parts = line.split(" | ");
const taskId = parts[0]?.replace("TASK : ", "").trim() ?? "";
const name = parts[1]?.replace("Name: ", "").trim() ?? "";
const node = parts[2]?.replace("Node: ", "").trim() ?? "";
const currentState = parts[3]
? parts[3].replace("CurrentState: ", "").trim()
: "";
const error = parts[4] ? parts[4].replace("Error: ", "").trim() : "";
const containerId = containerIdByTask.get(taskId) ?? "";
containers.push({
containerId: containerId.slice(0, 12),
name: `${name}.${taskId}`,
node,
state: currentState.split(" ")[0]?.toLowerCase() ?? "No state",
status: error ? `${currentState} (${error})` : currentState,
});
}
return containers;
} catch {}
return [];
};
export const getStackContainersByAppName = async ( export const getStackContainersByAppName = async (
appName: string, appName: string,
serverId?: string, serverId?: string,
@@ -175,7 +238,6 @@ export const getStackContainersByAppName = async (
const command = `docker stack ps ${appName} --no-trunc --format 'CONTAINER ID : {{.ID}} | Name: {{.Name}} | State: {{.DesiredState}} | Node: {{.Node}} | CurrentState: {{.CurrentState}} | Error: {{.Error}}'`; const command = `docker stack ps ${appName} --no-trunc --format 'CONTAINER ID : {{.ID}} | Name: {{.Name}} | State: {{.DesiredState}} | Node: {{.Node}} | CurrentState: {{.CurrentState}} | Error: {{.Error}}'`;
console.log("command ", command);
if (serverId) { if (serverId) {
const { stdout, stderr } = await execAsyncRemote(serverId, command); const { stdout, stderr } = await execAsyncRemote(serverId, command);