Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: option to delete docker volumes on service deletion #886

Merged
Merged
Show file tree
Hide file tree
Changes from 11 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import {
Dialog,
DialogContent,
Expand Down Expand Up @@ -31,6 +32,7 @@ const deleteApplicationSchema = z.object({
projectName: z.string().min(1, {
message: "Application name is required",
}),
deleteVolumes: z.boolean(),
});

type DeleteApplication = z.infer<typeof deleteApplicationSchema>;
Expand All @@ -50,6 +52,7 @@ export const DeleteApplication = ({ applicationId }: Props) => {
const form = useForm<DeleteApplication>({
defaultValues: {
projectName: "",
deleteVolumes: false,
},
resolver: zodResolver(deleteApplicationSchema),
});
Expand All @@ -59,6 +62,7 @@ export const DeleteApplication = ({ applicationId }: Props) => {
if (formData.projectName === expectedName) {
await mutateAsync({
applicationId,
deleteVolumes: formData.deleteVolumes,
})
.then((data) => {
push(`/dashboard/project/${data?.projectId}`);
Expand Down Expand Up @@ -134,6 +138,27 @@ export const DeleteApplication = ({ applicationId }: Props) => {
</FormItem>
)}
/>
<FormField
control={form.control}
name="deleteVolumes"
render={({ field }) => (
<FormItem>
<div className="flex items-center">
<FormControl>
<Checkbox
checked={field.value}
onCheckedChange={field.onChange}
/>
</FormControl>

<FormLabel className="ml-2">
Delete volumes associated with this compose
</FormLabel>
</div>
<FormMessage />
</FormItem>
)}
/>
</form>
</Form>
</div>
Expand Down
28 changes: 27 additions & 1 deletion apps/dokploy/components/dashboard/compose/delete-compose.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import {
Dialog,
DialogContent,
Expand All @@ -12,6 +13,7 @@ import {
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
Expand All @@ -32,6 +34,7 @@ const deleteComposeSchema = z.object({
projectName: z.string().min(1, {
message: "Compose name is required",
}),
deleteVolumes: z.boolean(),
});

type DeleteCompose = z.infer<typeof deleteComposeSchema>;
Expand All @@ -51,14 +54,16 @@ export const DeleteCompose = ({ composeId }: Props) => {
const form = useForm<DeleteCompose>({
defaultValues: {
projectName: "",
deleteVolumes: false,
},
resolver: zodResolver(deleteComposeSchema),
});

const onSubmit = async (formData: DeleteCompose) => {
const expectedName = `${data?.name}/${data?.appName}`;
if (formData.projectName === expectedName) {
await mutateAsync({ composeId })
const { deleteVolumes } = formData;
await mutateAsync({ composeId, deleteVolumes })
.then((result) => {
push(`/dashboard/project/${result?.projectId}`);
toast.success("Compose deleted successfully");
Expand Down Expand Up @@ -133,6 +138,27 @@ export const DeleteCompose = ({ composeId }: Props) => {
</FormItem>
)}
/>
<FormField
control={form.control}
name="deleteVolumes"
render={({ field }) => (
<FormItem>
<div className="flex items-center">
<FormControl>
<Checkbox
checked={field.value}
onCheckedChange={field.onChange}
/>
</FormControl>

<FormLabel className="ml-2">
Delete volumes associated with this compose
</FormLabel>
</div>
<FormMessage />
</FormItem>
)}
/>
</form>
</Form>
</div>
Expand Down
9 changes: 7 additions & 2 deletions apps/dokploy/server/api/routers/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import { db } from "@/server/db";
import {
apiCreateApplication,
apiDeleteApplication,
apiFindMonitoringStats,
apiFindOneApplication,
apiReloadApplication,
Expand Down Expand Up @@ -142,7 +143,7 @@ export const applicationRouter = createTRPCRouter({
}),

delete: protectedProcedure
.input(apiFindOneApplication)
.input(apiDeleteApplication)
.mutation(async ({ input, ctx }) => {
if (ctx.user.rol === "user") {
await checkServiceAccess(
Expand Down Expand Up @@ -178,7 +179,11 @@ export const applicationRouter = createTRPCRouter({
async () =>
await removeTraefikConfig(application.appName, application.serverId),
async () =>
await removeService(application?.appName, application.serverId),
await removeService(
application?.appName,
application.serverId,
input.deleteVolumes,
),
];

for (const operation of cleanupOperations) {
Expand Down
5 changes: 3 additions & 2 deletions apps/dokploy/server/api/routers/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { db } from "@/server/db";
import {
apiCreateCompose,
apiCreateComposeByTemplate,
apiDeleteCompose,
apiFetchServices,
apiFindCompose,
apiRandomizeCompose,
Expand Down Expand Up @@ -117,7 +118,7 @@ export const composeRouter = createTRPCRouter({
return updateCompose(input.composeId, input);
}),
delete: protectedProcedure
.input(apiFindCompose)
.input(apiDeleteCompose)
.mutation(async ({ input, ctx }) => {
if (ctx.user.rol === "user") {
await checkServiceAccess(ctx.user.authId, input.composeId, "delete");
Expand All @@ -138,7 +139,7 @@ export const composeRouter = createTRPCRouter({
.returning();

const cleanupOperations = [
async () => await removeCompose(composeResult),
async () => await removeCompose(composeResult, input.deleteVolumes),
async () => await removeDeploymentsByComposeId(composeResult),
async () => await removeComposeDirectory(composeResult.appName),
];
Expand Down
7 changes: 6 additions & 1 deletion packages/server/src/db/schema/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { github } from "./github";
import { gitlab } from "./gitlab";
import { mounts } from "./mount";
import { ports } from "./port";
import { previewDeployments } from "./preview-deployments";
import { projects } from "./project";
import { redirects } from "./redirects";
import { registry } from "./registry";
Expand All @@ -25,7 +26,6 @@ import { server } from "./server";
import { applicationStatus, certificateType } from "./shared";
import { sshKeys } from "./ssh-key";
import { generateAppName } from "./utils";
import { previewDeployments } from "./preview-deployments";

export const sourceType = pgEnum("sourceType", [
"docker",
Expand Down Expand Up @@ -518,3 +518,8 @@ export const apiUpdateApplication = createSchema
applicationId: z.string().min(1),
})
.omit({ serverId: true });

export const apiDeleteApplication = z.object({
applicationId: z.string().min(1),
deleteVolumes: z.boolean(),
});
5 changes: 5 additions & 0 deletions packages/server/src/db/schema/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ export const apiFindCompose = z.object({
composeId: z.string().min(1),
});

export const apiDeleteCompose = z.object({
composeId: z.string().min(1),
deleteVolumes: z.boolean(),
});

export const apiFetchServices = z.object({
composeId: z.string().min(1),
type: z.enum(["fetch", "cache"]).optional().default("cache"),
Expand Down
25 changes: 22 additions & 3 deletions packages/server/src/services/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,13 +437,26 @@ export const rebuildRemoteCompose = async ({
return true;
};

export const removeCompose = async (compose: Compose) => {
export const removeCompose = async (
compose: Compose,
deleteVolumes: boolean,
) => {
try {
const { COMPOSE_PATH } = paths(!!compose.serverId);
const projectPath = join(COMPOSE_PATH, compose.appName);

console.log("API: DELETE VOLUMES=", deleteVolumes);

if (compose.composeType === "stack") {
const command = `cd ${projectPath} && docker stack rm ${compose.appName} && rm -rf ${projectPath}`;
const listVolumesCommand = `docker volume ls --format \"{{.Name}}\" | grep ${compose.appName}`;
const removeVolumesCommand = `${listVolumesCommand} | xargs -r docker volume rm`;
let command: string;
if (deleteVolumes) {
command = `cd ${projectPath} && docker stack rm ${compose.appName} && ${removeVolumesCommand} && rm -rf ${projectPath}`;
} else {
command = `cd ${projectPath} && docker stack rm ${compose.appName} && rm -rf ${projectPath}`;
}

DJKnaeckebrot marked this conversation as resolved.
Show resolved Hide resolved
if (compose.serverId) {
await execAsyncRemote(compose.serverId, command);
} else {
Expand All @@ -453,7 +466,13 @@ export const removeCompose = async (compose: Compose) => {
cwd: projectPath,
});
} else {
const command = `cd ${projectPath} && docker compose -p ${compose.appName} down && rm -rf ${projectPath}`;
let command: string;
if (deleteVolumes) {
command = `cd ${projectPath} && docker compose -p ${compose.appName} down --volumes && rm -rf ${projectPath}`;
} else {
command = `cd ${projectPath} && docker compose -p ${compose.appName} down && rm -rf ${projectPath}`;
}

if (compose.serverId) {
await execAsyncRemote(compose.serverId, command);
} else {
Expand Down
Loading
Loading