Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: improve loading speed of stack and container pages (#194)
Kyle Mendellccbbdb425033
diff --git a/src/lib/services/docker/container-service.ts b/src/lib/services/docker/container-service.tsindex 17fa49165..286666214 100644--- a/src/lib/services/docker/container-service.ts+++ b/src/lib/services/docker/container-service.ts@@ -5,6 +5,25 @@ import type { ServiceContainer } from '$lib/types/docker/container.type';import { NotFoundError, ConflictError, DockerApiError } from '$lib/types/errors';import type Docker from 'dockerode';+// Add a container cache with TTL+const containerCache = new Map();+const CONTAINER_CACHE_TTL = 15000; // 15 seconds - slightly shorter than stacks cache++/**+ * Helper function to invalidate container cache entries+ * @param {string} containerId - The ID of the container to invalidate (optional)+ */+function invalidateContainerCache(containerId?: string) {+ if (containerId) {+ // Remove specific container entry+ containerCache.delete(`container-${containerId}`);+ }++ // Always invalidate container lists as they're affected by any change+ containerCache.delete('list-containers-all');+ containerCache.delete('list-containers-running');+}+/*** This TypeScript function lists Docker containers and returns an array of ServiceContainer objects.* @param [all=true] - The `all` parameter in the `listContainers` function is a boolean parameter that@@ -16,10 +35,18 @@ import type Docker from 'dockerode';* extracted from the Docker containers retrieved using the Docker client.*/export async function listContainers(all = true): Promise<ServiceContainer[]> {+ const cacheKey = `list-containers-${all ? 'all' : 'running'}`;+ const cachedData = containerCache.get(cacheKey);++ // Return cached data if valid+ if (cachedData && Date.now() - cachedData.timestamp < CONTAINER_CACHE_TTL) {+ return cachedData.data;+ }+try {const docker = await getDockerClient();const containersData = await docker.listContainers({ all });- return containersData.map(+ const result = containersData.map((c): ServiceContainer => ({id: c.Id,names: c.Names,@@ -33,6 +60,14 @@ export async function listContainers(all = true): Promise<ServiceContainer[]> {ports: c.Ports}));++ // Cache the result+ containerCache.set(cacheKey, {+ data: result,+ timestamp: Date.now()+ });++ return result;} catch (error: unknown) {console.error('Docker Service: Error listing containers:', error);throw new Error(`Failed to list Docker containers using host "${dockerHost}".`);@@ -51,11 +86,20 @@ export async function listContainers(all = true): Promise<ServiceContainer[]> {* occurs during the process, it will be caught and handled accordingly. If the*/export async function getContainer(containerId: string) {+ const cacheKey = `container-${containerId}`;+ const cachedData = containerCache.get(cacheKey);++ // Return cached data if valid+ if (cachedData && Date.now() - cachedData.timestamp < CONTAINER_CACHE_TTL) {+ return cachedData.data;+ }+try {const docker = await getDockerClient();const container = docker.getContainer(containerId);const inspectData = await container.inspect();- return {++ const result = {id: inspectData.Id,name: inspectData.Name.substring(1),created: inspectData.Created,@@ -68,6 +112,14 @@ export async function getContainer(containerId: string) {mounts: inspectData.Mounts,labels: inspectData.Config.Labels};++ // Cache the result+ containerCache.set(cacheKey, {+ data: result,+ timestamp: Date.now()+ });++ return result;} catch (error: unknown) {console.error(`Docker Service: Error getting container ${containerId}:`, error);if (error instanceof Error && 'statusCode' in error && (error as { statusCode?: number }).statusCode === 404) {@@ -88,6 +140,9 @@ export async function startContainer(containerId: string): Promise<void> {const docker = await getDockerClient();const container = docker.getContainer(containerId);await container.start();++ // Invalidate cache for this container+ invalidateContainerCache(containerId);} catch (error: unknown) {console.error(`Docker Service: Error starting container ${containerId}:`, error);const errorMessage = error instanceof Error ? error.message : String(error);@@ -106,6 +161,9 @@ export async function stopContainer(containerId: string): Promise<void> {const docker = await getDockerClient();const container = docker.getContainer(containerId);await container.stop();++ // Invalidate cache for this container+ invalidateContainerCache(containerId);} catch (error: unknown) {console.error(`Docker Service: Error stopping container ${containerId}:`, error);const errorMessage = error instanceof Error ? error.message : String(error);@@ -123,6 +181,9 @@ export async function restartContainer(containerId: string): Promise<void> {const docker = await getDockerClient();const container = docker.getContainer(containerId);await container.restart();++ // Invalidate cache for this container+ invalidateContainerCache(containerId);} catch (error: unknown) {console.error(`Docker Service: Error restarting container ${containerId}:`, error);const errorMessage = error instanceof Error ? error.message : String(error);@@ -151,6 +212,9 @@ export async function removeContainer(containerId: string, force = false): Promi// Pass the force option directly to dockerode's remove methodawait container.remove({ force });+ // Invalidate cache for this container+ invalidateContainerCache(containerId);+console.log(`Docker Service: Container ${containerId} removed successfully (force=${force}).`);} catch (error: unknown) {console.error(`Docker Service: Error removing container ${containerId} (force=${force}):`, error);@@ -329,6 +393,9 @@ export async function createContainer(config: ContainerConfig) {// Get the container detailsconst containerInfo = await container.inspect();+ // Invalidate container cache+ invalidateContainerCache();+return {id: containerInfo.Id,name: containerInfo.Name.substring(1),@@ -431,6 +498,19 @@ export async function getContainerStats(containerId: string): Promise<Docker.Con* @returns {Promise<ServiceContainer>} Information about the newly created and started container.* @throws {DockerApiError} If any step fails.*/++// Define the Mount interface+interface DockerMount {+ Type: string;+ Source: string;+ Destination: string;+ RW: boolean;+ Name?: string;+ Driver?: string;+ Mode?: string;+ Propagation?: string;+}+export async function recreateContainer(containerId: string): Promise<ServiceContainer> {const docker = await getDockerClient();let originalContainer = null;@@ -453,7 +533,7 @@ export async function recreateContainer(containerId: string): Promise<ServiceConHostConfig: {PortBindings: originalContainer.networkSettings?.Ports || {},NetworkMode: originalContainer.networkSettings?.Networks?.bridge ? 'bridge' : Object.keys(originalContainer.networkSettings?.Networks || {})[0] || undefined,- Binds: originalContainer.mounts?.filter((mount) => mount.Type === 'bind' || mount.Type === 'volume').map((mount) => `${mount.Source}:${mount.Destination}${mount.RW ? '' : ':ro'}`)+ Binds: originalContainer.mounts?.filter((mount: DockerMount) => mount.Type === 'bind' || mount.Type === 'volume').map((mount: DockerMount) => `${mount.Source}:${mount.Destination}${mount.RW ? '' : ':ro'}`)},Cmd: originalContainer.config.Cmd,Entrypoint: originalContainer.config.Entrypoint,@@ -467,11 +547,26 @@ export async function recreateContainer(containerId: string): Promise<ServiceCon// If we need to add custom network configuration for non-default networksif (originalContainer.networkSettings?.Networks) {- const networks = Object.entries(originalContainer.networkSettings.Networks);+ interface DockerNetworkConfig {+ IPAddress?: string;+ IPPrefixLen?: number;+ Gateway?: string;+ MacAddress?: string;+ [key: string]: any;+ }++ type NetworkEntry = [string, DockerNetworkConfig];++ const networks = Object.entries(originalContainer.networkSettings.Networks) as NetworkEntry[];+// If container uses a non-default network, set it upif (networks.length > 0 && networks[0][0] !== 'bridge') {const [networkName, networkConfig] = networks[0];- createOptions.HostConfig!.NetworkMode = networkName;++ // Set the network mode in the host config+ if (createOptions.HostConfig) {+ createOptions.HostConfig.NetworkMode = networkName;+ }// If network has specific IP assignments, add NetworkingConfigif (networkConfig.IPAddress) {@@ -511,6 +606,9 @@ export async function recreateContainer(containerId: string): Promise<ServiceConconsole.log(`Recreating container ${containerId}: Starting new container ${newContainer.id}...`);await startContainer(newContainer.id);+ // Invalidate container cache after recreation+ invalidateContainerCache();+console.log(`Recreating container ${containerId}: Successfully recreated and started as ${newContainer.id}.`);// 6. Get basic container info to return as ServiceContainerdiff --git a/src/lib/services/docker/stack-service.ts b/src/lib/services/docker/stack-service.tsindex 217bb33e5..d03518810 100644--- a/src/lib/services/docker/stack-service.ts+++ b/src/lib/services/docker/stack-service.ts@@ -12,6 +12,9 @@ import type { Stack, StackService, StackUpdate } from '$lib/types/docker/stack.tTypeScript. */let STACKS_DIR = '';+const stackCache = new Map();+const CACHE_TTL = 30000; // 30 seconds+/*** The function `initComposeService` initializes the stacks directory and ensures its existence in a* TypeScript application.@@ -316,6 +319,13 @@ async function getStackServices(stackId: string, composeContent: string): Promis* `updatedAt`.*/export async function loadComposeStacks(): Promise<Stack[]> {+ const cacheKey = 'compose-stacks';+ const cachedData = stackCache.get(cacheKey);++ if (cachedData && Date.now() - cachedData.timestamp < CACHE_TTL) {+ return cachedData.data;+ }+const stacksDir = await ensureStacksDir();try {@@ -386,6 +396,11 @@ export async function loadComposeStacks(): Promise<Stack[]> {});}+ stackCache.set(cacheKey, {+ data: stacks,+ timestamp: Date.now()+ });+return stacks;} catch (err) {console.error('Error loading stacks from STACKS_DIR:', err);@@ -438,11 +453,12 @@ export async function createStack(name: string, composeContent: string, envConteconst dirStat = await fs.stat(stackDir);- return {- id: uniqueDirName, // ID is the directory name- name: uniqueDirName, // Name is also the directory name+ // Create the stack object+ const newStack: Stack = {+ id: uniqueDirName,+ name: uniqueDirName,serviceCount: serviceCount,- runningCount: 0, // New stacks are initially stopped+ runningCount: 0,status: 'stopped',createdAt: dirStat.birthtime.toISOString(),updatedAt: dirStat.mtime.toISOString(),@@ -450,6 +466,11 @@ export async function createStack(name: string, composeContent: string, envConteenvContent: envContent || '',isExternal: false};++ // Invalidate the cache to ensure the new stack appears immediately+ stackCache.delete('compose-stacks');++ return newStack;}/**@@ -558,6 +579,9 @@ export async function updateStack(currentStackId: string, updates: StackUpdate):await Promise.all(promises);}+ // Invalidate the cache after any update+ stackCache.delete('compose-stacks');+// 3. Return the final stack stateif (stackAfterRename && !contentUpdated) {// Only a rename occurred, no subsequent content changes in this call.@@ -607,11 +631,60 @@ export async function startStack(stackId: string): Promise<boolean> {await startStackWithExternalNetworks(stackId, composeData, stackDir);} else {// Standard approach for stacks without external networks+ const docker = await getDockerClient();+ const imagePullPromises = Object.entries(composeData.services || {})+ .filter(([_, serviceConfig]) => (serviceConfig as any).image)+ .map(async ([serviceName, serviceConfig]) => {+ const serviceImage = (serviceConfig as any).image;+ console.log(`Pulling image for service ${serviceName}: ${serviceImage}`);+ try {+ await new Promise((resolve, reject) => {+ interface PullError extends Error {}+ interface PullStream extends NodeJS.ReadableStream {}+ interface ProgressEvent {+ status?: string;+ progress?: string;+ }+ interface ProgressOutput {}++ docker.pull(serviceImage, (pullError: PullError | null, stream: PullStream) => {+ if (pullError) {+ reject(pullError);+ return;+ }+ docker.modem.followProgress(+ stream,+ (progressError: Error | null, output: ProgressOutput[]) => {+ if (progressError) {+ reject(progressError);+ } else {+ console.log(`Successfully pulled image: ${serviceImage}`);+ resolve(output);+ }+ },+ (event: ProgressEvent) => {+ if (event.progress) {+ console.log(`${serviceImage}: ${event.status} ${event.progress}`);+ } else if (event.status) {+ console.log(`${serviceImage}: ${event.status}`);+ }+ }+ );+ });+ });+ } catch (pullErr) {+ console.warn(`Warning: Failed to pull image ${serviceImage} for service ${serviceName}:`, pullErr);+ }+ });++ await Promise.all(imagePullPromises);+const compose = await getComposeInstance(stackId);- await compose.pull();await compose.up();}+ // Invalidate the cache at the end+ stackCache.delete('compose-stacks');return true;} catch (err) {console.error(`Error starting stack ${stackId} from directory ${stackDir}:`, err);@@ -1004,6 +1077,10 @@ export async function stopStack(stackId: string): Promise<boolean> {}console.log(`Stack ${stackId} processing complete. Stopped: ${stoppedCount}, Removed: ${removedCount}.`);++ // Invalidate the cache after stopping+ stackCache.delete('compose-stacks');+return true; // Indicate overall success even if some individual steps had errors (adjust if needed)} catch (err: unknown) {// Log the specific error@@ -1052,6 +1129,10 @@ export async function restartStack(stackId: string): Promise<boolean> {const compose = await getComposeInstance(stackId);await compose.up();console.log(`Stack ${stackId} started.`);++ // Invalidate the cache after restarting+ stackCache.delete('compose-stacks');+return true;} catch (err: unknown) {console.error(`Error restarting stack ${stackId} from directory ${stackDir}:`, err);@@ -1162,6 +1243,9 @@ export async function removeStack(stackId: string): Promise<boolean> {throw new Error(`Failed to remove stack directory`);}+ // Invalidate the cache after removing a stack+ stackCache.delete('compose-stacks');+return true;} catch (err: unknown) {// Catch errors from stopStack or directory removal@@ -1233,6 +1317,9 @@ export async function renameStack(currentStackId: string, newName: string): Promawait fs.rename(currentStackDir, newStackDir);console.log(`Stack directory for '${currentStackId}' successfully renamed to '${newUniqueDirName}'.`);+ // Invalidate the cache after renaming+ stackCache.delete('compose-stacks');+// The stack was stopped. When it's started next using `startStack(newUniqueDirName)`,// dockerode-compose will use `newUniqueDirName` as the project name,// effectively creating a "new" project from Docker's perspective with the existing files.@@ -1456,6 +1543,9 @@ ${yaml.dump({ services }, { indent: 2 }).substring('services:'.length).trimStart// envContent will remain undefined in this case.}+ // Invalidate the cache after importing+ stackCache.delete('compose-stacks');+// 5. Create a new stack in Arcane's managed stacksreturn await createStack(stackId, normalizeHealthcheckTest(composeContent), envContent); // Pass envContent here}diff --git a/src/routes/containers/[id]/+page.svelte b/src/routes/containers/[id]/+page.svelteindex 0f57de1ea..7acfc84b4 100644--- a/src/routes/containers/[id]/+page.svelte+++ b/src/routes/containers/[id]/+page.svelte@@ -11,10 +11,27 @@import { formatBytes } from '$lib/utils/bytes.util';import type Docker from 'dockerode';import type { ContainerInspectInfo } from 'dockerode';+ import type { ContainerDetails } from '$lib/types/docker/container.type';import StatusBadge from '$lib/components/badges/status-badge.svelte';import * as Tabs from '$lib/components/ui/tabs/index.js';import { onDestroy } from 'svelte';+ // Define the network config interface to match what Docker actually returns+ interface NetworkConfig {+ IPAddress?: string;+ IPPrefixLen?: number;+ Gateway?: string;+ MacAddress?: string;+ Aliases?: string[];+ Links?: string[];+ [key: string]: any;+ }++ // Type assertion helper for network settings+ function ensureNetworkConfig(config: any): NetworkConfig {+ return config as NetworkConfig;+ }+let { data }: { data: PageData } = $props();let { container, logs: initialLogsFromServer, stats } = $derived(data);@@ -430,7 +447,7 @@</div><div class="flex flex-wrap items-center gap-2"><span class="text-xs text-muted-foreground">→</span>- {#if hostBindings && hostBindings.length > 0}+ {#if Array.isArray(hostBindings) && hostBindings.length > 0}{#each hostBindings as binding (binding.HostIp + ':' + binding.HostPort)}<Badge variant="outline" class="font-mono truncate max-w-[150px]" title="{binding.HostIp || '0.0.0.0'}:{binding.HostPort}">{binding.HostIp || '0.0.0.0'}:{binding.HostPort}@@ -482,7 +499,8 @@<Card.Content>{#if container.networkSettings?.Networks && Object.keys(container.networkSettings.Networks).length > 0}<div class="space-y-4">- {#each Object.entries(container.networkSettings.Networks) as [networkName, networkConfig] (networkName)}+ {#each Object.entries(container.networkSettings.Networks) as [networkName, rawNetworkConfig] (networkName)}+ {@const networkConfig = ensureNetworkConfig(rawNetworkConfig)}<div class="rounded-md bg-muted/40 p-3"><div class="text-sm font-medium">{networkName}</div><div class="grid grid-cols-1 sm:grid-cols-2 gap-x-4 gap-y-2 mt-2">diff --git a/src/routes/stacks/+page.server.ts b/src/routes/stacks/+page.server.tsindex 926095284..02f3edad9 100644--- a/src/routes/stacks/+page.server.ts+++ b/src/routes/stacks/+page.server.ts@@ -3,8 +3,7 @@ import type { PageServerLoad } from './$types';import { tryCatch } from '$lib/utils/try-catch';export const load: PageServerLoad = async () => {- const managedResult = await tryCatch(loadComposeStacks());- const externalResult = await tryCatch(discoverExternalStacks());+ const [managedResult, externalResult] = await Promise.all([tryCatch(loadComposeStacks()), tryCatch(discoverExternalStacks())]);if (managedResult.error || externalResult.error) {console.error('Failed to load stacks:', managedResult.error || externalResult.error);