Skip to content

Commit c2071bb

Browse files
committed
feat: add Images and Disk Usage tabs to docker dashboard
- Images tab: list, inspect and delete docker images, with a force-delete fallback when an image is in use. - Disk Usage tab: docker system df summary as stat cards plus a Build Cache table (docker system df -v) with a prune action.
1 parent 9412b52 commit c2071bb

9 files changed

Lines changed: 1179 additions & 3 deletions

File tree

‎apps/dokploy/components/dashboard/docker/disk-usage/show-disk-usage.tsx‎

Lines changed: 428 additions & 0 deletions
Large diffs are not rendered by default.

‎apps/dokploy/components/dashboard/docker/images/show-images.tsx‎

Lines changed: 470 additions & 0 deletions
Large diffs are not rendered by default.

‎apps/dokploy/pages/dashboard/docker.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import type { GetServerSidePropsContext } from "next";
44
import { useRouter } from "next/router";
55
import type { ReactElement } from "react";
66
import superjson from "superjson";
7+
import { ShowDiskUsage } from "@/components/dashboard/docker/disk-usage/show-disk-usage";
78
import { ShowDockerEvents } from "@/components/dashboard/docker/events/show-docker-events";
9+
import { ShowImages } from "@/components/dashboard/docker/images/show-images";
810
import { ShowContainers } from "@/components/dashboard/docker/show/show-containers";
911
import { ShowVolumes } from "@/components/dashboard/docker/volumes/show-volumes";
1012
import { ShowNetworks } from "@/components/dashboard/networks/show-networks";
@@ -63,9 +65,11 @@ const Dashboard = () => {
6365
<TabsList>
6466
<TabsTrigger value="containers">Containers</TabsTrigger>
6567
<TabsTrigger value="swarm">Swarm</TabsTrigger>
68+
<TabsTrigger value="images">Images</TabsTrigger>
6669
<TabsTrigger value="volumes">Volumes</TabsTrigger>
6770
<TabsTrigger value="networks">Networks</TabsTrigger>
6871
<TabsTrigger value="events">Events</TabsTrigger>
72+
<TabsTrigger value="disk-usage">Disk Usage</TabsTrigger>
6973
</TabsList>
7074
<TabsContent value="containers">
7175
<ShowContainers serverId={serverId} />
@@ -92,6 +96,9 @@ const Dashboard = () => {
9296
</TabsContent>
9397
</Tabs>
9498
</TabsContent>
99+
<TabsContent value="images">
100+
<ShowImages serverId={serverId} />
101+
</TabsContent>
95102
<TabsContent value="volumes">
96103
<ShowVolumes serverId={serverId} />
97104
</TabsContent>
@@ -101,6 +108,9 @@ const Dashboard = () => {
101108
<TabsContent value="events">
102109
<ShowDockerEvents serverId={serverId} />
103110
</TabsContent>
111+
<TabsContent value="disk-usage">
112+
<ShowDiskUsage serverId={serverId} />
113+
</TabsContent>
104114
</Tabs>
105115
)}
106116
</ServerFilter>

‎apps/dokploy/server/api/root.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { deploymentRouter } from "./routers/deployment";
1111
import { destinationRouter } from "./routers/destination";
1212
import { dnsProviderRouter } from "./routers/dns-provider";
1313
import { dockerRouter } from "./routers/docker";
14+
import { dockerDiskUsageRouter } from "./routers/docker-disk-usage";
15+
import { dockerImageRouter } from "./routers/docker-image";
1416
import { dockerVolumeRouter } from "./routers/docker-volume";
1517
import { domainRouter } from "./routers/domain";
1618
import { environmentRouter } from "./routers/environment";
@@ -72,6 +74,8 @@ export const appRouter = createTRPCRouter({
7274
destination: destinationRouter,
7375
dnsProvider: dnsProviderRouter,
7476
docker: dockerRouter,
77+
dockerDiskUsage: dockerDiskUsageRouter,
78+
dockerImage: dockerImageRouter,
7579
dockerVolume: dockerVolumeRouter,
7680
domain: domainRouter,
7781
gitea: giteaRouter,
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {
2+
cleanupBuilders,
3+
findServerById,
4+
getBuildCache,
5+
getDockerDiskUsage,
6+
} from "@dokploy/server";
7+
import { TRPCError } from "@trpc/server";
8+
import { z } from "zod";
9+
import { audit } from "@/server/api/utils/audit";
10+
import { createTRPCRouter, withPermission } from "../trpc";
11+
12+
export const dockerDiskUsageRouter = createTRPCRouter({
13+
getDiskUsage: withPermission("docker", "read")
14+
.input(
15+
z.object({
16+
serverId: z.string().optional(),
17+
}),
18+
)
19+
.query(async ({ input, ctx }) => {
20+
if (input.serverId) {
21+
const server = await findServerById(input.serverId);
22+
if (server.organizationId !== ctx.session?.activeOrganizationId) {
23+
throw new TRPCError({ code: "UNAUTHORIZED" });
24+
}
25+
}
26+
return await getDockerDiskUsage(input.serverId);
27+
}),
28+
29+
getBuildCache: withPermission("docker", "read")
30+
.input(
31+
z.object({
32+
serverId: z.string().optional(),
33+
}),
34+
)
35+
.query(async ({ input, ctx }) => {
36+
if (input.serverId) {
37+
const server = await findServerById(input.serverId);
38+
if (server.organizationId !== ctx.session?.activeOrganizationId) {
39+
throw new TRPCError({ code: "UNAUTHORIZED" });
40+
}
41+
}
42+
return await getBuildCache(input.serverId);
43+
}),
44+
45+
pruneBuildCache: withPermission("docker", "read")
46+
.input(
47+
z.object({
48+
serverId: z.string().optional(),
49+
}),
50+
)
51+
.mutation(async ({ input, ctx }) => {
52+
if (input.serverId) {
53+
const server = await findServerById(input.serverId);
54+
if (server.organizationId !== ctx.session?.activeOrganizationId) {
55+
throw new TRPCError({ code: "UNAUTHORIZED" });
56+
}
57+
}
58+
await cleanupBuilders(input.serverId);
59+
await audit(ctx, {
60+
action: "delete",
61+
resourceType: "docker",
62+
resourceId: "build-cache",
63+
resourceName: "Docker build cache",
64+
});
65+
}),
66+
});
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import {
2+
findServerById,
3+
getImageConfig,
4+
getImages,
5+
removeImage,
6+
} from "@dokploy/server";
7+
import { TRPCError } from "@trpc/server";
8+
import { z } from "zod";
9+
import { audit } from "@/server/api/utils/audit";
10+
import { createTRPCRouter, withPermission } from "../trpc";
11+
12+
export const dockerImageRouter = createTRPCRouter({
13+
getImages: withPermission("docker", "read")
14+
.input(
15+
z.object({
16+
serverId: z.string().optional(),
17+
}),
18+
)
19+
.query(async ({ input, ctx }) => {
20+
if (input.serverId) {
21+
const server = await findServerById(input.serverId);
22+
if (server.organizationId !== ctx.session?.activeOrganizationId) {
23+
throw new TRPCError({ code: "UNAUTHORIZED" });
24+
}
25+
}
26+
return await getImages(input.serverId);
27+
}),
28+
29+
getImageConfig: withPermission("docker", "read")
30+
.input(
31+
z.object({
32+
imageRef: z.string().min(1),
33+
serverId: z.string().optional(),
34+
}),
35+
)
36+
.query(async ({ input, ctx }) => {
37+
if (input.serverId) {
38+
const server = await findServerById(input.serverId);
39+
if (server.organizationId !== ctx.session?.activeOrganizationId) {
40+
throw new TRPCError({ code: "UNAUTHORIZED" });
41+
}
42+
}
43+
return await getImageConfig(input.imageRef, input.serverId);
44+
}),
45+
46+
removeImage: withPermission("docker", "read")
47+
.input(
48+
z.object({
49+
repository: z.string(),
50+
tag: z.string(),
51+
id: z.string().min(1),
52+
force: z.boolean().optional(),
53+
serverId: z.string().optional(),
54+
}),
55+
)
56+
.mutation(async ({ input, ctx }) => {
57+
if (input.serverId) {
58+
const server = await findServerById(input.serverId);
59+
if (server.organizationId !== ctx.session?.activeOrganizationId) {
60+
throw new TRPCError({ code: "UNAUTHORIZED" });
61+
}
62+
}
63+
await removeImage(
64+
{
65+
repository: input.repository,
66+
tag: input.tag,
67+
id: input.id,
68+
force: input.force,
69+
},
70+
input.serverId,
71+
);
72+
await audit(ctx, {
73+
action: "delete",
74+
resourceType: "docker",
75+
resourceId: input.id,
76+
resourceName:
77+
input.repository !== "<none>" && input.tag !== "<none>"
78+
? `${input.repository}:${input.tag}`
79+
: input.id,
80+
});
81+
}),
82+
});

‎packages/server/src/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export * from "./services/deployment";
1818
export * from "./services/destination";
1919
export * from "./services/dns-provider";
2020
export * from "./services/docker";
21+
export * from "./services/docker-image";
2122
export * from "./services/docker-volume";
2223
export * from "./services/domain";
2324
export * from "./services/environment";
@@ -104,7 +105,6 @@ export * from "./utils/docker/compose/volume";
104105
export * from "./utils/docker/domain";
105106
export * from "./utils/docker/types";
106107
export * from "./utils/docker/utils";
107-
export * from "./utils/vault";
108108
export * from "./utils/filesystem/directory";
109109
export * from "./utils/filesystem/ssh";
110110
export * from "./utils/git-branch-validation";
@@ -141,6 +141,7 @@ export * from "./utils/traefik/redirect";
141141
export * from "./utils/traefik/security";
142142
export * from "./utils/traefik/types";
143143
export * from "./utils/traefik/web-server";
144+
export * from "./utils/vault";
144145
export * from "./utils/volume-backups/index";
145146
export * from "./utils/watch-paths/should-deploy";
146147
export * from "./verification/send-verification-email";
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {
2+
execAsync,
3+
execAsyncRemote,
4+
} from "@dokploy/server/utils/process/execAsync";
5+
import { quote } from "shell-quote";
6+
7+
export interface DockerImage {
8+
Repository: string;
9+
Tag: string;
10+
ID: string;
11+
Digest?: string;
12+
CreatedAt: string;
13+
CreatedSince: string;
14+
Size: string;
15+
SharedSize?: string;
16+
UniqueSize?: string;
17+
VirtualSize?: string;
18+
}
19+
20+
export const getImages = async (serverId?: string) => {
21+
try {
22+
const command = "docker images --format '{{json .}}'";
23+
const { stdout } = serverId
24+
? await execAsyncRemote(serverId, command)
25+
: await execAsync(command);
26+
27+
return stdout
28+
.trim()
29+
.split("\n")
30+
.filter(Boolean)
31+
.map((line) => JSON.parse(line) as DockerImage);
32+
} catch (error) {
33+
console.error(error);
34+
return [];
35+
}
36+
};
37+
38+
export const getImageConfig = async (imageRef: string, serverId?: string) => {
39+
const command = `docker image inspect ${quote([String(imageRef ?? "")])}`;
40+
const { stdout } = serverId
41+
? await execAsyncRemote(serverId, command)
42+
: await execAsync(command);
43+
44+
return JSON.parse(stdout.trim())[0];
45+
};
46+
47+
interface RemoveImageParams {
48+
repository: string;
49+
tag: string;
50+
id: string;
51+
force?: boolean;
52+
}
53+
54+
export const removeImage = async (
55+
{ repository, tag, id, force }: RemoveImageParams,
56+
serverId?: string,
57+
) => {
58+
const hasTaggedReference =
59+
repository && tag && repository !== "<none>" && tag !== "<none>";
60+
const reference = hasTaggedReference ? `${repository}:${tag}` : id;
61+
const command = `docker rmi ${force ? "-f " : ""}${quote([String(reference ?? "")])}`;
62+
63+
if (serverId) {
64+
await execAsyncRemote(serverId, command);
65+
} else {
66+
await execAsync(command);
67+
}
68+
};

‎packages/server/src/utils/docker/utils.ts‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,13 @@ const parseSizeToBytes = (size: string): number => {
291291
return value * (multipliers[unit] || 0);
292292
};
293293

294-
export const getDockerDiskUsage = async (): Promise<DockerDiskUsageItem[]> => {
294+
export const getDockerDiskUsage = async (
295+
serverId?: string,
296+
): Promise<DockerDiskUsageItem[]> => {
295297
const command = "docker system df --format '{{json .}}'";
296-
const { stdout } = await execAsync(command);
298+
const { stdout } = serverId
299+
? await execAsyncRemote(serverId, command)
300+
: await execAsync(command);
297301

298302
const lines = stdout.trim().split("\n").filter(Boolean);
299303
return lines.map((line) => {
@@ -309,6 +313,49 @@ export const getDockerDiskUsage = async (): Promise<DockerDiskUsageItem[]> => {
309313
});
310314
};
311315

316+
export interface DockerBuildCacheItem {
317+
id: string;
318+
type: string;
319+
description: string;
320+
size: string;
321+
sizeBytes: number;
322+
createdSince: string;
323+
lastUsedSince: string;
324+
usageCount: number;
325+
shared: boolean;
326+
inUse: boolean;
327+
}
328+
329+
export const getBuildCache = async (
330+
serverId?: string,
331+
): Promise<DockerBuildCacheItem[]> => {
332+
try {
333+
const command = "docker system df -v --format '{{json .}}'";
334+
const { stdout } = serverId
335+
? await execAsyncRemote(serverId, command)
336+
: await execAsync(command);
337+
338+
const diskUsage = JSON.parse(stdout.trim());
339+
return ((diskUsage?.BuildCache ?? []) as Record<string, string>[]).map(
340+
(entry) => ({
341+
id: entry.ID ?? "",
342+
type: entry.CacheType ?? "",
343+
description: entry.Description ?? "",
344+
size: entry.Size ?? "",
345+
sizeBytes: parseSizeToBytes(entry.Size ?? ""),
346+
createdSince: entry.CreatedSince ?? "",
347+
lastUsedSince: entry.LastUsedSince ?? "",
348+
usageCount: Number.parseInt(entry.UsageCount ?? "0", 10) || 0,
349+
shared: entry.Shared === "true",
350+
inUse: entry.InUse === "true",
351+
}),
352+
);
353+
} catch (error) {
354+
console.error(error);
355+
return [];
356+
}
357+
};
358+
312359
/**
313360
* Volume cleanup should always be performed manually by the user. The reason is that during automatic cleanup, a volume may be deleted due to a stopped container, which is a dangerous situation.
314361
*

0 commit comments

Comments
 (0)