Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions apps/dokploy/server/api/routers/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
checkGPUStatus,
checkPortInUse,
cleanupAll,
cleanupAllBackground,
cleanupBuilders,
cleanupContainers,
cleanupImages,
Expand Down Expand Up @@ -202,9 +201,12 @@ export const settingsRouter = createTRPCRouter({
.input(apiServerSchema)
.mutation(async ({ input }) => {
// Execute cleanup in background and return immediately to avoid gateway timeouts
const result = await cleanupAllBackground(input?.serverId);
void cleanupAll(input?.serverId);

return result;
return {
status: "scheduled",
message: "Docker cleanup has been initiated in the background",
};
}),
cleanMonitoring: adminProcedure.mutation(async () => {
if (IS_CLOUD) {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/db/schema/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const server = pgTable("server", {
appName: text("appName")
.notNull()
.$defaultFn(() => generateAppName("server")),
enableDockerCleanup: boolean("enableDockerCleanup").notNull().default(false),
enableDockerCleanup: boolean("enableDockerCleanup").notNull().default(true),
createdAt: text("createdAt").notNull(),
organizationId: text("organizationId")
.notNull()
Expand Down
85 changes: 23 additions & 62 deletions packages/server/src/utils/docker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ CHECK_INTERVAL=10
echo "Preparing for execution..."

while true; do
PROCESSES=$(ps aux | grep -E "^.*docker [A-Za-z]" | grep -v grep)
PROCESSES=$(ps aux | grep -E "^.*docker [a-zA-Z]" | grep -v grep)

if [ -z "$PROCESSES" ]; then
echo "Docker is idle. Starting execution..."
Expand All @@ -173,26 +173,24 @@ echo "Execution completed."
`;

const cleanupCommands = {
containers: "docker container prune --force",
images: "docker image prune --all --force",
volumes: "docker volume prune --all --force",
builders: "docker builder prune --all --force",
system: "docker system prune --all --force",
containers: dockerSafeExec("docker container prune --force"),
images: dockerSafeExec("docker image prune --all --force"),
volumes: dockerSafeExec("docker volume prune --all --force"),
builders: dockerSafeExec("docker builder prune --all --force"),
system: dockerSafeExec("docker system prune --all --force"),
};

export const cleanupContainers = async (serverId?: string) => {
try {
const command = cleanupCommands.containers;

if (serverId) {
await execAsyncRemote(serverId, dockerSafeExec(command));
await execAsyncRemote(serverId, command);
} else {
await execAsync(dockerSafeExec(command));
await execAsync(command);
}
} catch (error) {
console.error(error);

throw error;
}
};

Expand All @@ -201,12 +199,12 @@ export const cleanupImages = async (serverId?: string) => {
const command = cleanupCommands.images;

if (serverId) {
await execAsyncRemote(serverId, dockerSafeExec(command));
} else await execAsync(dockerSafeExec(command));
await execAsyncRemote(serverId, command);
} else {
await execAsync(command);
}
} catch (error) {
console.error(error);

throw error;
}
};

Expand All @@ -215,14 +213,12 @@ export const cleanupVolumes = async (serverId?: string) => {
const command = cleanupCommands.volumes;

if (serverId) {
await execAsyncRemote(serverId, dockerSafeExec(command));
await execAsyncRemote(serverId, command);
} else {
await execAsync(dockerSafeExec(command));
await execAsync(command);
}
} catch (error) {
console.error(error);

throw error;
}
};

Expand All @@ -231,14 +227,12 @@ export const cleanupBuilders = async (serverId?: string) => {
const command = cleanupCommands.builders;

if (serverId) {
await execAsyncRemote(serverId, dockerSafeExec(command));
await execAsyncRemote(serverId, command);
} else {
await execAsync(dockerSafeExec(command));
await execAsync(command);
}
} catch (error) {
console.error(error);

throw error;
}
};

Expand All @@ -247,14 +241,12 @@ export const cleanupSystem = async (serverId?: string) => {
const command = cleanupCommands.system;

if (serverId) {
await execAsyncRemote(serverId, dockerSafeExec(command));
await execAsyncRemote(serverId, command);
} else {
await execAsync(dockerSafeExec(command));
await execAsync(command);
}
} catch (error) {
console.error(error);

throw error;
}
};

Expand All @@ -276,47 +268,16 @@ export const cleanupAll = async (serverId?: string) => {

try {
if (serverId) {
await execAsyncRemote(serverId, dockerSafeExec(command));
await execAsyncRemote(serverId, command);
} else {
await execAsync(dockerSafeExec(command));
await execAsync(command);
}
} catch {}
} catch (error) {
console.error(error);
}
}
};

export const cleanupAllBackground = async (serverId?: string) => {
Promise.allSettled(
(
Object.entries(cleanupCommands) as [
keyof typeof cleanupCommands,
string,
][]
)
.filter(([key]) => !excludedCleanupAllCommands.includes(key))
.map(async ([, command]) => {
if (serverId) {
await execAsyncRemote(serverId, dockerSafeExec(command));
} else {
await execAsync(dockerSafeExec(command));
}
}),
)
.then((results) => {
const failed = results.filter((r) => r.status === "rejected");
if (failed.length > 0) {
console.error(`Docker cleanup: ${failed.length} operations failed`);
} else {
console.log("Docker cleanup completed successfully");
}
})
.catch((error) => console.error("Error in cleanup:", error));

return {
status: "scheduled",
message: "Docker cleanup has been initiated in the background",
};
};

export const startService = async (appName: string) => {
try {
await execAsync(`docker service scale ${appName}=1 `);
Expand Down