mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-10-22 08:25:26 +00:00
4bf6dfff13
Server diagnostics tab (Docker dashboard) with:
- Per-network IP usage: subnet capacity vs. containers in use, including
reserved networks like dokploy-network which are excluded from the
managed Networks tab. Surfaces read errors instead of silently showing
zeroes when the Docker API call fails.
- Broadened daemon-error detection and failure classification for Swarm
network failures: IP pool exhaustion on an existing network
("could not find an available IP", "task allocation failure") and
network attach timeouts ("attaching to network failed", "context
deadline exceeded") — both previously fell through as Unclassified.
Verified end-to-end against a real systemd-managed dockerd.
- Failed-deployment rows now carry the raw log tail used for
classification, shown on hover and linked to their service/project,
so a guess (or lack of one) can be verified against the actual error
and followed straight to the app.
- Daemon-error panel is a read-only code editor, always visible (not
hidden when empty), prefixed with the actual "Logs from X to Y"
window passed to journalctl so the --since window can be verified.
- "Download report" exports everything on screen (metrics + raw logs)
as Markdown, so a customer can send one file instead of a screen-share.
- Health check now IS_CLOUD-gated the same way network sync already is:
a cloud org can no longer omit serverId and have the check silently
run against the shared platform host instead of their own server.
Includes an inotify diagnostics card (current vs. max_user_instances/
watches/queued_events) and a failed-deployments log with automatic
cause classification based on daemon/log error patterns.
471 lines
13 KiB
TypeScript
471 lines
13 KiB
TypeScript
import {
|
|
containerKill,
|
|
containerRemove,
|
|
containerRestart,
|
|
containerStart,
|
|
containerStop,
|
|
deleteContainerFile,
|
|
findServerById,
|
|
getConfig,
|
|
getContainers,
|
|
getContainersByAppLabel,
|
|
getContainersByAppNameMatch,
|
|
getDockerEvents,
|
|
getServerHealth as getServerHealthData,
|
|
getServiceContainersByAppName,
|
|
getStackContainersByAppName,
|
|
listContainerFiles,
|
|
readContainerFile,
|
|
uploadFileToContainer,
|
|
writeContainerFile,
|
|
} from "@dokploy/server";
|
|
import { checkPermission } from "@dokploy/server/services/permission";
|
|
import { TRPCError } from "@trpc/server";
|
|
import { z } from "zod";
|
|
import { audit } from "@/server/api/utils/audit";
|
|
import { uploadFileToContainerSchema } from "@/utils/schema";
|
|
import { createTRPCRouter, protectedProcedure, withPermission } from "../trpc";
|
|
|
|
export const containerIdRegex = /^[a-zA-Z0-9.\-_]+$/;
|
|
|
|
const containerPathSchema = z
|
|
.string()
|
|
.min(1)
|
|
.max(4096)
|
|
.refine(
|
|
(path) => path.startsWith("/") && !path.includes("\0"),
|
|
"Path must be absolute.",
|
|
);
|
|
|
|
export const dockerRouter = createTRPCRouter({
|
|
getContainers: withPermission("docker", "read")
|
|
.input(
|
|
z.object({
|
|
serverId: z.string().optional(),
|
|
}),
|
|
)
|
|
.query(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
return await getContainers(input.serverId);
|
|
}),
|
|
|
|
// Host-level diagnostics, so this requires docker.read AND server.read.
|
|
getServerHealth: protectedProcedure
|
|
.input(
|
|
z.object({
|
|
serverId: z.string().optional(),
|
|
sinceHours: z.number().int().min(1).max(168).optional(),
|
|
}),
|
|
)
|
|
.query(async ({ input, ctx }) => {
|
|
await checkPermission(ctx, { docker: ["read"], server: ["read"] });
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
return await getServerHealthData(
|
|
ctx.session.activeOrganizationId,
|
|
input.serverId,
|
|
input.sinceHours,
|
|
);
|
|
}),
|
|
|
|
restartContainer: withPermission("docker", "read")
|
|
.input(
|
|
z.object({
|
|
containerId: z
|
|
.string()
|
|
.min(1)
|
|
.regex(containerIdRegex, "Invalid container id."),
|
|
serverId: z.string().optional(),
|
|
}),
|
|
)
|
|
.mutation(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
await containerRestart(input.containerId, input.serverId);
|
|
await audit(ctx, {
|
|
action: "start",
|
|
resourceType: "docker",
|
|
resourceId: input.containerId,
|
|
resourceName: input.containerId,
|
|
});
|
|
}),
|
|
|
|
startContainer: withPermission("docker", "read")
|
|
.input(
|
|
z.object({
|
|
containerId: z
|
|
.string()
|
|
.min(1)
|
|
.regex(containerIdRegex, "Invalid container id."),
|
|
serverId: z.string().optional(),
|
|
}),
|
|
)
|
|
.mutation(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
await containerStart(input.containerId, input.serverId);
|
|
await audit(ctx, {
|
|
action: "start",
|
|
resourceType: "docker",
|
|
resourceId: input.containerId,
|
|
resourceName: input.containerId,
|
|
});
|
|
}),
|
|
|
|
stopContainer: withPermission("docker", "read")
|
|
.input(
|
|
z.object({
|
|
containerId: z
|
|
.string()
|
|
.min(1)
|
|
.regex(containerIdRegex, "Invalid container id."),
|
|
serverId: z.string().optional(),
|
|
}),
|
|
)
|
|
.mutation(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
await containerStop(input.containerId, input.serverId);
|
|
await audit(ctx, {
|
|
action: "stop",
|
|
resourceType: "docker",
|
|
resourceId: input.containerId,
|
|
resourceName: input.containerId,
|
|
});
|
|
}),
|
|
|
|
killContainer: withPermission("docker", "read")
|
|
.input(
|
|
z.object({
|
|
containerId: z
|
|
.string()
|
|
.min(1)
|
|
.regex(containerIdRegex, "Invalid container id."),
|
|
serverId: z.string().optional(),
|
|
}),
|
|
)
|
|
.mutation(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
await containerKill(input.containerId, input.serverId);
|
|
await audit(ctx, {
|
|
action: "stop",
|
|
resourceType: "docker",
|
|
resourceId: input.containerId,
|
|
resourceName: input.containerId,
|
|
});
|
|
}),
|
|
|
|
removeContainer: withPermission("docker", "read")
|
|
.input(
|
|
z.object({
|
|
containerId: z
|
|
.string()
|
|
.min(1)
|
|
.regex(containerIdRegex, "Invalid container id."),
|
|
serverId: z.string().optional(),
|
|
}),
|
|
)
|
|
.mutation(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
await containerRemove(input.containerId, input.serverId);
|
|
await audit(ctx, {
|
|
action: "delete",
|
|
resourceType: "docker",
|
|
resourceId: input.containerId,
|
|
resourceName: input.containerId,
|
|
});
|
|
}),
|
|
|
|
getConfig: withPermission("docker", "read")
|
|
.input(
|
|
z.object({
|
|
containerId: z
|
|
.string()
|
|
.min(1)
|
|
.regex(containerIdRegex, "Invalid container id."),
|
|
serverId: z.string().optional(),
|
|
}),
|
|
)
|
|
.query(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
return await getConfig(input.containerId, input.serverId);
|
|
}),
|
|
|
|
getContainersByAppNameMatch: withPermission("service", "read")
|
|
.input(
|
|
z.object({
|
|
appType: z.enum(["stack", "docker-compose"]).optional(),
|
|
appName: z.string().min(1).regex(containerIdRegex, "Invalid app name."),
|
|
serverId: z.string().optional(),
|
|
}),
|
|
)
|
|
.query(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
return await getContainersByAppNameMatch(
|
|
input.appName,
|
|
input.appType,
|
|
input.serverId,
|
|
);
|
|
}),
|
|
|
|
getContainersByAppLabel: withPermission("docker", "read")
|
|
.input(
|
|
z.object({
|
|
appName: z.string().min(1).regex(containerIdRegex, "Invalid app name."),
|
|
serverId: z.string().optional(),
|
|
type: z.enum(["standalone", "swarm"]),
|
|
}),
|
|
)
|
|
.query(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
return await getContainersByAppLabel(
|
|
input.appName,
|
|
input.type,
|
|
input.serverId,
|
|
);
|
|
}),
|
|
|
|
getStackContainersByAppName: withPermission("docker", "read")
|
|
.input(
|
|
z.object({
|
|
appName: z.string().min(1).regex(containerIdRegex, "Invalid app name."),
|
|
serverId: z.string().optional(),
|
|
}),
|
|
)
|
|
.query(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
return await getStackContainersByAppName(input.appName, input.serverId);
|
|
}),
|
|
|
|
getServiceContainersByAppName: withPermission("docker", "read")
|
|
.input(
|
|
z.object({
|
|
appName: z.string().min(1).regex(containerIdRegex, "Invalid app name."),
|
|
serverId: z.string().optional(),
|
|
}),
|
|
)
|
|
.query(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
return await getServiceContainersByAppName(input.appName, input.serverId);
|
|
}),
|
|
|
|
uploadFileToContainer: withPermission("docker", "read")
|
|
.input(uploadFileToContainerSchema)
|
|
.mutation(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
|
|
const file = input.file;
|
|
if (!(file instanceof File)) {
|
|
throw new TRPCError({
|
|
code: "BAD_REQUEST",
|
|
message: "Invalid file provided",
|
|
});
|
|
}
|
|
|
|
// Convert File to Buffer
|
|
const arrayBuffer = await file.arrayBuffer();
|
|
const fileBuffer = Buffer.from(arrayBuffer);
|
|
|
|
await uploadFileToContainer(
|
|
input.containerId,
|
|
fileBuffer,
|
|
file.name,
|
|
input.destinationPath,
|
|
input.serverId || null,
|
|
);
|
|
|
|
return { success: true, message: "File uploaded successfully" };
|
|
}),
|
|
|
|
listContainerFiles: withPermission("docker", "read")
|
|
.input(
|
|
z.object({
|
|
containerId: z
|
|
.string()
|
|
.min(1)
|
|
.regex(containerIdRegex, "Invalid container id."),
|
|
path: containerPathSchema,
|
|
serverId: z.string().optional(),
|
|
}),
|
|
)
|
|
.query(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
return await listContainerFiles(
|
|
input.containerId,
|
|
input.path,
|
|
input.serverId,
|
|
);
|
|
}),
|
|
|
|
readContainerFile: withPermission("docker", "read")
|
|
.input(
|
|
z.object({
|
|
containerId: z
|
|
.string()
|
|
.min(1)
|
|
.regex(containerIdRegex, "Invalid container id."),
|
|
path: containerPathSchema,
|
|
serverId: z.string().optional(),
|
|
}),
|
|
)
|
|
.query(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
return await readContainerFile(
|
|
input.containerId,
|
|
input.path,
|
|
input.serverId,
|
|
);
|
|
}),
|
|
|
|
writeContainerFile: withPermission("docker", "read")
|
|
.input(
|
|
z.object({
|
|
containerId: z
|
|
.string()
|
|
.min(1)
|
|
.regex(containerIdRegex, "Invalid container id."),
|
|
path: containerPathSchema,
|
|
content: z.string(),
|
|
serverId: z.string().optional(),
|
|
}),
|
|
)
|
|
.mutation(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
await writeContainerFile(
|
|
input.containerId,
|
|
input.path,
|
|
input.content,
|
|
input.serverId,
|
|
);
|
|
await audit(ctx, {
|
|
action: "update",
|
|
resourceType: "docker",
|
|
resourceId: input.containerId,
|
|
resourceName: `${input.containerId}:${input.path}`,
|
|
});
|
|
}),
|
|
|
|
deleteContainerFile: withPermission("docker", "read")
|
|
.input(
|
|
z.object({
|
|
containerId: z
|
|
.string()
|
|
.min(1)
|
|
.regex(containerIdRegex, "Invalid container id."),
|
|
path: containerPathSchema.refine(
|
|
(path) => path !== "/",
|
|
"Cannot delete the container root.",
|
|
),
|
|
serverId: z.string().optional(),
|
|
}),
|
|
)
|
|
.mutation(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
await deleteContainerFile(input.containerId, input.path, input.serverId);
|
|
await audit(ctx, {
|
|
action: "delete",
|
|
resourceType: "docker",
|
|
resourceId: input.containerId,
|
|
resourceName: `${input.containerId}:${input.path}`,
|
|
});
|
|
}),
|
|
|
|
getEvents: withPermission("docker", "read")
|
|
.input(
|
|
z.object({
|
|
serverId: z.string().optional(),
|
|
minutes: z.number().min(1).max(1440).default(15),
|
|
}),
|
|
)
|
|
.query(async ({ input, ctx }) => {
|
|
if (input.serverId) {
|
|
const server = await findServerById(input.serverId);
|
|
if (server.organizationId !== ctx.session?.activeOrganizationId) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
return await getDockerEvents(input.serverId, input.minutes);
|
|
}),
|
|
});
|