mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-06-16 04:35:24 +02:00
Merge pull request #2305 from Dokploy/2249-traefik-doesnt-reload-when-installed-as-a-docker-service
refactor(traefik): update Traefik initialization to support standalon…
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { useTranslation } from "next-i18next";
|
||||
import { toast } from "sonner";
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
@@ -10,8 +11,6 @@ import {
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu";
|
||||
import { api } from "@/utils/api";
|
||||
import { useTranslation } from "next-i18next";
|
||||
import { toast } from "sonner";
|
||||
import { EditTraefikEnv } from "../../web-server/edit-traefik-env";
|
||||
import { ManageTraefikPorts } from "../../web-server/manage-traefik-ports";
|
||||
import { ShowModalLogs } from "../../web-server/show-modal-logs";
|
||||
|
||||
@@ -1,3 +1,11 @@
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { ArrowRightLeft, Plus, Trash2 } from "lucide-react";
|
||||
import { useTranslation } from "next-i18next";
|
||||
import type React from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useFieldArray, useForm } from "react-hook-form";
|
||||
import { toast } from "sonner";
|
||||
import { z } from "zod";
|
||||
import { AlertBlock } from "@/components/shared/alert-block";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent } from "@/components/ui/card";
|
||||
@@ -19,15 +27,15 @@ import {
|
||||
} from "@/components/ui/form";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { ScrollArea } from "@/components/ui/scroll-area";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectGroup,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import { api } from "@/utils/api";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { ArrowRightLeft, Plus, Trash2 } from "lucide-react";
|
||||
import { useTranslation } from "next-i18next";
|
||||
import type React from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useFieldArray, useForm } from "react-hook-form";
|
||||
import { toast } from "sonner";
|
||||
import { z } from "zod";
|
||||
|
||||
interface Props {
|
||||
children: React.ReactNode;
|
||||
@@ -37,6 +45,7 @@ interface Props {
|
||||
const PortSchema = z.object({
|
||||
targetPort: z.number().min(1, "Target port is required"),
|
||||
publishedPort: z.number().min(1, "Published port is required"),
|
||||
protocol: z.enum(["tcp", "udp", "sctp"]),
|
||||
});
|
||||
|
||||
const TraefikPortsSchema = z.object({
|
||||
@@ -75,12 +84,17 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {
|
||||
|
||||
useEffect(() => {
|
||||
if (currentPorts) {
|
||||
form.reset({ ports: currentPorts });
|
||||
form.reset({
|
||||
ports: currentPorts.map((port) => ({
|
||||
...port,
|
||||
protocol: port.protocol as "tcp" | "udp" | "sctp",
|
||||
})),
|
||||
});
|
||||
}
|
||||
}, [currentPorts, form]);
|
||||
|
||||
const handleAddPort = () => {
|
||||
append({ targetPort: 0, publishedPort: 0 });
|
||||
append({ targetPort: 0, publishedPort: 0, protocol: "tcp" });
|
||||
};
|
||||
|
||||
const onSubmit = async (data: TraefikPortsForm) => {
|
||||
@@ -96,7 +110,9 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {
|
||||
|
||||
return (
|
||||
<>
|
||||
<div onClick={() => setOpen(true)}>{children}</div>
|
||||
<button type="button" onClick={() => setOpen(true)}>
|
||||
{children}
|
||||
</button>
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogContent className="sm:max-w-3xl">
|
||||
<DialogHeader>
|
||||
@@ -143,8 +159,8 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {
|
||||
<ScrollArea className="h-[400px] pr-4">
|
||||
<div className="grid gap-4">
|
||||
{fields.map((field, index) => (
|
||||
<Card key={field.id}>
|
||||
<CardContent className="grid grid-cols-[1fr_1fr_auto] gap-4 p-4 transparent">
|
||||
<Card key={field.id} className="bg-transparent">
|
||||
<CardContent className="grid grid-cols-[1fr_1fr_1fr_auto] gap-4 p-4 transparent">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name={`ports.${index}.targetPort`}
|
||||
@@ -168,7 +184,6 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {
|
||||
);
|
||||
}}
|
||||
value={field.value || ""}
|
||||
className="w-full dark:bg-black"
|
||||
placeholder="e.g. 8080"
|
||||
/>
|
||||
</FormControl>
|
||||
@@ -200,7 +215,6 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {
|
||||
);
|
||||
}}
|
||||
value={field.value || ""}
|
||||
className="w-full dark:bg-black"
|
||||
placeholder="e.g. 80"
|
||||
/>
|
||||
</FormControl>
|
||||
@@ -208,6 +222,42 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name={`ports.${index}.protocol`}
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel className="text-sm font-medium text-muted-foreground">
|
||||
Protocol
|
||||
</FormLabel>
|
||||
<FormControl>
|
||||
<Select
|
||||
onValueChange={field.onChange}
|
||||
defaultValue={field.value}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Select a protocol" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectGroup>
|
||||
{["tcp", "udp", "sctp"].map(
|
||||
(protocol) => (
|
||||
<SelectItem
|
||||
key={protocol}
|
||||
value={protocol}
|
||||
>
|
||||
{protocol}
|
||||
</SelectItem>
|
||||
),
|
||||
)}
|
||||
</SelectGroup>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<div className="flex items-end">
|
||||
<Button
|
||||
|
||||
@@ -1,3 +1,54 @@
|
||||
import {
|
||||
canAccessToTraefikFiles,
|
||||
checkGPUStatus,
|
||||
cleanStoppedContainers,
|
||||
cleanUpDockerBuilder,
|
||||
cleanUpSystemPrune,
|
||||
cleanUpUnusedImages,
|
||||
cleanUpUnusedVolumes,
|
||||
DEFAULT_UPDATE_DATA,
|
||||
execAsync,
|
||||
findServerById,
|
||||
findUserById,
|
||||
getDokployImage,
|
||||
getDokployImageTag,
|
||||
getLogCleanupStatus,
|
||||
getUpdateData,
|
||||
IS_CLOUD,
|
||||
parseRawConfig,
|
||||
paths,
|
||||
prepareEnvironmentVariables,
|
||||
processLogs,
|
||||
pullLatestRelease,
|
||||
readConfig,
|
||||
readConfigInPath,
|
||||
readDirectory,
|
||||
readEnvironmentVariables,
|
||||
readMainConfig,
|
||||
readMonitoringConfig,
|
||||
readPorts,
|
||||
recreateDirectory,
|
||||
reloadDockerResource,
|
||||
sendDockerCleanupNotifications,
|
||||
setupGPUSupport,
|
||||
spawnAsync,
|
||||
startLogCleanup,
|
||||
stopLogCleanup,
|
||||
updateLetsEncryptEmail,
|
||||
updateServerById,
|
||||
updateServerTraefik,
|
||||
updateUser,
|
||||
writeConfig,
|
||||
writeMainConfig,
|
||||
writeTraefikConfigInPath,
|
||||
writeTraefikSetup,
|
||||
} from "@dokploy/server";
|
||||
import { generateOpenApiDocument } from "@dokploy/trpc-openapi";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { sql } from "drizzle-orm";
|
||||
import { dump, load } from "js-yaml";
|
||||
import { scheduledJobs, scheduleJob } from "node-schedule";
|
||||
import { z } from "zod";
|
||||
import { db } from "@/server/db";
|
||||
import {
|
||||
apiAssignDomain,
|
||||
@@ -11,54 +62,6 @@ import {
|
||||
apiUpdateDockerCleanup,
|
||||
} from "@/server/db/schema";
|
||||
import { removeJob, schedule } from "@/server/utils/backup";
|
||||
import {
|
||||
DEFAULT_UPDATE_DATA,
|
||||
IS_CLOUD,
|
||||
canAccessToTraefikFiles,
|
||||
cleanStoppedContainers,
|
||||
cleanUpDockerBuilder,
|
||||
cleanUpSystemPrune,
|
||||
cleanUpUnusedImages,
|
||||
cleanUpUnusedVolumes,
|
||||
execAsync,
|
||||
execAsyncRemote,
|
||||
findServerById,
|
||||
findUserById,
|
||||
getDokployImage,
|
||||
getDokployImageTag,
|
||||
getLogCleanupStatus,
|
||||
getUpdateData,
|
||||
initializeTraefik,
|
||||
parseRawConfig,
|
||||
paths,
|
||||
prepareEnvironmentVariables,
|
||||
processLogs,
|
||||
pullLatestRelease,
|
||||
readConfig,
|
||||
readConfigInPath,
|
||||
readDirectory,
|
||||
readMainConfig,
|
||||
readMonitoringConfig,
|
||||
recreateDirectory,
|
||||
sendDockerCleanupNotifications,
|
||||
spawnAsync,
|
||||
startLogCleanup,
|
||||
stopLogCleanup,
|
||||
updateLetsEncryptEmail,
|
||||
updateServerById,
|
||||
updateServerTraefik,
|
||||
updateUser,
|
||||
writeConfig,
|
||||
writeMainConfig,
|
||||
writeTraefikConfigInPath,
|
||||
} from "@dokploy/server";
|
||||
import { checkGPUStatus, setupGPUSupport } from "@dokploy/server";
|
||||
import { generateOpenApiDocument } from "@dokploy/trpc-openapi";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { sql } from "drizzle-orm";
|
||||
import { dump, load } from "js-yaml";
|
||||
import { scheduleJob, scheduledJobs } from "node-schedule";
|
||||
import { z } from "zod";
|
||||
import packageInfo from "../../../package.json";
|
||||
import { appRouter } from "../root";
|
||||
import {
|
||||
@@ -73,10 +76,7 @@ export const settingsRouter = createTRPCRouter({
|
||||
if (IS_CLOUD) {
|
||||
return true;
|
||||
}
|
||||
const { stdout } = await execAsync(
|
||||
"docker service inspect dokploy --format '{{.ID}}'",
|
||||
);
|
||||
await execAsync(`docker service update --force ${stdout.trim()}`);
|
||||
await reloadDockerResource("dokploy");
|
||||
return true;
|
||||
}),
|
||||
cleanRedis: adminProcedure.mutation(async () => {
|
||||
@@ -101,20 +101,15 @@ export const settingsRouter = createTRPCRouter({
|
||||
if (IS_CLOUD) {
|
||||
return true;
|
||||
}
|
||||
await reloadDockerResource("dokploy-redis");
|
||||
|
||||
await execAsync("docker service scale dokploy-redis=0");
|
||||
await execAsync("docker service scale dokploy-redis=1");
|
||||
return true;
|
||||
}),
|
||||
reloadTraefik: adminProcedure
|
||||
.input(apiServerSchema)
|
||||
.mutation(async ({ input }) => {
|
||||
try {
|
||||
if (input?.serverId) {
|
||||
await execAsync("docker restart dokploy-traefik");
|
||||
} else if (!IS_CLOUD) {
|
||||
await execAsync("docker restart dokploy-traefik");
|
||||
}
|
||||
await reloadDockerResource("dokploy-traefik", input?.serverId);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
@@ -124,17 +119,28 @@ export const settingsRouter = createTRPCRouter({
|
||||
toggleDashboard: adminProcedure
|
||||
.input(apiEnableDashboard)
|
||||
.mutation(async ({ input }) => {
|
||||
const ports = (await getTraefikPorts(input.serverId)).filter(
|
||||
(port) =>
|
||||
port.targetPort !== 80 &&
|
||||
port.targetPort !== 443 &&
|
||||
port.targetPort !== 8080,
|
||||
const ports = await readPorts("dokploy-traefik", input.serverId);
|
||||
const env = await readEnvironmentVariables(
|
||||
"dokploy-traefik",
|
||||
input.serverId,
|
||||
);
|
||||
await initializeTraefik({
|
||||
additionalPorts: ports,
|
||||
enableDashboard: input.enableDashboard,
|
||||
const preparedEnv = prepareEnvironmentVariables(env);
|
||||
let newPorts = ports;
|
||||
// If receive true, add 8080 to ports
|
||||
if (input.enableDashboard) {
|
||||
newPorts.push({
|
||||
targetPort: 8080,
|
||||
publishedPort: 8080,
|
||||
protocol: "tcp",
|
||||
});
|
||||
} else {
|
||||
newPorts = ports.filter((port) => port.targetPort !== 8080);
|
||||
}
|
||||
|
||||
await writeTraefikSetup({
|
||||
env: preparedEnv,
|
||||
additionalPorts: newPorts,
|
||||
serverId: input.serverId,
|
||||
force: true,
|
||||
});
|
||||
return true;
|
||||
}),
|
||||
@@ -551,29 +557,23 @@ export const settingsRouter = createTRPCRouter({
|
||||
readTraefikEnv: adminProcedure
|
||||
.input(apiServerSchema)
|
||||
.query(async ({ input }) => {
|
||||
const command =
|
||||
"docker container inspect dokploy-traefik --format '{{json .Config.Env}}'";
|
||||
|
||||
let result = "";
|
||||
if (input?.serverId) {
|
||||
const execResult = await execAsyncRemote(input.serverId, command);
|
||||
result = execResult.stdout;
|
||||
} else {
|
||||
const execResult = await execAsync(command);
|
||||
result = execResult.stdout;
|
||||
}
|
||||
const envVars = JSON.parse(result.trim());
|
||||
return envVars.join("\n");
|
||||
const envVars = await readEnvironmentVariables(
|
||||
"dokploy-traefik",
|
||||
input?.serverId,
|
||||
);
|
||||
return envVars;
|
||||
}),
|
||||
|
||||
writeTraefikEnv: adminProcedure
|
||||
.input(z.object({ env: z.string(), serverId: z.string().optional() }))
|
||||
.mutation(async ({ input }) => {
|
||||
const envs = prepareEnvironmentVariables(input.env);
|
||||
await initializeTraefik({
|
||||
const ports = await readPorts("dokploy-traefik", input?.serverId);
|
||||
|
||||
await writeTraefikSetup({
|
||||
env: envs,
|
||||
additionalPorts: ports,
|
||||
serverId: input.serverId,
|
||||
force: true,
|
||||
});
|
||||
|
||||
return true;
|
||||
@@ -581,22 +581,8 @@ export const settingsRouter = createTRPCRouter({
|
||||
haveTraefikDashboardPortEnabled: adminProcedure
|
||||
.input(apiServerSchema)
|
||||
.query(async ({ input }) => {
|
||||
const command = `docker container inspect --format='{{json .NetworkSettings.Ports}}' dokploy-traefik`;
|
||||
|
||||
let stdout = "";
|
||||
if (input?.serverId) {
|
||||
const result = await execAsyncRemote(input.serverId, command);
|
||||
stdout = result.stdout;
|
||||
} else if (!IS_CLOUD) {
|
||||
const result = await execAsync(command);
|
||||
stdout = result.stdout;
|
||||
}
|
||||
|
||||
const ports = JSON.parse(stdout.trim());
|
||||
return Object.entries(ports).some(([containerPort, bindings]) => {
|
||||
const [port] = containerPort.split("/");
|
||||
return port === "8080" && bindings && (bindings as any[]).length > 0;
|
||||
});
|
||||
const ports = await readPorts("dokploy-traefik", input?.serverId);
|
||||
return ports.some((port) => port.targetPort === 8080);
|
||||
}),
|
||||
|
||||
readStatsLogs: adminProcedure
|
||||
@@ -793,6 +779,7 @@ export const settingsRouter = createTRPCRouter({
|
||||
z.object({
|
||||
targetPort: z.number(),
|
||||
publishedPort: z.number(),
|
||||
protocol: z.enum(["tcp", "udp", "sctp"]),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
@@ -805,10 +792,16 @@ export const settingsRouter = createTRPCRouter({
|
||||
message: "Please set a serverId to update Traefik ports",
|
||||
});
|
||||
}
|
||||
await initializeTraefik({
|
||||
serverId: input.serverId,
|
||||
const env = await readEnvironmentVariables(
|
||||
"dokploy-traefik",
|
||||
input?.serverId,
|
||||
);
|
||||
const preparedEnv = prepareEnvironmentVariables(env);
|
||||
|
||||
await writeTraefikSetup({
|
||||
env: preparedEnv,
|
||||
additionalPorts: input.additionalPorts,
|
||||
force: true,
|
||||
serverId: input.serverId,
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
@@ -825,7 +818,8 @@ export const settingsRouter = createTRPCRouter({
|
||||
getTraefikPorts: adminProcedure
|
||||
.input(apiServerSchema)
|
||||
.query(async ({ input }) => {
|
||||
return await getTraefikPorts(input?.serverId);
|
||||
const ports = await readPorts("dokploy-traefik", input?.serverId);
|
||||
return ports;
|
||||
}),
|
||||
updateLogCleanup: adminProcedure
|
||||
.input(
|
||||
@@ -855,56 +849,3 @@ export const settingsRouter = createTRPCRouter({
|
||||
return ips;
|
||||
}),
|
||||
});
|
||||
|
||||
export const getTraefikPorts = async (serverId?: string) => {
|
||||
const command = `docker container inspect --format='{{json .NetworkSettings.Ports}}' dokploy-traefik`;
|
||||
try {
|
||||
let stdout = "";
|
||||
if (serverId) {
|
||||
const result = await execAsyncRemote(serverId, command);
|
||||
stdout = result.stdout;
|
||||
} else if (!IS_CLOUD) {
|
||||
const result = await execAsync(command);
|
||||
stdout = result.stdout;
|
||||
}
|
||||
|
||||
const portsMap = JSON.parse(stdout.trim());
|
||||
const additionalPorts: Array<{
|
||||
targetPort: number;
|
||||
publishedPort: number;
|
||||
}> = [];
|
||||
|
||||
// Convert the Docker container port format to our expected format
|
||||
for (const [containerPort, bindings] of Object.entries(portsMap)) {
|
||||
if (!bindings) continue;
|
||||
|
||||
const [port = ""] = containerPort.split("/");
|
||||
if (!port) continue;
|
||||
|
||||
const targetPortNum = Number.parseInt(port, 10);
|
||||
if (Number.isNaN(targetPortNum)) continue;
|
||||
|
||||
// Skip default ports
|
||||
if ([80, 443].includes(targetPortNum)) continue;
|
||||
|
||||
for (const binding of bindings as Array<{ HostPort: string }>) {
|
||||
if (!binding.HostPort) continue;
|
||||
const publishedPort = Number.parseInt(binding.HostPort, 10);
|
||||
if (Number.isNaN(publishedPort)) continue;
|
||||
|
||||
additionalPorts.push({
|
||||
targetPort: targetPortNum,
|
||||
publishedPort,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return additionalPorts;
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "Failed to get Traefik ports",
|
||||
cause: error,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,10 +1,3 @@
|
||||
import {
|
||||
createDefaultMiddlewares,
|
||||
createDefaultServerTraefikConfig,
|
||||
createDefaultTraefikConfig,
|
||||
initializeTraefik,
|
||||
} from "@dokploy/server/setup/traefik-setup";
|
||||
|
||||
import { execAsync } from "@dokploy/server";
|
||||
import { setupDirectories } from "@dokploy/server/setup/config-paths";
|
||||
import { initializePostgres } from "@dokploy/server/setup/postgres-setup";
|
||||
@@ -13,6 +6,13 @@ import {
|
||||
initializeNetwork,
|
||||
initializeSwarm,
|
||||
} from "@dokploy/server/setup/setup";
|
||||
import {
|
||||
createDefaultMiddlewares,
|
||||
createDefaultServerTraefikConfig,
|
||||
createDefaultTraefikConfig,
|
||||
initializeStandaloneTraefik,
|
||||
} from "@dokploy/server/setup/traefik-setup";
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
setupDirectories();
|
||||
@@ -22,7 +22,7 @@ import {
|
||||
createDefaultTraefikConfig();
|
||||
createDefaultServerTraefikConfig();
|
||||
await execAsync("docker pull traefik:v3.1.2");
|
||||
await initializeTraefik();
|
||||
await initializeStandaloneTraefik();
|
||||
await initializeRedis();
|
||||
await initializePostgres();
|
||||
} catch (e) {
|
||||
|
||||
@@ -5,6 +5,11 @@ import {
|
||||
execAsync,
|
||||
execAsyncRemote,
|
||||
} from "@dokploy/server/utils/process/execAsync";
|
||||
import {
|
||||
initializeStandaloneTraefik,
|
||||
initializeTraefikService,
|
||||
type TraefikOptions,
|
||||
} from "../setup/traefik-setup";
|
||||
|
||||
export interface IUpdateData {
|
||||
latestVersion: string | null;
|
||||
@@ -243,3 +248,165 @@ export const cleanupFullDocker = async (serverId?: string | null) => {
|
||||
console.log(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const getDockerResourceType = async (
|
||||
resourceName: string,
|
||||
serverId?: string,
|
||||
) => {
|
||||
let result = "";
|
||||
const command = `
|
||||
RESOURCE_NAME="${resourceName}"
|
||||
if docker service inspect "$RESOURCE_NAME" &>/dev/null; then
|
||||
echo "service"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if docker inspect "$RESOURCE_NAME" &>/dev/null; then
|
||||
echo "standalone"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "unknown"
|
||||
exit 0
|
||||
`;
|
||||
|
||||
if (serverId) {
|
||||
const { stdout } = await execAsyncRemote(serverId, command);
|
||||
result = stdout.trim();
|
||||
} else {
|
||||
const { stdout } = await execAsync(command);
|
||||
result = stdout.trim();
|
||||
}
|
||||
if (result === "service") {
|
||||
return "service";
|
||||
}
|
||||
if (result === "standalone") {
|
||||
return "standalone";
|
||||
}
|
||||
return "unknown";
|
||||
};
|
||||
|
||||
export const reloadDockerResource = async (
|
||||
resourceName: string,
|
||||
serverId?: string,
|
||||
) => {
|
||||
const resourceType = await getDockerResourceType(resourceName, serverId);
|
||||
let command = "";
|
||||
if (resourceType === "service") {
|
||||
command = `docker service update --force ${resourceName}`;
|
||||
} else {
|
||||
command = `docker restart ${resourceName}`;
|
||||
}
|
||||
if (serverId) {
|
||||
await execAsyncRemote(serverId, command);
|
||||
} else {
|
||||
await execAsync(command);
|
||||
}
|
||||
};
|
||||
|
||||
export const readEnvironmentVariables = async (
|
||||
resourceName: string,
|
||||
serverId?: string,
|
||||
) => {
|
||||
const resourceType = await getDockerResourceType(resourceName, serverId);
|
||||
let command = "";
|
||||
if (resourceType === "service") {
|
||||
command = `docker service inspect ${resourceName} --format '{{json .Spec.TaskTemplate.ContainerSpec.Env}}'`;
|
||||
} else {
|
||||
command = `docker container inspect ${resourceName} --format '{{json .Config.Env}}'`;
|
||||
}
|
||||
let result = "";
|
||||
if (serverId) {
|
||||
const { stdout } = await execAsyncRemote(serverId, command);
|
||||
result = stdout.trim();
|
||||
} else {
|
||||
const { stdout } = await execAsync(command);
|
||||
result = stdout.trim();
|
||||
}
|
||||
if (result === "null") {
|
||||
return "";
|
||||
}
|
||||
return JSON.parse(result)?.join("\n");
|
||||
};
|
||||
|
||||
export const readPorts = async (
|
||||
resourceName: string,
|
||||
serverId?: string,
|
||||
): Promise<
|
||||
{ targetPort: number; publishedPort: number; protocol?: string }[]
|
||||
> => {
|
||||
const resourceType = await getDockerResourceType(resourceName, serverId);
|
||||
let command = "";
|
||||
if (resourceType === "service") {
|
||||
command = `docker service inspect ${resourceName} --format '{{json .Spec.EndpointSpec.Ports}}'`;
|
||||
} else {
|
||||
command = `docker container inspect ${resourceName} --format '{{json .NetworkSettings.Ports}}'`;
|
||||
}
|
||||
let result = "";
|
||||
if (serverId) {
|
||||
const { stdout } = await execAsyncRemote(serverId, command);
|
||||
result = stdout.trim();
|
||||
} else {
|
||||
const { stdout } = await execAsync(command);
|
||||
result = stdout.trim();
|
||||
}
|
||||
|
||||
if (result === "null") {
|
||||
return [];
|
||||
}
|
||||
|
||||
const parsedResult = JSON.parse(result);
|
||||
|
||||
if (resourceType === "service") {
|
||||
return parsedResult
|
||||
.map((port: any) => ({
|
||||
targetPort: port.TargetPort,
|
||||
publishedPort: port.PublishedPort,
|
||||
protocol: port.Protocol,
|
||||
}))
|
||||
.filter((port: any) => port.targetPort !== 80 && port.targetPort !== 443);
|
||||
}
|
||||
const ports: {
|
||||
targetPort: number;
|
||||
publishedPort: number;
|
||||
protocol?: string;
|
||||
}[] = [];
|
||||
for (const key in parsedResult) {
|
||||
if (Object.hasOwn(parsedResult, key)) {
|
||||
const containerPortMapppings = parsedResult[key];
|
||||
const protocol = key.split("/")[1];
|
||||
const targetPort = Number.parseInt(key.split("/")[0] ?? "0", 10);
|
||||
|
||||
containerPortMapppings.forEach((mapping: any) => {
|
||||
ports.push({
|
||||
targetPort: targetPort,
|
||||
publishedPort: Number.parseInt(mapping.HostPort, 10),
|
||||
protocol: protocol,
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
return ports.filter(
|
||||
(port: any) => port.targetPort !== 80 && port.targetPort !== 443,
|
||||
);
|
||||
};
|
||||
|
||||
export const writeTraefikSetup = async (
|
||||
input: TraefikOptions,
|
||||
serverId?: string,
|
||||
) => {
|
||||
const resourceType = await getDockerResourceType("dokploy-traefik", serverId);
|
||||
if (resourceType === "service") {
|
||||
await initializeTraefikService({
|
||||
env: input.env,
|
||||
additionalPorts: input.additionalPorts,
|
||||
serverId: serverId,
|
||||
});
|
||||
} else {
|
||||
await initializeStandaloneTraefik({
|
||||
env: input.env,
|
||||
additionalPorts: input.additionalPorts,
|
||||
serverId: serverId,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { chmodSync, existsSync, mkdirSync, writeFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import type { ContainerCreateOptions } from "dockerode";
|
||||
import type { ContainerCreateOptions, CreateServiceOptions } from "dockerode";
|
||||
import { dump } from "js-yaml";
|
||||
import { paths } from "../constants";
|
||||
import { getRemoteDocker } from "../utils/servers/remote-docker";
|
||||
@@ -15,23 +15,20 @@ export const TRAEFIK_HTTP3_PORT =
|
||||
Number.parseInt(process.env.TRAEFIK_HTTP3_PORT!, 10) || 443;
|
||||
export const TRAEFIK_VERSION = process.env.TRAEFIK_VERSION || "3.1.2";
|
||||
|
||||
interface TraefikOptions {
|
||||
enableDashboard?: boolean;
|
||||
export interface TraefikOptions {
|
||||
env?: string[];
|
||||
serverId?: string;
|
||||
additionalPorts?: {
|
||||
targetPort: number;
|
||||
publishedPort: number;
|
||||
protocol?: string;
|
||||
}[];
|
||||
force?: boolean;
|
||||
}
|
||||
|
||||
export const initializeTraefik = async ({
|
||||
enableDashboard = false,
|
||||
export const initializeStandaloneTraefik = async ({
|
||||
env,
|
||||
serverId,
|
||||
additionalPorts = [],
|
||||
force = false,
|
||||
}: TraefikOptions = {}) => {
|
||||
const { MAIN_TRAEFIK_PATH, DYNAMIC_TRAEFIK_PATH } = paths(!!serverId);
|
||||
const imageName = `traefik:v${TRAEFIK_VERSION}`;
|
||||
@@ -51,13 +48,17 @@ export const initializeTraefik = async ({
|
||||
],
|
||||
};
|
||||
|
||||
const enableDashboard = additionalPorts.some(
|
||||
(port) => port.targetPort === 8080,
|
||||
);
|
||||
|
||||
if (enableDashboard) {
|
||||
exposedPorts["8080/tcp"] = {};
|
||||
portBindings["8080/tcp"] = [{ HostPort: "8080" }];
|
||||
}
|
||||
|
||||
for (const port of additionalPorts) {
|
||||
const portKey = `${port.targetPort}/tcp`;
|
||||
const portKey = `${port.targetPort}/${port.protocol ?? "tcp"}`;
|
||||
exposedPorts[portKey] = {};
|
||||
portBindings[portKey] = [{ HostPort: port.publishedPort.toString() }];
|
||||
}
|
||||
@@ -87,68 +88,117 @@ export const initializeTraefik = async ({
|
||||
|
||||
const docker = await getRemoteDocker(serverId);
|
||||
try {
|
||||
try {
|
||||
const service = docker.getService("dokploy-traefik");
|
||||
await service?.remove({ force: true });
|
||||
|
||||
let attempts = 0;
|
||||
const maxAttempts = 5;
|
||||
while (attempts < maxAttempts) {
|
||||
try {
|
||||
await docker.listServices({
|
||||
filters: { name: ["dokploy-traefik"] },
|
||||
});
|
||||
console.log("Waiting for service cleanup...");
|
||||
await new Promise((resolve) => setTimeout(resolve, 5000));
|
||||
attempts++;
|
||||
} catch {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
console.log("No existing service to remove");
|
||||
}
|
||||
|
||||
// Then try to remove any existing container
|
||||
const container = docker.getContainer(containerName);
|
||||
try {
|
||||
const inspect = await container.inspect();
|
||||
if (inspect.State.Status === "running" && !force) {
|
||||
console.log("Traefik already running");
|
||||
return;
|
||||
}
|
||||
|
||||
await container.remove({ force: true });
|
||||
await new Promise((resolve) => setTimeout(resolve, 5000));
|
||||
} catch {
|
||||
console.log("No existing container to remove");
|
||||
}
|
||||
|
||||
// Create and start the new container
|
||||
try {
|
||||
await docker.createContainer(settings);
|
||||
const newContainer = docker.getContainer(containerName);
|
||||
await newContainer.start();
|
||||
console.log("Traefik container started successfully");
|
||||
} catch (error: any) {
|
||||
if (error?.json?.message?.includes("port is already allocated")) {
|
||||
console.log("Ports still in use, waiting longer for cleanup...");
|
||||
await new Promise((resolve) => setTimeout(resolve, 10000));
|
||||
// Try one more time
|
||||
await docker.createContainer(settings);
|
||||
const newContainer = docker.getContainer(containerName);
|
||||
await newContainer.start();
|
||||
console.log("Traefik container started successfully after retry");
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
console.log("Traefik Started ✅");
|
||||
} catch (error) {
|
||||
console.error("Error in initializeStandaloneTraefik", error);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to initialize Traefik:", error);
|
||||
await docker.createContainer(settings);
|
||||
console.error("Error in initializeStandaloneTraefik", error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const initializeTraefikService = async ({
|
||||
env,
|
||||
additionalPorts = [],
|
||||
serverId,
|
||||
}: TraefikOptions) => {
|
||||
const { MAIN_TRAEFIK_PATH, DYNAMIC_TRAEFIK_PATH } = paths(!!serverId);
|
||||
const imageName = `traefik:v${TRAEFIK_VERSION}`;
|
||||
const appName = "dokploy-traefik";
|
||||
|
||||
const settings: CreateServiceOptions = {
|
||||
Name: appName,
|
||||
TaskTemplate: {
|
||||
ContainerSpec: {
|
||||
Image: imageName,
|
||||
Env: env,
|
||||
Mounts: [
|
||||
{
|
||||
Type: "bind",
|
||||
Source: `${MAIN_TRAEFIK_PATH}/traefik.yml`,
|
||||
Target: "/etc/traefik/traefik.yml",
|
||||
},
|
||||
{
|
||||
Type: "bind",
|
||||
Source: DYNAMIC_TRAEFIK_PATH,
|
||||
Target: "/etc/dokploy/traefik/dynamic",
|
||||
},
|
||||
{
|
||||
Type: "bind",
|
||||
Source: "/var/run/docker.sock",
|
||||
Target: "/var/run/docker.sock",
|
||||
},
|
||||
],
|
||||
},
|
||||
Networks: [{ Target: "dokploy-network" }],
|
||||
Placement: {
|
||||
Constraints: ["node.role==manager"],
|
||||
},
|
||||
},
|
||||
Mode: {
|
||||
Replicated: {
|
||||
Replicas: 1,
|
||||
},
|
||||
},
|
||||
EndpointSpec: {
|
||||
Ports: [
|
||||
{
|
||||
TargetPort: 443,
|
||||
PublishedPort: TRAEFIK_SSL_PORT,
|
||||
PublishMode: "host",
|
||||
Protocol: "tcp",
|
||||
},
|
||||
{
|
||||
TargetPort: 443,
|
||||
PublishedPort: TRAEFIK_SSL_PORT,
|
||||
PublishMode: "host",
|
||||
Protocol: "udp",
|
||||
},
|
||||
{
|
||||
TargetPort: 80,
|
||||
PublishedPort: TRAEFIK_PORT,
|
||||
PublishMode: "host",
|
||||
Protocol: "tcp",
|
||||
},
|
||||
|
||||
...additionalPorts.map((port) => ({
|
||||
TargetPort: port.targetPort,
|
||||
PublishedPort: port.publishedPort,
|
||||
Protocol: port.protocol as "tcp" | "udp" | "sctp" | undefined,
|
||||
PublishMode: "host" as const,
|
||||
})),
|
||||
],
|
||||
},
|
||||
};
|
||||
const docker = await getRemoteDocker(serverId);
|
||||
try {
|
||||
const service = docker.getService(appName);
|
||||
const inspect = await service.inspect();
|
||||
|
||||
await service.update({
|
||||
version: Number.parseInt(inspect.Version.Index),
|
||||
...settings,
|
||||
TaskTemplate: {
|
||||
...settings.TaskTemplate,
|
||||
ForceUpdate: inspect.Spec.TaskTemplate.ForceUpdate + 1,
|
||||
},
|
||||
});
|
||||
console.log("Traefik Updated ✅");
|
||||
} catch {
|
||||
await docker.createService(settings);
|
||||
console.log("Traefik Started ✅");
|
||||
}
|
||||
};
|
||||
|
||||
export const createDefaultServerTraefikConfig = () => {
|
||||
const { DYNAMIC_TRAEFIK_PATH } = paths();
|
||||
const configFilePath = path.join(DYNAMIC_TRAEFIK_PATH, "dokploy.yml");
|
||||
|
||||
Reference in New Issue
Block a user