* Run Docker container using the base image
( projectPath: string )
| 321 | * Run Docker container using the base image |
| 322 | */ |
| 323 | async 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( |
no test coverage detected