Merge pull request #2997 from Harikrishnan1367709/Traefik--Enable-dashboard-dokploy-traefik-container-gone,all-services-domains-down

fix(traefik): validate port 8080 before enabling dashboard -#2996
This commit is contained in:
Mauricio Siu
2025-12-06 14:08:57 -06:00
committed by GitHub
4 changed files with 98 additions and 10 deletions
@@ -1,5 +1,7 @@
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import { toast } from "sonner"; import { toast } from "sonner";
import { AlertBlock } from "@/components/shared/alert-block";
import { DialogAction } from "@/components/shared/dialog-action";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { import {
DropdownMenu, DropdownMenu,
@@ -85,7 +87,26 @@ export const ShowTraefikActions = ({ serverId }: Props) => {
</DropdownMenuItem> </DropdownMenuItem>
</EditTraefikEnv> </EditTraefikEnv>
<DropdownMenuItem <DialogAction
title={
haveTraefikDashboardPortEnabled
? "Disable Traefik Dashboard"
: "Enable Traefik Dashboard"
}
description={
<div className="space-y-4">
<AlertBlock type="warning">
The Traefik container will be recreated from scratch. This
means the container will be deleted and created again, which
may cause downtime in your applications.
</AlertBlock>
<p>
Are you sure you want to{" "}
{haveTraefikDashboardPortEnabled ? "disable" : "enable"} the
Traefik dashboard?
</p>
</div>
}
onClick={async () => { onClick={async () => {
await toggleDashboard({ await toggleDashboard({
enableDashboard: !haveTraefikDashboardPortEnabled, enableDashboard: !haveTraefikDashboardPortEnabled,
@@ -97,14 +118,26 @@ export const ShowTraefikActions = ({ serverId }: Props) => {
); );
refetchDashboard(); refetchDashboard();
}) })
.catch(() => {}); .catch((error) => {
const errorMessage =
error?.message ||
"Failed to toggle dashboard. Please check if port 8080 is available.";
toast.error(errorMessage);
});
}} }}
className="w-full cursor-pointer space-x-3" disabled={toggleDashboardIsLoading}
type="default"
> >
<span> <DropdownMenuItem
{haveTraefikDashboardPortEnabled ? "Disable" : "Enable"} Dashboard onSelect={(e) => e.preventDefault()}
</span> className="w-full cursor-pointer space-x-3"
</DropdownMenuItem> >
<span>
{haveTraefikDashboardPortEnabled ? "Disable" : "Enable"}{" "}
Dashboard
</span>
</DropdownMenuItem>
</DialogAction>
<ManageTraefikPorts serverId={serverId}> <ManageTraefikPorts serverId={serverId}>
<DropdownMenuItem <DropdownMenuItem
onSelect={(e) => e.preventDefault()} onSelect={(e) => e.preventDefault()}
@@ -105,7 +105,9 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {
}); });
toast.success(t("settings.server.webServer.traefik.portsUpdated")); toast.success(t("settings.server.webServer.traefik.portsUpdated"));
setOpen(false); setOpen(false);
} catch {} } catch (error) {
toast.error((error as Error).message || "Error updating Traefik ports");
}
}; };
return ( return (
@@ -156,11 +158,11 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {
</p> </p>
</div> </div>
) : ( ) : (
<ScrollArea className="h-[400px] pr-4"> <ScrollArea className="pr-4">
<div className="grid gap-4"> <div className="grid gap-4">
{fields.map((field, index) => ( {fields.map((field, index) => (
<Card key={field.id} className="bg-transparent"> <Card key={field.id} className="bg-transparent">
<CardContent className="grid grid-cols-[1fr_1fr_1fr_auto] gap-4 p-4 transparent"> <CardContent className="grid grid-cols-4 gap-4 p-4 transparent">
<FormField <FormField
control={form.control} control={form.control}
name={`ports.${index}.targetPort`} name={`ports.${index}.targetPort`}
@@ -303,6 +305,12 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {
</div> </div>
</AlertBlock> </AlertBlock>
)} )}
<AlertBlock type="warning">
The Traefik container will be recreated from scratch. This
means the container will be deleted and created again, which
may cause downtime in your applications.
</AlertBlock>
</div> </div>
<DialogFooter> <DialogFooter>
<Button <Button
@@ -1,6 +1,7 @@
import { import {
canAccessToTraefikFiles, canAccessToTraefikFiles,
checkGPUStatus, checkGPUStatus,
checkPortInUse,
cleanStoppedContainers, cleanStoppedContainers,
cleanUpDockerBuilder, cleanUpDockerBuilder,
cleanUpSystemPrune, cleanUpSystemPrune,
@@ -130,6 +131,17 @@ export const settingsRouter = createTRPCRouter({
let newPorts = ports; let newPorts = ports;
// If receive true, add 8080 to ports // If receive true, add 8080 to ports
if (input.enableDashboard) { if (input.enableDashboard) {
// Check if port 8080 is already in use before enabling dashboard
const portCheck = await checkPortInUse(8080, input.serverId);
if (portCheck.isInUse) {
const conflictingContainer = portCheck.conflictingContainer
? ` by container "${portCheck.conflictingContainer}"`
: "";
throw new TRPCError({
code: "CONFLICT",
message: `Port 8080 is already in use${conflictingContainer}. Please stop the conflicting service or use a different port for the Traefik dashboard.`,
});
}
newPorts.push({ newPorts.push({
targetPort: 8080, targetPort: 8080,
publishedPort: 8080, publishedPort: 8080,
@@ -810,6 +822,19 @@ export const settingsRouter = createTRPCRouter({
"dokploy-traefik", "dokploy-traefik",
input?.serverId, input?.serverId,
); );
for (const port of input.additionalPorts) {
const portCheck = await checkPortInUse(
port.publishedPort,
input.serverId,
);
if (portCheck.isInUse) {
throw new TRPCError({
code: "CONFLICT",
message: `Port ${port.targetPort} is already in use by ${portCheck.conflictingContainer}`,
});
}
}
const preparedEnv = prepareEnvironmentVariables(env); const preparedEnv = prepareEnvironmentVariables(env);
await writeTraefikSetup({ await writeTraefikSetup({
+22
View File
@@ -392,6 +392,28 @@ export const readPorts = async (
); );
}; };
export const checkPortInUse = async (
port: number,
serverId?: string,
): Promise<{ isInUse: boolean; conflictingContainer?: string }> => {
try {
const command = `docker ps -a --format '{{.Names}}' | grep -v '^dokploy-traefik$' | while read name; do docker port "$name" 2>/dev/null | grep -q ':${port}' && echo "$name" && break; done || true`;
const { stdout } = serverId
? await execAsyncRemote(serverId, command)
: await execAsync(command);
const container = stdout.trim();
return {
isInUse: !!container,
conflictingContainer: container || undefined,
};
} catch (error) {
console.error("Error checking port availability:", error);
return { isInUse: false };
}
};
export const writeTraefikSetup = async (input: TraefikOptions) => { export const writeTraefikSetup = async (input: TraefikOptions) => {
const resourceType = await getDockerResourceType( const resourceType = await getDockerResourceType(
"dokploy-traefik", "dokploy-traefik",