feat: enforce per-container memory/CPU limits

CONTAINER_MEMORY_MB and CONTAINER_CPUS (defaults 512/1) are now applied
to every installed MCP container's HostConfig instead of leaving them
unbounded.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-10-08 03:12:44 +02:00
parent 5e8397bcd9
commit ccb7fcd842
5 changed files with 47 additions and 1 deletions

View File

@@ -14,4 +14,7 @@ export const config = {
publicBaseUrl: process.env.PUBLIC_BASE_URL ?? `http://localhost:${process.env.PORT ?? 3000}`,
adminPassword: process.env.ADMIN_PASSWORD,
sessionSecret: process.env.SESSION_SECRET,
// Per-container resource caps, applied to every installation (no per-installation override in V1).
containerMemoryMb: Number(process.env.CONTAINER_MEMORY_MB ?? 512),
containerCpus: Number(process.env.CONTAINER_CPUS ?? 1),
};

View File

@@ -39,6 +39,8 @@ export async function runInstallationContainer(installation: Installation): Prom
HostConfig: {
RestartPolicy: { Name: "unless-stopped" },
NetworkMode: config.mcpNetworkName,
Memory: config.containerMemoryMb * 1024 * 1024,
NanoCpus: Math.round(config.containerCpus * 1e9),
},
});
@@ -46,6 +48,24 @@ export async function runInstallationContainer(installation: Installation): Prom
return container.id;
}
export async function getImageId(imageTag: string): Promise<string | undefined> {
try {
const info = await docker.getImage(imageTag).inspect();
return info.Id;
} catch {
return undefined;
}
}
/** Best-effort removal: leaves the image alone if it's still referenced (e.g. by a running container). */
export async function removeImageIfUnused(imageId: string): Promise<void> {
try {
await docker.getImage(imageId).remove({ force: false });
} catch {
// still in use or already gone — nothing to do
}
}
export async function stopInstallationContainer(containerName: string): Promise<void> {
await removeContainerIfExists(containerName);
}