Skip to content

Modern Docker Management, Designed for Everyone

chore: improve container details page

Kyle Mendell46b4699bd9d1
diff --git a/src/lib/services/docker-service.ts b/src/lib/services/docker-service.ts
index f470ab48c..0552ffc4f 100644
--- a/src/lib/services/docker-service.ts
+++ b/src/lib/services/docker-service.ts
@@ -68,30 +68,46 @@ export async function getDockerInfo(hostToTest?: string) {
}
}
+// Define and export the type returned by listContainers
+export type ServiceContainer = {
+ id: string;
+ names: string[];
+ name: string; // Your derived name
+ image: string;
+ imageId: string;
+ command: string;
+ created: number;
+ state: string; // 'created', 'running', 'paused', 'restarting', 'removing', 'exited', 'dead'
+ status: string; // e.g., "Up 2 hours"
+ ports: Docker.Port[];
+};
+
/**
* Lists Docker containers.
* @param all - Whether to show all containers (including stopped). Defaults to true.
*/
-export async function listContainers(all = true) {
+// Add the return type annotation
+export async function listContainers(all = true): Promise<ServiceContainer[]> {
try {
const containers = await defaultDocker.listContainers({ all });
- // Map to a simpler format if desired, or return raw data
- return containers.map((c) => ({
- id: c.Id,
- names: c.Names,
- name: c.Names[0]?.substring(1) || c.Id.substring(0, 12), // Extract a primary name
- image: c.Image,
- imageId: c.ImageID,
- command: c.Command,
- created: c.Created,
- state: c.State, // 'created', 'running', 'paused', 'restarting', 'removing', 'exited', 'dead'
- status: c.Status,
- ports: c.Ports,
- mounts: c.Mounts,
- }));
- } catch (error) {
+ // Ensure the mapping matches the ServiceContainer type
+ return containers.map(
+ (c): ServiceContainer => ({
+ id: c.Id,
+ names: c.Names,
+ name: c.Names[0]?.substring(1) || c.Id.substring(0, 12),
+ image: c.Image,
+ imageId: c.ImageID,
+ command: c.Command,
+ created: c.Created,
+ state: c.State,
+ status: c.Status,
+ ports: c.Ports,
+ })
+ );
+ } catch (error: any) {
console.error("Error listing containers:", error);
- // Rethrow or return an empty array/error indicator
+ // Rethrow or handle as appropriate for your app's error strategy
throw new Error("Failed to list Docker containers.");
}
}
diff --git a/src/lib/types/container.ts b/src/lib/types/container.ts
deleted file mode 100644
index 952d2fd16..000000000
--- a/src/lib/types/container.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-export type Container = {
- name: string;
- id: string;
- image: string;
- status: "running" | "stopped" | "failed" | "healthy" | "unhealthy";
-};
diff --git a/src/routes/containers/+page.server.ts b/src/routes/containers/+page.server.ts
index d2254c7dc..827886386 100644
--- a/src/routes/containers/+page.server.ts
+++ b/src/routes/containers/+page.server.ts
@@ -1,34 +1,21 @@
import type { PageServerLoad } from "./$types";
-import type { Container } from "$lib/types/container";
+import { listContainers } from "$lib/services/docker-service"; // Import the service function
-// This will eventually call your Docker API
-export const load: PageServerLoad = async ({ fetch }) => {
- // In production, this would call your API endpoint
- // const response = await fetch('/api/docker/containers');
- // const containers = await response.json();
+export const load: PageServerLoad = async () => {
+ try {
+ // Call the listContainers function from your service
+ const containers = await listContainers(true); // Get all containers (running and stopped)
- const containers: Container[] = [
- {
- id: "1a2b3c-11111",
- name: "1a2b3c",
- status: "running",
- image: "nginx:latest",
- },
- {
- id: "1a2b3c-22222",
- name: "1a2b3c",
- status: "running",
- image: "nginx:latest",
- },
- {
- id: "1a2b3c-3333",
- name: "1a2b3c",
- status: "running",
- image: "nginx:latest",
- },
- ];
-
- return {
- containers,
- };
+ // The listContainers function already maps the data, so we can return it directly
+ return {
+ containers,
+ };
+ } catch (error: any) {
+ console.error("Failed to load containers:", error);
+ // Return an error state or an empty array so the page can handle it
+ return {
+ containers: [],
+ error: error.message || "Failed to connect to Docker or list containers.",
+ };
+ }
};
diff --git a/src/routes/containers/+page.svelte b/src/routes/containers/+page.svelte
index f54f8af3c..b0ffcf8ae 100644
--- a/src/routes/containers/+page.svelte
+++ b/src/routes/containers/+page.svelte
@@ -1,7 +1,9 @@
<script lang="ts">
import DataTable from "$lib/components/data-table.svelte";
+ import Button from "$lib/components/ui/button/button.svelte";
import * as Card from "$lib/components/ui/card/index.js";
import { columns } from "./columns.js";
+ import { Plus } from "@lucide/svelte";
let { data } = $props();
const { containers } = data;
@@ -9,9 +11,10 @@
<div class="flex justify-between items-center mb-6">
<h1 class="text-2xl font-bold">Containers</h1>
- <button class="bg-primary text-primary-foreground px-4 py-2 rounded"
- >New Container</button
- >
+ <Button variant="default">
+ <Plus class="w-4 h-4" />
+ Create Container
+ </Button>
</div>
<Card.Root>
diff --git a/src/routes/containers/[id]/+page.svelte b/src/routes/containers/[id]/+page.svelte
index b0d9a9311..38106c94c 100644
--- a/src/routes/containers/[id]/+page.svelte
+++ b/src/routes/containers/[id]/+page.svelte
@@ -1,133 +1,170 @@
<script lang="ts">
+ import type { PageData } from "./$types";
import * as Card from "$lib/components/ui/card/index.js";
+ import { Button } from "$lib/components/ui/button/index.js";
+ import StatusBadge from "$lib/components/docker/StatusBadge.svelte";
+ import { ArrowLeft } from "@lucide/svelte";
+ import * as Breadcrumb from "$lib/components/ui/breadcrumb/index.js";
- let { data } = $props();
+ let { data }: { data: PageData } = $props();
const { container } = data;
+
+ function formatDate(dateString: string | undefined | null): string {
+ if (!dateString) return "Unknown";
+ try {
+ return new Date(dateString).toLocaleString();
+ } catch (e) {
+ return "Invalid Date";
+ }
+ }
</script>
-<div class="flex justify-between items-center mb-6">
- <div>
- <a
- href="/containers"
- class="text-muted-foreground hover:underline mb-2 inline-block"
- >
- &larr; Back to containers
- </a>
- <h1 class="text-2xl font-bold">{container.name}</h1>
- </div>
+<div
+ class="flex flex-col sm:flex-row justify-between items-start sm:items-center mb-6 gap-4"
+>
+ <Breadcrumb.Root>
+ <Breadcrumb.List>
+ <Breadcrumb.Item>
+ <Breadcrumb.Link href="/">Dashboard</Breadcrumb.Link>
+ </Breadcrumb.Item>
+ <Breadcrumb.Separator />
+ <Breadcrumb.Item>
+ <Breadcrumb.Link href="/containers">Containers</Breadcrumb.Link>
+ </Breadcrumb.Item>
+ <Breadcrumb.Separator />
+ <Breadcrumb.Item>
+ <Breadcrumb.Page>{container.name}</Breadcrumb.Page>
+ </Breadcrumb.Item>
+ </Breadcrumb.List>
+ </Breadcrumb.Root>
- <div class="flex gap-2">
- {#if container.status === "running"}
- <button class="bg-secondary text-secondary-foreground px-3 py-1 rounded"
- >Stop</button
- >
- <button class="bg-secondary text-secondary-foreground px-3 py-1 rounded"
- >Restart</button
- >
- {:else}
- <button class="bg-primary text-primary-foreground px-3 py-1 rounded"
- >Start</button
- >
- {/if}
- <button class="bg-destructive text-destructive-foreground px-3 py-1 rounded"
- >Remove</button
- >
- </div>
+ {#if container}
+ <div class="flex gap-2 flex-wrap">
+ {#if container.state?.Running}
+ <Button variant="secondary">Stop</Button>
+ <Button variant="secondary">Restart</Button>
+ {:else if container.state?.Status === "exited" || container.state?.Status === "created"}
+ <Button variant="default">Start</Button>
+ {/if}
+ <Button variant="destructive">Remove</Button>
+ </div>
+ {/if}
</div>
-<div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-6">
- <Card.Root>
- <Card.Header>
- <Card.Title>Status</Card.Title>
- </Card.Header>
- <Card.Content>
- <span
- class={container.status === "running"
- ? "text-green-600"
- : "text-gray-500"}
- >
- {container.status}
- </span>
- </Card.Content>
- </Card.Root>
+{#if container}
+ <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
+ <Card.Root>
+ <Card.Header class="pb-2">
+ <Card.Title class="text-sm font-medium text-muted-foreground"
+ >State</Card.Title
+ >
+ </Card.Header>
+ <Card.Content>
+ <StatusBadge state={container.state?.Status || "unknown"} />
+ </Card.Content>
+ </Card.Root>
- <Card.Root>
- <Card.Header>
- <Card.Title>Image</Card.Title>
- </Card.Header>
- <Card.Content>
- {container.image}
- </Card.Content>
- </Card.Root>
+ <Card.Root>
+ <Card.Header class="pb-2">
+ <Card.Title class="text-sm font-medium text-muted-foreground"
+ >Image</Card.Title
+ >
+ </Card.Header>
+ <Card.Content class="text-sm font-medium break-all">
+ {container.config?.Image || "N/A"}
+ </Card.Content>
+ </Card.Root>
- <Card.Root>
- <Card.Header>
- <Card.Title>Created</Card.Title>
- </Card.Header>
- <Card.Content>
- {container.created || "Unknown"}
- </Card.Content>
- </Card.Root>
+ <Card.Root>
+ <Card.Header class="pb-2">
+ <Card.Title class="text-sm font-medium text-muted-foreground"
+ >Created</Card.Title
+ >
+ </Card.Header>
+ <Card.Content class="text-sm font-medium">
+ {formatDate(container.created)}
+ </Card.Content>
+ </Card.Root>
- <Card.Root>
- <Card.Header>
- <Card.Title>ID</Card.Title>
- </Card.Header>
- <Card.Content class="font-mono text-sm">
- {container.id}
- </Card.Content>
- </Card.Root>
-</div>
+ <Card.Root>
+ <Card.Header class="pb-2">
+ <Card.Title class="text-sm font-medium text-muted-foreground"
+ >IP Address</Card.Title
+ >
+ </Card.Header>
+ <Card.Content class="text-sm font-medium">
+ {container.networkSettings?.IPAddress || "N/A"}
+ </Card.Content>
+ </Card.Root>
+ </div>
-<div class="grid grid-cols-1 md:grid-cols-12 gap-6">
- <Card.Root class="md:col-span-8">
- <Card.Header>
- <Card.Title>Logs</Card.Title>
- <Card.Description>Recent container output</Card.Description>
- </Card.Header>
- <Card.Content>
- <div
- class="bg-black text-white p-4 rounded font-mono text-sm h-64 overflow-y-auto"
- >
- <p>Starting container...</p>
- <p>Container ready</p>
- <!-- Logs content would go here -->
- </div>
- </Card.Content>
- <Card.Footer>
- <a
- href="/containers/{container.id}/logs"
- class="text-primary hover:underline">View full logs</a
- >
- </Card.Footer>
- </Card.Root>
+ <div class="grid grid-cols-1 lg:grid-cols-12 gap-6">
+ <Card.Root class="lg:col-span-8">
+ <Card.Header>
+ <Card.Title>Logs</Card.Title>
+ <Card.Description>Recent container output</Card.Description>
+ </Card.Header>
+ <Card.Content>
+ <div
+ class="bg-muted text-muted-foreground p-4 rounded font-mono text-xs h-64 overflow-y-auto"
+ >
+ <p>Log fetching not implemented yet.</p>
+ </div>
+ </Card.Content>
+ <Card.Footer>
+ <Button
+ variant="link"
+ class="p-0 h-auto"
+ href="/containers/{container.id}/logs"
+ >
+ View full logs
+ </Button>
+ </Card.Footer>
+ </Card.Root>
- <Card.Root class="md:col-span-4">
- <Card.Header>
- <Card.Title>Stats</Card.Title>
- <Card.Description>Resource usage</Card.Description>
- </Card.Header>
- <Card.Content>
- <ul>
- <li class="mb-2">
- <div class="text-muted-foreground text-sm">CPU</div>
- <div class="w-full bg-secondary rounded-full h-2.5 mt-1">
- <div class="bg-primary h-2.5 rounded-full" style="width: 25%"></div>
- </div>
- </li>
- <li class="mb-2">
- <div class="text-muted-foreground text-sm">Memory</div>
- <div class="w-full bg-secondary rounded-full h-2.5 mt-1">
- <div class="bg-primary h-2.5 rounded-full" style="width: 40%"></div>
- </div>
- </li>
- </ul>
- </Card.Content>
- <Card.Footer>
- <a
- href="/containers/{container.id}/stats"
- class="text-primary hover:underline">View detailed stats</a
- >
- </Card.Footer>
- </Card.Root>
-</div>
+ <Card.Root class="lg:col-span-4">
+ <Card.Header>
+ <Card.Title>Stats</Card.Title>
+ <Card.Description>Resource usage</Card.Description>
+ </Card.Header>
+ <Card.Content>
+ <p class="text-sm text-muted-foreground">
+ Stats fetching not implemented yet.
+ </p>
+ <ul>
+ <li class="mb-2">
+ <div class="text-muted-foreground text-sm">CPU</div>
+ <div class="w-full bg-secondary rounded-full h-2.5 mt-1">
+ <div
+ class="bg-primary h-2.5 rounded-full"
+ style="width: 0%"
+ ></div>
+ </div>
+ </li>
+ <li class="mb-2">
+ <div class="text-muted-foreground text-sm">Memory</div>
+ <div class="w-full bg-secondary rounded-full h-2.5 mt-1">
+ <div
+ class="bg-primary h-2.5 rounded-full"
+ style="width: 0%"
+ ></div>
+ </div>
+ </li>
+ </ul>
+ </Card.Content>
+ <Card.Footer>
+ <Button
+ variant="link"
+ class="p-0 h-auto"
+ href="/containers/{container.id}/stats"
+ >
+ View detailed stats
+ </Button>
+ </Card.Footer>
+ </Card.Root>
+ </div>
+{:else}
+ <p class="text-center text-muted-foreground">
+ Could not load container data.
+ </p>
+{/if}
diff --git a/src/routes/containers/columns.ts b/src/routes/containers/columns.ts
index ade3f1267..3458566ca 100644
--- a/src/routes/containers/columns.ts
+++ b/src/routes/containers/columns.ts
@@ -1,9 +1,11 @@
-import type { Container } from "$lib/types/container";
+import type { ServiceContainer } from "$lib/services/docker-service";
import type { ColumnDef } from "@tanstack/table-core";
import { renderComponent } from "$lib/components/ui/data-table/index.js";
import DataTableActions from "./data-table-actions.svelte";
+import StatusBadge from "$lib/components/docker/StatusBadge.svelte";
+import IdCell from "./IdCell.svelte";
-export const columns: ColumnDef<Container>[] = [
+export const columns: ColumnDef<ServiceContainer>[] = [
{
accessorKey: "name",
header: "Name",
@@ -11,20 +13,35 @@ export const columns: ColumnDef<Container>[] = [
{
accessorKey: "id",
header: "ID",
+ cell: ({ row }) => {
+ return renderComponent(IdCell, { id: row.original.id });
+ },
+ enableSorting: false,
},
{
accessorKey: "image",
header: "Image",
},
+ {
+ accessorKey: "state",
+ header: "State",
+ cell: ({ row }) => {
+ return renderComponent(StatusBadge, { state: row.original.state });
+ },
+ },
{
accessorKey: "status",
header: "Status",
},
{
id: "actions",
+ header: "Actions",
cell: ({ row }) => {
- // You can pass whatever you need from `row.original` to the component
- return renderComponent(DataTableActions, { id: row.original.id });
+ return renderComponent(DataTableActions, {
+ id: row.original.id,
+ state: row.original.state,
+ });
},
+ enableSorting: false,
},
];
diff --git a/src/routes/containers/data-table-actions.svelte b/src/routes/containers/data-table-actions.svelte
index d95139380..310abea14 100644
--- a/src/routes/containers/data-table-actions.svelte
+++ b/src/routes/containers/data-table-actions.svelte
@@ -2,8 +2,13 @@
import Ellipsis from "@lucide/svelte/icons/ellipsis";
import { Button } from "$lib/components/ui/button/index.js";
import * as DropdownMenu from "$lib/components/ui/dropdown-menu/index.js";
+ import { goto } from "$app/navigation";
let { id }: { id: string } = $props();
+
+ function viewContainer() {
+ goto(`/containers/${id}`);
+ }
</script>
<DropdownMenu.Root>
@@ -23,8 +28,10 @@
<DropdownMenu.Content>
<DropdownMenu.Group>
<DropdownMenu.GroupHeading>Actions</DropdownMenu.GroupHeading>
- <DropdownMenu.Item>Recreate Container</DropdownMenu.Item>
- <DropdownMenu.Item>Start/Stop</DropdownMenu.Item>
+ <DropdownMenu.Separator />
+ <DropdownMenu.Item onclick={() => viewContainer()}
+ >View Container</DropdownMenu.Item
+ >
</DropdownMenu.Group>
</DropdownMenu.Content>
</DropdownMenu.Root>