Skip to content

Commit 6a10923

Browse files
committed
fix: warn when the postgres image implies a data directory that mismatches the mounted volume
1 parent bcab5b0 commit 6a10923

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

‎apps/dokploy/components/dashboard/postgres/advanced/show-custom-command.tsx‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useEffect } from "react";
44
import { useFieldArray, useForm } from "react-hook-form";
55
import { toast } from "sonner";
66
import { z } from "zod";
7+
import { AlertBlock } from "@/components/shared/alert-block";
78
import { Button } from "@/components/ui/button";
89
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
910
import {
@@ -18,6 +19,19 @@ import { Input } from "@/components/ui/input";
1819
import { api } from "@/utils/api";
1920
import type { ServiceType } from "../../application/advanced/show-resources";
2021

22+
const getPostgresMountPath = (dockerImage: string): string => {
23+
const versionMatch = dockerImage.match(/postgres:(\d+)/);
24+
if (versionMatch?.[1]) {
25+
const version = Number.parseInt(versionMatch[1], 10);
26+
if (version >= 18) {
27+
return `/var/lib/postgresql/${version}/docker`;
28+
}
29+
}
30+
return "/var/lib/postgresql/data";
31+
};
32+
33+
const POSTGRES_DATA_PATH_REGEX = /^\/var\/lib\/postgresql(\/|$)/;
34+
2135
const addDockerImage = z.object({
2236
dockerImage: z.string().min(1, "Docker image is required"),
2337
command: z.string(),
@@ -91,6 +105,22 @@ export const ShowCustomCommand = ({ id, type }: Props) => {
91105
}
92106
}, [data, form]);
93107

108+
const dockerImage = form.watch("dockerImage");
109+
110+
const mountPathWarning = (() => {
111+
if (type !== "postgres" || !dockerImage) return null;
112+
const mounts = (data as any)?.mounts ?? [];
113+
const dataMount = mounts.find(
114+
(mount: { type: string; mountPath: string }) =>
115+
mount.type === "volume" &&
116+
POSTGRES_DATA_PATH_REGEX.test(mount.mountPath),
117+
);
118+
if (!dataMount) return null;
119+
const expectedPath = getPostgresMountPath(dockerImage);
120+
if (expectedPath === dataMount.mountPath) return null;
121+
return { expectedPath, currentPath: dataMount.mountPath };
122+
})();
123+
94124
const onSubmit = async (formData: AddDockerImage) => {
95125
await mutateAsync({
96126
mongoId: id || "",
@@ -139,6 +169,18 @@ export const ShowCustomCommand = ({ id, type }: Props) => {
139169
</FormItem>
140170
)}
141171
/>
172+
{mountPathWarning && (
173+
<AlertBlock type="warning">
174+
This image expects its data directory under{" "}
175+
<code>{mountPathWarning.expectedPath}</code>, but the
176+
volume of this database is mounted at{" "}
177+
<code>{mountPathWarning.currentPath}</code>. Changing the
178+
image does not migrate existing data — Postgres may
179+
crash-loop or start with an empty database. Adjust the
180+
volume mount path in the Volumes section or keep a
181+
compatible image before saving.
182+
</AlertBlock>
183+
)}
142184
</div>
143185
<FormField
144186
control={form.control}

0 commit comments

Comments
 (0)