Skip to content

Modern Docker Management, Designed for Everyone

feat: container service link for stacks (#131)

Kyle Mendell420cf918851b
diff --git a/src/lib/services/api/container-api-service.ts b/src/lib/services/api/container-api-service.ts
index 07f928315..7fecd5747 100644
--- a/src/lib/services/api/container-api-service.ts
+++ b/src/lib/services/api/container-api-service.ts
@@ -46,4 +46,9 @@ export default class ContainerAPIService extends BaseAPIService {
const res = await this.api.get('');
return res.data;
}
+
+ async get(id: string) {
+ const res = await this.api.get(`/containers/${id}`);
+ return res.data;
+ }
}
diff --git a/src/lib/types/settings.type.ts b/src/lib/types/settings.type.ts
index df8a6da4b..2d70a54b8 100644
--- a/src/lib/types/settings.type.ts
+++ b/src/lib/types/settings.type.ts
@@ -27,4 +27,5 @@ export interface Settings {
registryCredentials: RegistryCredential[];
auth: AuthSettings;
onboarding?: Onboarding;
+ baseServerUrl?: string;
}
diff --git a/src/routes/api/containers/[containerId]/+server.ts b/src/routes/api/containers/[containerId]/+server.ts
new file mode 100644
index 000000000..fc102a08a
--- /dev/null
+++ b/src/routes/api/containers/[containerId]/+server.ts
@@ -0,0 +1,26 @@
+import { json } from '@sveltejs/kit';
+import type { RequestHandler } from './$types';
+import { getContainer } from '$lib/services/docker/container-service';
+
+export const GET: RequestHandler = async ({ params }) => {
+ const containerId = params.containerId;
+
+ try {
+ const container = await getContainer(containerId);
+
+ if (!container) {
+ return json({ success: false, error: 'Container not found' }, { status: 404 });
+ }
+
+ return json(container);
+ } catch (error: any) {
+ console.error(`API Error getting container ${containerId}:`, error);
+ return json(
+ {
+ success: false,
+ error: error.message || 'Failed to get container information'
+ },
+ { status: 500 }
+ );
+ }
+};
diff --git a/src/routes/settings/tabs/app-settings.svelte b/src/routes/settings/tabs/app-settings.svelte
index d970e111b..21274d231 100644
--- a/src/routes/settings/tabs/app-settings.svelte
+++ b/src/routes/settings/tabs/app-settings.svelte
@@ -79,6 +79,12 @@
<p class="text-xs font-bold text-destructive">Changing this setting will not move existing stacks!</p>
</div>
+ <div class="space-y-2">
+ <label for="baseServerUrl" class="text-sm font-medium">Base Server URL</label>
+ <Input type="text" id="baseServerUrl" name="baseServerUrl" bind:value={$settingsStore.baseServerUrl} placeholder="localhost" />
+ <p class="text-xs text-muted-foreground">Host/IP used when accessing container services that aren't on ipvlan or macvlan networks</p>
+ </div>
+
<div class="pt-4 border-t mt-4">
<div class="flex items-center gap-2 mb-3">
<div class="bg-green-500/10 p-2 rounded-full">
diff --git a/src/routes/stacks/[stackId]/+page.server.ts b/src/routes/stacks/[stackId]/+page.server.ts
index 3fd08e1a9..70408bdfe 100644
--- a/src/routes/stacks/[stackId]/+page.server.ts
+++ b/src/routes/stacks/[stackId]/+page.server.ts
@@ -1,4 +1,6 @@
import { getStack, updateStack, startStack, stopStack, restartStack, removeStack, fullyRedeployStack } from '$lib/services/docker/stack-service';
+import { getSettings } from '$lib/services/settings-service';
+import { getContainer } from '$lib/services/docker/container-service';
import type { PageServerLoad, Actions } from './$types';
export const load: PageServerLoad = async ({ params }) => {
@@ -7,20 +9,56 @@ export const load: PageServerLoad = async ({ params }) => {
try {
const stack = await getStack(stackId);
- // Create editor state with all current values
const editorState = {
name: stack.name,
composeContent: stack.composeContent || '',
- envContent: stack.envContent || '', // Include env content
+ envContent: stack.envContent || '',
autoUpdate: stack.meta?.autoUpdate || false,
originalName: stack.name,
originalComposeContent: stack.composeContent || '',
- originalEnvContent: stack.envContent || '' // Include original env content
+ originalEnvContent: stack.envContent || ''
};
+ const settings = await getSettings();
+
+ const servicePorts: Record<string, string[]> = {};
+ if (stack.services) {
+ for (const service of stack.services) {
+ if (service.id) {
+ try {
+ const containerData = await getContainer(service.id);
+ if (containerData && containerData.networkSettings?.Ports) {
+ const portBindings = containerData.networkSettings.Ports;
+ const parsedPorts: string[] = [];
+
+ for (const containerPort in portBindings) {
+ if (portBindings.hasOwnProperty(containerPort)) {
+ const bindings = portBindings[containerPort];
+ if (bindings && Array.isArray(bindings) && bindings.length > 0) {
+ bindings.forEach((binding: any) => {
+ if (binding.HostPort) {
+ const portType = containerPort.split('/')[1] || 'tcp';
+ parsedPorts.push(`${binding.HostPort}:${containerPort.split('/')[0]}/${portType}`);
+ }
+ });
+ }
+ }
+ }
+
+ servicePorts[service.id] = parsedPorts;
+ }
+ } catch (error) {
+ console.error(`Failed to fetch ports for service ${service.id}:`, error);
+ }
+ }
+ }
+ }
+
return {
stack,
- editorState
+ servicePorts,
+ editorState,
+ settings
};
} catch (err: unknown) {
console.error(`Error loading stack ${stackId}:`, err);
diff --git a/src/routes/stacks/[stackId]/+page.svelte b/src/routes/stacks/[stackId]/+page.svelte
index c979e809b..05c6439a1 100644
--- a/src/routes/stacks/[stackId]/+page.svelte
+++ b/src/routes/stacks/[stackId]/+page.svelte
@@ -2,7 +2,7 @@
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 { ArrowLeft, Loader2, AlertCircle, Save, FileStack, Layers, ArrowRight } from '@lucide/svelte';
+ import { ArrowLeft, Loader2, AlertCircle, Save, FileStack, Layers, ArrowRight, ExternalLink } from '@lucide/svelte';
import * as Breadcrumb from '$lib/components/ui/breadcrumb/index.js';
import * as Alert from '$lib/components/ui/alert/index.js';
import { Input } from '$lib/components/ui/input/index.js';
@@ -23,7 +23,7 @@
const stackApi = new StackAPIService();
let { data }: { data: PageData } = $props();
- let { stack, editorState } = $derived(data);
+ let { stack, editorState, servicePorts, settings } = $derived(data);
let isLoading = $state({
deploying: false,
@@ -48,6 +48,8 @@
let hasChanges = $derived(name !== originalName || composeContent !== originalComposeContent || envContent !== originalEnvContent || autoUpdate !== originalAutoUpdate);
+ const baseServerUrl = $derived(settings?.baseServerUrl || 'localhost');
+
$effect(() => {
isLoading.deploying = false;
isLoading.stopping = false;
@@ -77,6 +79,57 @@
}
);
}
+
+ function getHostForService(service: any): string {
+ if (!service || !service.networkSettings?.Networks) return baseServerUrl;
+
+ const networks = service.networkSettings.Networks;
+ for (const networkName in networks) {
+ const network = networks[networkName];
+ if (network.Driver === 'macvlan' || network.Driver === 'ipvlan') {
+ if (network.IPAddress) return network.IPAddress;
+ }
+ }
+
+ return baseServerUrl;
+ }
+
+ // Define a more specific interface for the port type
+ interface Port {
+ PublicPort?: number;
+ PrivatePort?: number;
+ Type?: string;
+ [key: string]: any;
+ }
+
+ function getServicePortUrl(service: any, port: string | number | Port, protocol = 'http'): string {
+ const host = getHostForService(service);
+
+ if (typeof port === 'string') {
+ const parts = port.split('/');
+ const portNumber = parseInt(parts[0], 10);
+
+ if (parts.length > 1 && parts[1] === 'udp') {
+ protocol = 'udp';
+ }
+
+ return `${protocol}://${host}:${portNumber}`;
+ }
+
+ if (typeof port === 'number') {
+ return `${protocol}://${host}:${port}`;
+ }
+
+ if (port && typeof port === 'object') {
+ const portNumber = port.PublicPort || port.PrivatePort || 80;
+ if (port.Type) {
+ protocol = port.Type.toLowerCase() === 'tcp' ? 'http' : 'https';
+ }
+ return `${protocol}://${host}:${portNumber}`;
+ }
+
+ return `${protocol}://${host}:80`;
+ }
</script>
<div class="space-y-6 pb-8">
@@ -99,8 +152,18 @@
</Breadcrumb.Root>
<div class="mt-2 flex items-center gap-2">
- <h1 class="text-2xl font-bold tracking-tight">
+ <h1 class="text-2xl font-bold tracking-tight flex items-center gap-2">
{stack?.name || 'Stack Details'}
+ {#if stack && servicePorts && Object.keys(servicePorts).length > 0}
+ {#each Object.values(servicePorts) as ports}
+ {#each ports as port}
+ <a href={getServicePortUrl(stack, port)} target="_blank" rel="noopener noreferrer" class="text-xs bg-blue-500/10 text-blue-600 dark:text-blue-400 px-2 py-1 rounded-md hover:bg-blue-500/20 transition-colors flex items-center gap-1">
+ {port}
+ <ExternalLink class="h-3 w-3" />
+ </a>
+ {/each}
+ {/each}
+ {/if}
</h1>
</div>
</div>
@@ -241,28 +304,46 @@
{#each stack.services as service (service.id || service.name)}
{@const status = service.state?.Status || 'unknown'}
{@const variant = statusVariantMap[status.toLowerCase()] || 'gray'}
- <a href={service.id ? `/containers/${service.id}` : undefined} class={`flex items-center justify-between p-3 border rounded-md ${service.id ? 'hover:bg-muted/50 transition-colors cursor-pointer' : 'cursor-default'}`}>
- <div class="flex items-center gap-3">
- <div class="bg-muted rounded-md p-1">
- <Layers class="h-4 w-4" />
- </div>
- <div>
- <p class="font-medium">{service.name}</p>
- <p class="text-xs text-muted-foreground">
- {service.id ? service.id.substring(0, 12) : 'Not created'}
- </p>
+
+ {#if service.id}
+ <a href={`/containers/${service.id}`} class="flex flex-col p-3 border rounded-md hover:bg-muted/50 transition-colors">
+ <div class="flex items-center justify-between">
+ <div class="flex items-center gap-3">
+ <div class="bg-muted rounded-md p-1">
+ <Layers class="h-4 w-4" />
+ </div>
+ <div>
+ <p class="font-medium flex items-center gap-1">{service.name}</p>
+ <p class="text-xs text-muted-foreground">{service.id.substring(0, 12)}</p>
+ </div>
+ </div>
+ <div class="flex items-center gap-2">
+ <StatusBadge {variant} text={capitalizeFirstLetter(status)} />
+ <span class="text-xs text-blue-500 ml-2 flex items-center">
+ <span class="hidden sm:inline">View details</span>
+ <ArrowRight class="inline-block ml-1 h-3 w-3" />
+ </span>
+ </div>
</div>
- </div>
- <div class="flex items-center gap-2">
- <StatusBadge {variant} text={capitalizeFirstLetter(status)} />
- {#if service.id}
- <div class="text-xs text-blue-500 ml-2">
- <span class="hidden sm:inline">View details</span>
- <ArrowRight class="inline-block ml-1 h-3 w-3" />
+ </a>
+ {:else}
+ <div class="flex flex-col p-3 border rounded-md">
+ <div class="flex items-center justify-between">
+ <div class="flex items-center gap-3">
+ <div class="bg-muted rounded-md p-1">
+ <Layers class="h-4 w-4" />
+ </div>
+ <div>
+ <p class="font-medium flex items-center gap-1">{service.name}</p>
+ <p class="text-xs text-muted-foreground">Not created</p>
+ </div>
+ </div>
+ <div class="flex items-center gap-2">
+ <StatusBadge {variant} text={capitalizeFirstLetter(status)} />
</div>
- {/if}
+ </div>
</div>
- </a>
+ {/if}
{/each}
{:else}
<div class="text-center py-6 text-muted-foreground">