mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-06 20:22:14 +00:00
feat: docker cleanups stable
This commit is contained in:
@@ -10,7 +10,7 @@ import {
|
|||||||
initializeTraefikService,
|
initializeTraefikService,
|
||||||
type TraefikOptions,
|
type TraefikOptions,
|
||||||
} from "../setup/traefik-setup";
|
} from "../setup/traefik-setup";
|
||||||
import { dockerSafeExec } from "@dokploy/server/utils/docker/utils";
|
import { cleanupAll } from "@dokploy/server/utils/docker/utils";
|
||||||
|
|
||||||
export interface IUpdateData {
|
export interface IUpdateData {
|
||||||
latestVersion: string | null;
|
latestVersion: string | null;
|
||||||
@@ -216,40 +216,7 @@ echo "$json_output"
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const cleanupFullDocker = async (serverId?: string | null) => {
|
export const cleanupFullDocker = async (serverId?: string) => cleanupAll(serverId);
|
||||||
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 getDockerResourceType = async (
|
export const getDockerResourceType = async (
|
||||||
resourceName: string,
|
resourceName: string,
|
||||||
|
|||||||
@@ -7,9 +7,7 @@ import { scheduleJob } from "node-schedule";
|
|||||||
import { db } from "../../db/index";
|
import { db } from "../../db/index";
|
||||||
import { startLogCleanup } from "../access-log/handler";
|
import { startLogCleanup } from "../access-log/handler";
|
||||||
import {
|
import {
|
||||||
cleanUpDockerBuilder,
|
cleanupAll
|
||||||
cleanUpSystemPrune,
|
|
||||||
cleanUpUnusedImages,
|
|
||||||
} from "../docker/utils";
|
} from "../docker/utils";
|
||||||
import { sendDockerCleanupNotifications } from "../notifications/docker-cleanup";
|
import { sendDockerCleanupNotifications } from "../notifications/docker-cleanup";
|
||||||
import { execAsync, execAsyncRemote } from "../process/execAsync";
|
import { execAsync, execAsyncRemote } from "../process/execAsync";
|
||||||
@@ -34,9 +32,9 @@ export const initCronJobs = async () => {
|
|||||||
console.log(
|
console.log(
|
||||||
`Docker Cleanup ${new Date().toLocaleString()}] Running docker cleanup`,
|
`Docker Cleanup ${new Date().toLocaleString()}] Running docker cleanup`,
|
||||||
);
|
);
|
||||||
await cleanUpUnusedImages();
|
|
||||||
await cleanUpDockerBuilder();
|
await cleanupAll();
|
||||||
await cleanUpSystemPrune();
|
|
||||||
await sendDockerCleanupNotifications(admin.user.id);
|
await sendDockerCleanupNotifications(admin.user.id);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -49,10 +47,10 @@ export const initCronJobs = async () => {
|
|||||||
scheduleJob(serverId, "0 0 * * *", async () => {
|
scheduleJob(serverId, "0 0 * * *", async () => {
|
||||||
console.log(
|
console.log(
|
||||||
`SERVER-BACKUP[${new Date().toLocaleString()}] Running Cleanup ${name}`,
|
`SERVER-BACKUP[${new Date().toLocaleString()}] Running Cleanup ${name}`,
|
||||||
);
|
)
|
||||||
await cleanUpUnusedImages(serverId);
|
|
||||||
await cleanUpDockerBuilder(serverId);
|
await cleanupImages(serverId);
|
||||||
await cleanUpSystemPrune(serverId);
|
|
||||||
await sendDockerCleanupNotifications(
|
await sendDockerCleanupNotifications(
|
||||||
admin.user.id,
|
admin.user.id,
|
||||||
`Docker cleanup for Server ${name} (${serverId})`,
|
`Docker cleanup for Server ${name} (${serverId})`,
|
||||||
|
|||||||
@@ -166,49 +166,77 @@ ${exec}
|
|||||||
|
|
||||||
echo "Execution completed."`;
|
echo "Execution completed."`;
|
||||||
|
|
||||||
export const cleanUpUnusedImages = async (serverId?: string) => {
|
export const cleanupContainers = 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) => {
|
|
||||||
try {
|
try {
|
||||||
const command = "docker container prune --force";
|
const command = "docker container prune --force";
|
||||||
|
|
||||||
if (serverId) {
|
if (serverId) {
|
||||||
await execAsyncRemote(serverId, dockerSafeExec(command));
|
await execAsyncRemote(serverId, dockerSafeExec(command));
|
||||||
} else {
|
} else await execAsync(dockerSafeExec(command));
|
||||||
await execAsync(dockerSafeExec(command));
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
||||||
throw 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 {
|
try {
|
||||||
const command = "docker volume prune --all --force";
|
const command = "docker volume prune --all --force";
|
||||||
|
|
||||||
if (serverId) {
|
if (serverId) {
|
||||||
await execAsyncRemote(serverId, dockerSafeExec(command));
|
await execAsyncRemote(serverId, dockerSafeExec(command));
|
||||||
} else {
|
} else await execAsync(dockerSafeExec(command));
|
||||||
await execAsync(dockerSafeExec(command));
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
||||||
throw 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 {
|
try {
|
||||||
const containers = await docker.listContainers({ all: true });
|
const containers = await docker.listContainers({ all: true });
|
||||||
const inactiveContainers = containers.filter(
|
const inactiveContainers = containers.filter(
|
||||||
@@ -225,23 +253,14 @@ export const cleanUpInactiveContainers = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const cleanUpDockerBuilder = async (serverId?: string) => {
|
export const cleanupAll = async (serverId?: string) => {
|
||||||
const command = "docker builder prune --all --force";
|
await cleanupContainers(serverId);
|
||||||
if (serverId) {
|
await cleanupImages(serverId);
|
||||||
await execAsyncRemote(serverId, dockerSafeExec(command));
|
await cleanupVolumes(serverId);
|
||||||
} else {
|
await cleanupBuilders(serverId);
|
||||||
await execAsync(dockerSafeExec(command));
|
await cleanupSystem(serverId);
|
||||||
}
|
await cleanupInactiveContainers();
|
||||||
};
|
}
|
||||||
|
|
||||||
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 startService = async (appName: string) => {
|
export const startService = async (appName: string) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user