Support dyad docker (#674)

TODOs:
- [ ] clean-up docker images

https://claude.ai/chat/13b2c5d3-0d46-49e3-a771-d10edf1e29f4
This commit is contained in:
Will Chen
2025-08-26 11:07:40 -07:00
committed by GitHub
parent e6c92a24ed
commit 9869fefbcb
6 changed files with 455 additions and 47 deletions

View File

@@ -1,10 +1,12 @@
import { ChildProcess } from "node:child_process";
import { ChildProcess, spawn } from "node:child_process";
import treeKill from "tree-kill";
// Define a type for the value stored in runningApps
export interface RunningAppInfo {
process: ChildProcess;
processId: number;
isDocker: boolean;
containerName?: string;
}
// Store running app processes
@@ -81,6 +83,49 @@ export function killProcess(process: ChildProcess): Promise<void> {
});
}
/**
* Gracefully stops a Docker container by name. Resolves even if the container doesn't exist.
*/
export function stopDockerContainer(containerName: string): Promise<void> {
return new Promise<void>((resolve) => {
const stop = spawn("docker", ["stop", containerName], { stdio: "pipe" });
stop.on("close", () => resolve());
stop.on("error", () => resolve());
});
}
/**
* Removes Docker named volumes used for an app's dependencies.
* Best-effort: resolves even if volumes don't exist.
*/
export function removeDockerVolumesForApp(appId: number): Promise<void> {
return new Promise<void>((resolve) => {
const pnpmVolume = `dyad-pnpm-${appId}`;
const rm = spawn("docker", ["volume", "rm", "-f", pnpmVolume], {
stdio: "pipe",
});
rm.on("close", () => resolve());
rm.on("error", () => resolve());
});
}
/**
* Stops an app based on its RunningAppInfo (container vs host) and removes it from the running map.
*/
export async function stopAppByInfo(
appId: number,
appInfo: RunningAppInfo,
): Promise<void> {
if (appInfo.isDocker) {
const containerName = appInfo.containerName || `dyad-app-${appId}`;
await stopDockerContainer(containerName);
} else {
await killProcess(appInfo.process);
}
runningApps.delete(appId);
}
/**
* Removes an app from the running apps map if it's the current process
* @param appId The app ID