Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
feat: application settings page
Kyle Mendell10084f832b11
diff --git a/app-settings.json b/app-settings.jsonnew file mode 100644index 000000000..c9c46a89e--- /dev/null+++ b/app-settings.json@@ -0,0 +1,6 @@+{+ "dockerHost": "unix:///var/run/docker.sock",+ "autoRefresh": true,+ "refreshInterval": 10,+ "darkMode": true+}\ No newline at end of filediff --git a/src/lib/services/docker-service.ts b/src/lib/services/docker-service.tsindex 308ada5b7..2b4c1c5a0 100644--- a/src/lib/services/docker-service.ts+++ b/src/lib/services/docker-service.ts@@ -1,70 +1,134 @@import Docker from "dockerode";+import fs from "fs"; // Use synchronous fs for initial load+import path from "path";-// This instance can be used for general operations once settings are loaded/applied-// For now, it might default to the standard connection-// TODO: Initialize this based on saved settings later-const defaultDocker = new Docker();+// Define Settings Type (can be shared with +page.server.ts if moved to a types file)+type SettingsData = {+ dockerHost: string;+ autoRefresh: boolean;+ refreshInterval: number;+ darkMode: boolean;+};++// --- Load Initial Settings ---+const SETTINGS_FILE = path.resolve("./app-settings.json");+const DEFAULT_SETTINGS: SettingsData = {+ dockerHost: "unix:///var/run/docker.sock",+ autoRefresh: true,+ refreshInterval: 10,+ darkMode: true,+};++function loadInitialSettings(): SettingsData {+ try {+ // Use synchronous read for initial module load simplicity+ if (fs.existsSync(SETTINGS_FILE)) {+ const data = fs.readFileSync(SETTINGS_FILE, "utf-8");+ const settings = JSON.parse(data);+ console.log("Docker Service: Loaded initial settings:", settings);+ return { ...DEFAULT_SETTINGS, ...settings }; // Merge with defaults+ } else {+ console.log("Docker Service: Settings file not found, using defaults.");+ return DEFAULT_SETTINGS;+ }+ } catch (error) {+ console.error("Docker Service: Error loading initial settings:", error);+ return DEFAULT_SETTINGS; // Fallback to defaults+ }+}++const initialSettings = loadInitialSettings();+// --- End Load Initial Settings ---/**- * Creates a Dockerode instance options object from a host string.- * Handles unix sockets and TCP addresses.+ * Creates Dockerode options from a host string.*/-function getDockerOpts(host?: string): Docker.DockerOptions | undefined {- if (!host) {- return undefined; // Use default connection methods- }- if (host.startsWith("unix://")) {- return { socketPath: host.substring(7) };- }- if (host.startsWith("tcp://")) {+function getDockerOpts(host?: string): Docker.DockerOptions {+ const targetHost = host || initialSettings.dockerHost; // Use provided host or loaded setting+ const options: Docker.DockerOptions = {};++ if (targetHost.startsWith("unix://")) {+ options.socketPath = targetHost.substring(7);+ } else if (+ targetHost.startsWith("tcp://") ||+ targetHost.startsWith("http://") ||+ targetHost.startsWith("https://")+ ) {try {- const url = new URL(host);- return {- host: url.hostname,- port: url.port || 2375, // Default Docker TCP port- // TODO: Add support for TLS options if needed (protocol: 'https')- };+ const url = new URL(targetHost);+ options.host = url.hostname;+ options.port = url.port || (url.protocol === "https:" ? "2376" : "2375");+ options.protocol = url.protocol.slice(0, -1) as "https" | "http";+ // TODO: Add TLS options if needed based on protocol or settings} catch (e) {- console.error("Invalid Docker TCP host URL:", host);- return { host: "invalid-host" }; // Force failure+ console.error("Invalid Docker host URL format:", targetHost, e);+ // Return options that will likely fail connection, signaling an issue+ return { host: "invalid-host-format" };}+ } else {+ // Assume it's a socket path if no protocol+ options.socketPath = targetHost;}- // Assume it's a socket path if no protocol is specified- return { socketPath: host };+ return options;}+// --- Initialize the shared defaultDocker instance ---+// Use let so it can be reassigned by updateDefaultDockerInstance+let defaultDocker = new Docker(getDockerOpts());+console.log(+ `Docker Service: Initialized defaultDocker with options:`,+ getDockerOpts()+);+// --- End Initialization ---+/**- * Gets basic Docker system information, optionally testing a specific host.- * @param hostToTest - The Docker host string (e.g., "unix:///var/run/docker.sock" or "tcp://localhost:2375") to test. If undefined, uses the default connection.+ * Updates the shared default Docker instance used by the service.+ * @param host - The new Docker host string.*/-export async function getDockerInfo(hostToTest?: string) {+export function updateDefaultDockerInstance(host: string) {try {- // Create a specific instance for testing if hostToTest is provided- const dockerInstance = hostToTest- ? new Docker(getDockerOpts(hostToTest))- : defaultDocker;- return await dockerInstance.info();+ const newOptions = getDockerOpts(host);+ // Re-assign the module-level defaultDocker variable+ defaultDocker = new Docker(newOptions);+ // Update the settings object in memory (optional, but keeps it consistent)+ initialSettings.dockerHost = host;+ console.log(+ "Docker Service: Updated defaultDocker instance for host:",+ host+ );+ console.log("Docker Service: New options:", newOptions);+ } catch (e) {+ console.error(+ `Docker Service: Failed to update default Docker instance for host: ${host}`,+ e+ );+ // Optionally revert to a known good state or throw+ }+}++/**+ * Gets Docker system information.+ * Uses the *shared* defaultDocker instance unless a specific host is passed for testing.+ * @param host - Optional Docker host URL to connect to for *testing*. If not provided, uses the default shared instance.+ */+export async function getDockerInfo(host?: string): Promise<Docker.Info> {+ // Use a temporary instance ONLY if a specific host is provided for testing+ const dockerInstance = host ? new Docker(getDockerOpts(host)) : defaultDocker;+ const targetHost = host || initialSettings.dockerHost; // For logging++ try {+ const info = await dockerInstance.info();+ return info;} catch (error: any) {console.error(- `Error getting Docker info (Host: ${hostToTest || "default"}):`,- error.message+ `Docker Service: Error getting Docker info for host "${targetHost}":`,+ error.message || error+ );+ throw new Error(+ `Failed to connect to Docker Engine at "${targetHost}". ${+ error.message || ""+ }`);- // Make error more specific- let message = `Failed to connect to Docker Engine`;- if (hostToTest) {- message += ` at ${hostToTest}`;- }- if (error.code === "ENOENT" || error.message.includes("ENOENT")) {- message += `. Socket/Path not found.`;- } else if (- error.code === "ECONNREFUSED" ||- error.message.includes("ECONNREFUSED")- ) {- message += `. Connection refused. Is Docker running?`;- } else if (error.message) {- message += `. Error: ${error.message}`;- }- throw new Error(message);}}@@ -84,11 +148,11 @@ export type ServiceContainer = {/*** Lists Docker containers.- * @param all - Whether to show all containers (including stopped). Defaults to true.+ * Uses the shared defaultDocker instance.*/-// Add the return type annotationexport async function listContainers(all = true): Promise<ServiceContainer[]> {try {+ // Uses the potentially updated defaultDockerconst containers = await defaultDocker.listContainers({ all });// Ensure the mapping matches the ServiceContainer typereturn containers.map(@@ -106,18 +170,20 @@ export async function listContainers(all = true): Promise<ServiceContainer[]> {}));} catch (error: any) {- console.error("Error listing containers:", error);- // Rethrow or handle as appropriate for your app's error strategy- throw new Error("Failed to list Docker containers.");+ console.error("Docker Service: Error listing containers:", error);+ throw new Error(+ `Failed to list Docker containers using host "${initialSettings.dockerHost}".`+ );}}/*** Gets details for a specific container.- * @param containerId - The ID of the container.+ * Uses the shared defaultDocker instance.*/export async function getContainer(containerId: string) {try {+ // Uses the potentially updated defaultDockerconst container = defaultDocker.getContainer(containerId);const inspectData = await container.inspect();// Return relevant details@@ -135,11 +201,16 @@ export async function getContainer(containerId: string) {// Add more fields as needed};} catch (error: any) {- console.error(`Error getting container ${containerId}:`, error);+ console.error(+ `Docker Service: Error getting container ${containerId}:`,+ error+ );if (error.statusCode === 404) {return null; // Container not found}- throw new Error(`Failed to get container details for ${containerId}.`);+ throw new Error(+ `Failed to get container details for ${containerId} using host "${initialSettings.dockerHost}".`+ );}}@@ -152,10 +223,14 @@ export async function startContainer(containerId: string): Promise<void> {const container = defaultDocker.getContainer(containerId);await container.start();} catch (error: any) {- console.error(`Error starting container ${containerId}:`, error);- // Provide more context if possible (e.g., container already started)+ console.error(+ `Docker Service: Error starting container ${containerId}:`,+ error+ );throw new Error(- `Failed to start container ${containerId}. ${error.message || ""}`+ `Failed to start container ${containerId} using host "${+ initialSettings.dockerHost+ }". ${error.message || ""}`);}}@@ -169,10 +244,14 @@ export async function stopContainer(containerId: string): Promise<void> {const container = defaultDocker.getContainer(containerId);await container.stop();} catch (error: any) {- console.error(`Error stopping container ${containerId}:`, error);- // Provide more context (e.g., container not running)+ console.error(+ `Docker Service: Error stopping container ${containerId}:`,+ error+ );throw new Error(- `Failed to stop container ${containerId}. ${error.message || ""}`+ `Failed to stop container ${containerId} using host "${+ initialSettings.dockerHost+ }". ${error.message || ""}`);}}@@ -186,9 +265,14 @@ export async function restartContainer(containerId: string): Promise<void> {const container = defaultDocker.getContainer(containerId);await container.restart();} catch (error: any) {- console.error(`Error restarting container ${containerId}:`, error);+ console.error(+ `Docker Service: Error restarting container ${containerId}:`,+ error+ );throw new Error(- `Failed to restart container ${containerId}. ${error.message || ""}`+ `Failed to restart container ${containerId} using host "${+ initialSettings.dockerHost+ }". ${error.message || ""}`);}}@@ -206,8 +290,10 @@ export async function removeContainer(const container = defaultDocker.getContainer(containerId);await container.remove({ force });} catch (error: any) {- console.error(`Error removing container ${containerId}:`, error);- // Provide more context (e.g., conflict, container not found)+ console.error(+ `Docker Service: Error removing container ${containerId}:`,+ error+ );if (error.statusCode === 404) {throw new Error(`Container ${containerId} not found.`);}@@ -217,7 +303,9 @@ export async function removeContainer();}throw new Error(- `Failed to remove container ${containerId}. ${error.message || ""}`+ `Failed to remove container ${containerId} using host "${+ initialSettings.dockerHost+ }". ${error.message || ""}`);}}@@ -241,49 +329,44 @@ export async function getContainerLogs(try {const container = defaultDocker.getContainer(containerId);- // Set default options if not providedconst logOptions = {- tail: options.tail === "all" ? undefined : options.tail || 100, // Default to last 100 lines, undefined for 'all'- stdout: options.stdout !== false, // Include stdout by default- stderr: options.stderr !== false, // Include stderr by default- follow: false, // Don't stream by default (won't work well with SSR)- timestamps: true, // Include timestamps- since: options.since || 0, // Get all logs by default+ tail: options.tail === "all" ? undefined : options.tail || 100,+ stdout: options.stdout !== false,+ stderr: options.stderr !== false,+ follow: false,+ timestamps: true,+ since: options.since || 0,until: options.until || undefined,};- // Get the logs as a Bufferconst logsBuffer = await container.logs(logOptions);-- // Convert the Buffer to a string and handle the formatlet logString = logsBuffer.toString();- // Process the log string to handle Docker log format- // Docker prefixes each line with 8 bytes: first byte is 01 (stdout) or 02 (stderr),- // followed by 3 bytes of 0s, then 4 bytes for the sizeif (logOptions.stdout || logOptions.stderr) {- // This is a simple approach - split by lines and remove Docker headersconst lines = logString.split("\n");const processedLines = lines.map((line) => {- // Skip empty linesif (!line) return "";- // Remove the 8-byte header if present (check line length)if (line.length > 8) {return line.substring(8);}return line;})- .filter(Boolean); // Remove empty lines+ .filter(Boolean);logString = processedLines.join("\n");}return logString;} catch (error: any) {- console.error(`Error getting logs for container ${containerId}:`, error);+ console.error(+ `Docker Service: Error getting logs for container ${containerId}:`,+ error+ );throw new Error(- `Failed to get logs for container ${containerId}. ${error.message || ""}`+ `Failed to get logs for container ${containerId} using host "${+ initialSettings.dockerHost+ }". ${error.message || ""}`);}}@@ -297,7 +380,6 @@ export type ServiceImage = {size: number;virtualSize: number;labels: { [label: string]: string } | undefined;- // Add primary repo and tag for easier displayrepo: string;tag: string;};@@ -305,12 +387,10 @@ export type ServiceImage = {/*** Lists Docker images.*/-// Add the return type annotationexport async function listImages(): Promise<ServiceImage[]> {try {- const images = await defaultDocker.listImages({ all: false }); // Usually only show non-intermediate images+ const images = await defaultDocker.listImages({ all: false });- // Function to parse repo and tagconst parseRepoTag = (tag: string | undefined): { repo: string; tag: string } => {@@ -319,7 +399,7 @@ export async function listImages(): Promise<ServiceImage[]> {}const parts = tag.split(":");if (parts.length === 1) {- return { repo: parts[0], tag: "latest" }; // Assume latest if no tag+ return { repo: parts[0], tag: "latest" };}const tagPart = parts.pop() || "latest";const repoPart = parts.join(":");@@ -341,8 +421,10 @@ export async function listImages(): Promise<ServiceImage[]> {};});} catch (error: any) {- console.error("Error listing images:", error);- throw new Error("Failed to list Docker images.");+ console.error("Docker Service: Error listing images:", error);+ throw new Error(+ `Failed to list Docker images using host "${initialSettings.dockerHost}".`+ );}}@@ -352,9 +434,9 @@ export type ServiceNetwork = {name: string;driver: string;scope: string;- subnet: string | null; // Extract the first subnet if available- gateway: string | null; // Extract the first gateway if available- created: string; // Dockerode returns date as string+ subnet: string | null;+ gateway: string | null;+ created: string;};/**@@ -369,15 +451,16 @@ export async function listNetworks(): Promise<ServiceNetwork[]> {name: net.Name,driver: net.Driver,scope: net.Scope,- // Safely access the first IPAM config and its subnet/gatewaysubnet: net.IPAM?.Config?.[0]?.Subnet ?? null,gateway: net.IPAM?.Config?.[0]?.Gateway ?? null,- created: net.Created, // Keep as string or parse if needed+ created: net.Created,}));} catch (error: any) {- console.error("Error listing networks:", error);- throw new Error("Failed to list Docker networks.");+ console.error("Docker Service: Error listing networks:", error);+ throw new Error(+ `Failed to list Docker networks using host "${initialSettings.dockerHost}".`+ );}}@@ -385,7 +468,7 @@ export async function listNetworks(): Promise<ServiceNetwork[]> {export type ServiceVolume = {name: string;driver: string;- scope: string; // Usually 'local' or 'global'+ scope: string;mountpoint: string;labels: { [label: string]: string } | null;};@@ -395,9 +478,8 @@ export type ServiceVolume = {*/export async function listVolumes(): Promise<ServiceVolume[]> {try {- // The listVolumes response structure is slightly differentconst volumeResponse = await defaultDocker.listVolumes();- const volumes = volumeResponse.Volumes || []; // Access the Volumes array+ const volumes = volumeResponse.Volumes || [];return volumes.map((vol): ServiceVolume => ({@@ -409,8 +491,10 @@ export async function listVolumes(): Promise<ServiceVolume[]> {}));} catch (error: any) {- console.error("Error listing volumes:", error);- throw new Error("Failed to list Docker volumes.");+ console.error("Docker Service: Error listing volumes:", error);+ throw new Error(+ `Failed to list Docker volumes using host "${initialSettings.dockerHost}".`+ );}}diff --git a/src/routes/settings/+page.server.ts b/src/routes/settings/+page.server.tsindex bc23fbf5b..1cc932fa7 100644--- a/src/routes/settings/+page.server.ts+++ b/src/routes/settings/+page.server.ts@@ -1,7 +1,8 @@import type { PageServerLoad, Actions } from "./$types";import { fail } from "@sveltejs/kit";+import { updateDefaultDockerInstance } from "$lib/services/docker-service";-// Define a type for settings (optional but good practice)+// Define a type for settingstype SettingsData = {dockerHost: string;autoRefresh: boolean;@@ -9,15 +10,52 @@ type SettingsData = {darkMode: boolean;};-export const load: PageServerLoad = async () => {- // TODO: Load actual saved settings here instead of defaults- const currentSettings: SettingsData = {- dockerHost: "unix:///var/run/docker.sock",+// --- Persistence Layer (Example using simple file - replace with your actual storage) ---+import fs from "fs/promises";+import path from "path";+const SETTINGS_FILE = path.resolve("./app-settings.json"); // Store settings in project root (adjust path as needed)++async function loadSettingsFromFile(): Promise<SettingsData> {+ const defaults: SettingsData = {+ dockerHost: "unix:///var/run/docker.sock", // Default Docker hostautoRefresh: true,refreshInterval: 10,darkMode: true,};- // Return the raw settings data+ try {+ const data = await fs.readFile(SETTINGS_FILE, "utf-8");+ return JSON.parse(data);+ } catch (error: any) {+ if (error.code === "ENOENT") {+ console.log("Settings file not found, using defaults.");+ // Optionally save defaults if file doesn't exist+ // await saveSettingsToFile(defaults);+ return defaults;+ }+ console.error("Error loading settings:", error);+ return defaults; // Fallback to defaults on other errors+ }+}++async function saveSettingsToFile(settings: SettingsData): Promise<void> {+ try {+ await fs.writeFile(+ SETTINGS_FILE,+ JSON.stringify(settings, null, 2),+ "utf-8"+ );+ console.log("Settings saved to:", SETTINGS_FILE);+ } catch (error) {+ console.error("Error saving settings:", error);+ throw new Error("Failed to save settings."); // Propagate error+ }+}+// --- End Persistence Layer ---++export const load: PageServerLoad = async () => {+ // *** Load actual saved settings here ***+ const currentSettings = await loadSettingsFromFile();+ console.log("Loaded settings:", currentSettings);return { settings: currentSettings };};@@ -25,25 +63,25 @@ export const actions: Actions = {default: async ({ request }) => {const formData = await request.formData();- // Manually get and parse form dataconst dockerHost = formData.get("dockerHost") as string;- const autoRefresh = formData.get("autoRefresh") === "on"; // Checkbox/Switch value is 'on' or null+ const autoRefresh = formData.get("autoRefresh") === "on";const refreshIntervalStr = formData.get("refreshInterval") as string;const darkMode = formData.get("darkMode") === "on";- // Basic validation (optional)if (!dockerHost) {return fail(400, {error: "Docker host cannot be empty.",- values: Object.fromEntries(formData),+ values: Object.fromEntries(formData), // Return submitted values on error});}let refreshInterval = parseInt(refreshIntervalStr, 10);if (isNaN(refreshInterval) || refreshInterval < 5 || refreshInterval > 60) {- refreshInterval = 10; // Default or handle error- // Optionally return fail:- // return fail(400, { error: "Invalid refresh interval.", values: Object.fromEntries(formData) });+ // Return fail to show validation error in UI+ return fail(400, {+ error: "Refresh interval must be between 5 and 60 seconds.",+ values: Object.fromEntries(formData),+ });}const updatedSettings: SettingsData = {@@ -53,11 +91,21 @@ export const actions: Actions = {darkMode,};- // TODO: Persist the updated settings- console.log("Saving settings:", updatedSettings);+ try {+ // *** Persist the updated settings ***+ await saveSettingsToFile(updatedSettings);++ // *** Optional: Update the running server's Docker instance immediately ***+ updateDefaultDockerInstance(updatedSettings.dockerHost);- // You can return the updated settings or a success message- // Returning the settings allows the page to update if needed- return { success: true, settings: updatedSettings };+ // Return success and the *updated* settings+ // This updates the `form.settings` prop in the page component+ return { success: true, settings: updatedSettings };+ } catch (error: any) {+ return fail(500, {+ error: error.message || "Failed to save settings.",+ values: Object.fromEntries(formData),+ });+ }},};diff --git a/src/routes/settings/+page.svelte b/src/routes/settings/+page.svelteindex b93a9e303..aadfbbc36 100644--- a/src/routes/settings/+page.svelte+++ b/src/routes/settings/+page.svelte@@ -14,42 +14,22 @@import * as Switch from "$lib/components/ui/switch/index.js";import { enhance } from "$app/forms";- // Use $props() for component inputslet { data, form }: { data: PageData; form: ActionData } = $props();- // Derive initial settings from props, reacting if form action updates props- let initialSettings = $derived(form?.settings ?? data.settings);-- // Use $state for mutable component state bound to inputs- let dockerHost = $state("");- let autoRefresh = $state(false);- let refreshInterval = $state(30);- let darkMode = $state(false);+ // Initialize state variables *only* from the initial data load+ let dockerHost = $state(data.settings.dockerHost);+ let autoRefresh = $state(data.settings.autoRefresh);+ let refreshInterval = $state(data.settings.refreshInterval);+ let darkMode = $state(data.settings.darkMode);let isSubmitting = $state(false);let testStatus: "idle" | "testing" | "success" | "error" = $state("idle");let testMessage: string | null = $state(null);- // Effect to update local state when initialSettings (derived from props) changes- $effect(() => {- // Avoid resetting if the values are already the same (prevents potential loops)- if (dockerHost !== initialSettings.dockerHost)- dockerHost = initialSettings.dockerHost;- if (autoRefresh !== initialSettings.autoRefresh)- autoRefresh = initialSettings.autoRefresh;- if (refreshInterval !== initialSettings.refreshInterval)- refreshInterval = initialSettings.refreshInterval;- if (darkMode !== initialSettings.darkMode)- darkMode = initialSettings.darkMode;- });-- // Effect to reset test status when dockerHost input changes+ // Keep the effect for resetting test status when dockerHost input changes$effect(() => {- // This effect now correctly tracks the $state variable 'dockerHost'- // Need to access dockerHost to create the dependency- const currentHost = dockerHost;+ const currentHost = dockerHost; // Create dependency on dockerHostif (currentHost !== undefined) {- // Check if initializedtestStatus = "idle";testMessage = null;}@@ -106,12 +86,27 @@method="POST"use:enhance={() => {isSubmitting = true;- testStatus = "idle"; // Reset test status on save attempt+ testStatus = "idle";testMessage = null;- return async ({ update }) => {- await update({ reset: false }); // Use reset: false to prevent SvelteKit from resetting form state automatically+ return async ({ result, update }) => {isSubmitting = false;- // The $effect will handle updating local state from props automatically++ // IMPORTANT: Update local state *before* calling update()+ // This makes the UI reflect the change instantly+ if (result.type === "success" && result.data?.settings) {+ // Define the type for settings explicitly if needed, or infer+ const newSettings = result.data.settings as typeof data.settings;+ dockerHost = newSettings.dockerHost;+ autoRefresh = newSettings.autoRefresh;+ refreshInterval = newSettings.refreshInterval;+ darkMode = newSettings.darkMode;+ }++ // Now call update to refresh the 'form' prop (for success/error messages)+ // and potentially other page data if the load function depends on settings+ await update({ reset: false });++ // No need to update state again here, it was done before update()};}}>