From 1639da57794d2555426e56030b4b34e2cc201aa9 Mon Sep 17 00:00:00 2001 From: Matt Van Horn Date: Fri, 27 Mar 2026 12:04:03 -0700 Subject: [PATCH] fix: handle null ports in Docker container table (#5313) When Docker API returns `null` for container ports (instead of `[]`), the Docker management page crashes. Added optional chaining on `ports?.length` to handle this case. Fixes #5311 Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> --- .../src/app/[locale]/manage/tools/docker/docker-table.tsx | 1 + packages/api/src/router/docker/docker-router.ts | 2 +- .../modals-collection/src/docker/add-docker-app-to-homarr.tsx | 2 +- packages/request-handler/src/docker.ts | 3 ++- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/apps/nextjs/src/app/[locale]/manage/tools/docker/docker-table.tsx b/apps/nextjs/src/app/[locale]/manage/tools/docker/docker-table.tsx index f8c8509f5..537c85e7c 100644 --- a/apps/nextjs/src/app/[locale]/manage/tools/docker/docker-table.tsx +++ b/apps/nextjs/src/app/[locale]/manage/tools/docker/docker-table.tsx @@ -76,6 +76,7 @@ const createColumns = ( accessorKey: "ports", header: t("docker.field.ports.label"), Cell({ cell }) { + if (!cell.row.original.ports) return null; if (!cell.row.original.ports.length) return null; return ( port.PrivatePort.toString())} /> diff --git a/packages/api/src/router/docker/docker-router.ts b/packages/api/src/router/docker/docker-router.ts index 6a0ea49a0..e5a4b4796 100644 --- a/packages/api/src/router/docker/docker-router.ts +++ b/packages/api/src/router/docker/docker-router.ts @@ -140,7 +140,7 @@ interface DockerContainer { id: string; state: ContainerState; image: string; - ports: Port[]; + ports: Port[] | undefined; iconUrl: string | null; cpuUsage: number; memoryUsage: number; diff --git a/packages/modals-collection/src/docker/add-docker-app-to-homarr.tsx b/packages/modals-collection/src/docker/add-docker-app-to-homarr.tsx index fc8567ccb..f94bf8f24 100644 --- a/packages/modals-collection/src/docker/add-docker-app-to-homarr.tsx +++ b/packages/modals-collection/src/docker/add-docker-app-to-homarr.tsx @@ -21,7 +21,7 @@ export const AddDockerAppToHomarrModal = createModal( { initialValues: { containerUrls: innerProps.selectedContainers.map((container) => { - if (container.ports[0]) { + if (Array.isArray(container.ports) && container.ports[0]) { return `http://${container.ports[0].IP}:${container.ports[0].PublicPort}`; } diff --git a/packages/request-handler/src/docker.ts b/packages/request-handler/src/docker.ts index 0d2c02809..7b873ec00 100644 --- a/packages/request-handler/src/docker.ts +++ b/packages/request-handler/src/docker.ts @@ -1,5 +1,6 @@ import dayjs from "dayjs"; import type { ContainerInfo, ContainerStats } from "dockerode"; +import type Dockerode from "dockerode"; import { db, like, or } from "@homarr/db"; import { icons } from "@homarr/db/schema"; @@ -72,7 +73,7 @@ async function getContainersWithStatsAsync() { cpuUsage, memoryUsage, image: container.Image, - ports: container.Ports, + ports: container.Ports as Dockerode.Port[] | undefined, }; });