diff --git a/packages/server/src/services/settings.ts b/packages/server/src/services/settings.ts index da26512ab..748eb769f 100644 --- a/packages/server/src/services/settings.ts +++ b/packages/server/src/services/settings.ts @@ -10,7 +10,7 @@ import { initializeTraefikService, type TraefikOptions, } from "../setup/traefik-setup"; -import { dockerSafeExec } from "@dokploy/server/utils/docker/utils"; +import { cleanupAll } from "@dokploy/server/utils/docker/utils"; export interface IUpdateData { latestVersion: string | null; @@ -216,40 +216,7 @@ echo "$json_output" return result; }; -export const cleanupFullDocker = async (serverId?: string | null) => { - const cleanupImages = "docker image prune --all --force"; - const cleanupVolumes = "docker volume prune --all --force"; - const cleanupContainers = "docker container prune --force"; - const cleanupSystem = "docker system prune --all --volumes --force"; - const cleanupBuilder = "docker builder prune --all --force"; - - try { - if (serverId) { - await execAsyncRemote( - serverId, - dockerSafeExec(` - ${cleanupImages} - ${cleanupVolumes} - ${cleanupContainers} - ${cleanupSystem} - ${cleanupBuilder} - `), - ); - } - - await execAsync( - dockerSafeExec(` - ${cleanupImages} - ${cleanupVolumes} - ${cleanupContainers} - ${cleanupSystem} - ${cleanupBuilder} - `), - ); - } catch (error) { - console.log(error); - } -}; +export const cleanupFullDocker = async (serverId?: string) => cleanupAll(serverId); export const getDockerResourceType = async ( resourceName: string, diff --git a/packages/server/src/utils/backups/index.ts b/packages/server/src/utils/backups/index.ts index 6ce8f9e55..d1481c345 100644 --- a/packages/server/src/utils/backups/index.ts +++ b/packages/server/src/utils/backups/index.ts @@ -7,9 +7,7 @@ import { scheduleJob } from "node-schedule"; import { db } from "../../db/index"; import { startLogCleanup } from "../access-log/handler"; import { - cleanUpDockerBuilder, - cleanUpSystemPrune, - cleanUpUnusedImages, + cleanupAll } from "../docker/utils"; import { sendDockerCleanupNotifications } from "../notifications/docker-cleanup"; import { execAsync, execAsyncRemote } from "../process/execAsync"; @@ -34,9 +32,9 @@ export const initCronJobs = async () => { console.log( `Docker Cleanup ${new Date().toLocaleString()}] Running docker cleanup`, ); - await cleanUpUnusedImages(); - await cleanUpDockerBuilder(); - await cleanUpSystemPrune(); + + await cleanupAll(); + await sendDockerCleanupNotifications(admin.user.id); }); } @@ -49,10 +47,10 @@ export const initCronJobs = async () => { scheduleJob(serverId, "0 0 * * *", async () => { console.log( `SERVER-BACKUP[${new Date().toLocaleString()}] Running Cleanup ${name}`, - ); - await cleanUpUnusedImages(serverId); - await cleanUpDockerBuilder(serverId); - await cleanUpSystemPrune(serverId); + ) + + await cleanupImages(serverId); + await sendDockerCleanupNotifications( admin.user.id, `Docker cleanup for Server ${name} (${serverId})`, diff --git a/packages/server/src/utils/docker/utils.ts b/packages/server/src/utils/docker/utils.ts index 7babb61b2..a779e0b6d 100644 --- a/packages/server/src/utils/docker/utils.ts +++ b/packages/server/src/utils/docker/utils.ts @@ -166,49 +166,77 @@ ${exec} echo "Execution completed."`; -export const cleanUpUnusedImages = async (serverId?: string) => { - try { - const command = "docker image prune --all --force"; - if (serverId) { - await execAsyncRemote(serverId, dockerSafeExec(command)); - } else { - await execAsync(dockerSafeExec(command)); - } - } catch (error) { - console.error(error); - throw error; - } -}; - -export const cleanStoppedContainers = async (serverId?: string) => { +export const cleanupContainers = async (serverId?: string) => { try { const command = "docker container prune --force"; + if (serverId) { await execAsyncRemote(serverId, dockerSafeExec(command)); - } else { - await execAsync(dockerSafeExec(command)); - } + } else await execAsync(dockerSafeExec(command)); } catch (error) { console.error(error); + throw error; } }; -export const cleanUpUnusedVolumes = async (serverId?: string) => { +export const cleanupImages = async (serverId?: string) => { + try { + const command = "docker image prune --all --force"; + + if (serverId) { + await execAsyncRemote(serverId, dockerSafeExec(command)); + } else await execAsync(dockerSafeExec(command)); + } catch (error) { + console.error(error); + + throw error; + } +}; + +export const cleanupVolumes = async (serverId?: string) => { try { const command = "docker volume prune --all --force"; + if (serverId) { await execAsyncRemote(serverId, dockerSafeExec(command)); - } else { - await execAsync(dockerSafeExec(command)); - } + } else await execAsync(dockerSafeExec(command)); } catch (error) { console.error(error); + throw error; } }; -export const cleanUpInactiveContainers = async () => { +export const cleanupBuilders = async (serverId?: string) => { + try { + const command = "docker builder prune --all --force"; + + if (serverId) { + await execAsyncRemote(serverId, dockerSafeExec(command)); + } else await execAsync(dockerSafeExec(command)); + } catch (error) { + console.error(error); + + throw error; + } +}; + +export const cleanupSystem = async (serverId?: string) => { + try { + const command = "docker system prune --all --volumes --force"; + + if (serverId) { + await execAsyncRemote(serverId, dockerSafeExec(command)); + } else await execAsync(dockerSafeExec(command)); + } catch (error) { + console.error(error); + + throw error; + } +}; + +export const cleanupInactiveContainers = async () => { try { const containers = await docker.listContainers({ all: true }); const inactiveContainers = containers.filter( @@ -225,23 +253,14 @@ export const cleanUpInactiveContainers = async () => { } }; -export const cleanUpDockerBuilder = async (serverId?: string) => { - const command = "docker builder prune --all --force"; - if (serverId) { - await execAsyncRemote(serverId, dockerSafeExec(command)); - } else { - await execAsync(dockerSafeExec(command)); - } -}; - -export const cleanUpSystemPrune = async (serverId?: string) => { - const command = "docker system prune --all --volumes --force"; - if (serverId) { - await execAsyncRemote(serverId, dockerSafeExec(command)); - } else { - await execAsync(dockerSafeExec(command)); - } -}; +export const cleanupAll = async (serverId?: string) => { + await cleanupContainers(serverId); + await cleanupImages(serverId); + await cleanupVolumes(serverId); + await cleanupBuilders(serverId); + await cleanupSystem(serverId); + await cleanupInactiveContainers(); +} export const startService = async (appName: string) => { try {