MCPcopy Create free account
hub / github.com/CodeFox-Repo/codefox / runDockerContainer

Function runDockerContainer

frontend/src/app/api/runProject/route.ts:323–465  ·  view source on GitHub ↗

* Run Docker container using the base image

(
  projectPath: string
)

Source from the content-addressed store, hash-verified

321 * Run Docker container using the base image
322 */
323async function runDockerContainer(
324 projectPath: string
325): Promise<{ domain: string; containerId: string; port: number }> {
326 const traefikDomain = process.env.TRAEFIK_DOMAIN || 'docker.localhost';
327
328 // Check for existing container
329 const existingContainerId = await checkExistingContainer(projectPath);
330 if (existingContainerId) {
331 // Verify container is running
332 const isRunning = await checkContainerRunning(existingContainerId);
333 if (isRunning) {
334 const subdomain = projectPath.replace(/[^\w-]/g, '').toLowerCase();
335 const domain = `${subdomain}.${traefikDomain}`;
336 const containerInfo = runningContainers.get(projectPath);
337 const port = containerInfo?.port || 0;
338
339 // Update container state
340 runningContainers.set(projectPath, {
341 domain,
342 containerId: existingContainerId,
343 port,
344 timestamp: Date.now(),
345 });
346 await saveState();
347
348 return { domain, containerId: existingContainerId, port };
349 }
350
351 // If container is no longer running, try to remove it
352 try {
353 await execWithTimeout(`docker rm -f ${existingContainerId}`, {
354 timeout: 30000,
355 });
356 logger.info(`Removed non-running container: ${existingContainerId}`);
357 } catch (error) {
358 logger.error(`Error removing non-running container:`, error);
359 // Continue processing even if removal fails
360 }
361 }
362
363 // Ensure base image exists
364 await ensureBaseImageExists();
365
366 const directory = path.join(getProjectPath(projectPath), 'frontend');
367 const subdomain = projectPath.replace(/[^\w-]/g, '').toLowerCase();
368 const containerName = `container-${subdomain}`;
369 const domain = `${subdomain}.${traefikDomain}`;
370
371 // Allocate port
372 const exposedPort = await findAvailablePort();
373
374 try {
375 // Check if a container with the same name already exists, remove it if found
376 try {
377 await execWithTimeout(`docker inspect ${containerName}`, {
378 timeout: 10000,
379 });
380 logger.info(

Callers 1

GETFunction · 0.85

Calls 9

getProjectPathFunction · 0.90
checkExistingContainerFunction · 0.85
checkContainerRunningFunction · 0.85
saveStateFunction · 0.85
execWithTimeoutFunction · 0.85
ensureBaseImageExistsFunction · 0.85
findAvailablePortFunction · 0.85
getMethod · 0.80
setMethod · 0.80

Tested by

no test coverage detected