From c7cf384c598b08d5dbbbfd97c300ed9095ae7349 Mon Sep 17 00:00:00 2001 From: NiloCK Date: Fri, 16 May 2025 09:42:38 -0300 Subject: [PATCH] docker container cleanup ## Problem When working with git worktrees or multiple clones of the repository on the same machine, the CouchDB Docker container setup can cause conflicts. This happens because: 1. Multiple worktrees try to use the same container name (`skuilder-test-couch`) 2. Docker attempts to mount volumes to paths that may already exist in other containers 3. This results in errors like: ``` Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/path/to/local.ini" to rootfs at "/opt/couchdb/etc/local.ini" ``` ## Solution Automatic cleanup process in the `dev-couchdb.js` script that runs before starting CouchDB. This process: 1. Checks if a container with the name `skuilder-test-couch` already exists 2. If it exists: - Stops the container if it's running - Removes the container 3. Continues with the normal start process using the test-couch.sh script This ensures that each time you start CouchDB, you get a fresh container without conflicts from previous runs or other worktrees. --- scripts/cleanup-test.js | 46 +++++++++++++++++++++++++++++++++++ scripts/dev-couchdb.js | 54 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 94 insertions(+), 6 deletions(-) create mode 100755 scripts/cleanup-test.js mode change 100644 => 100755 scripts/dev-couchdb.js diff --git a/scripts/cleanup-test.js b/scripts/cleanup-test.js new file mode 100755 index 000000000..c1a9e1b68 --- /dev/null +++ b/scripts/cleanup-test.js @@ -0,0 +1,46 @@ +#!/usr/bin/env node + +const { execSync } = require('child_process'); + +// Docker container name +const CONTAINER_NAME = 'skuilder-test-couch'; + +console.log('Running container cleanup test...'); + +try { + // Check if the container exists + const containerExists = execSync(`docker ps -a -q -f name=^${CONTAINER_NAME}$`, { stdio: 'pipe' }).toString().trim(); + + if (containerExists) { + console.log(`Found existing container '${CONTAINER_NAME}'. Cleaning up...`); + + try { + // Check if container is running + const isRunning = execSync(`docker ps -q -f name=^${CONTAINER_NAME}$`, { stdio: 'pipe' }).toString().trim(); + if (isRunning) { + console.log('Container is running. Stopping it...'); + execSync(`docker stop ${CONTAINER_NAME}`, { stdio: 'pipe' }); + console.log('Container stopped successfully.'); + } else { + console.log('Container is not running.'); + } + + // Remove the container + console.log('Removing container...'); + execSync(`docker rm ${CONTAINER_NAME}`, { stdio: 'pipe' }); + console.log('Container removed successfully.'); + + } catch (error) { + console.error('Error during cleanup:', error.message); + process.exit(1); + } + } else { + console.log(`No container named '${CONTAINER_NAME}' found. Nothing to clean up.`); + } + + console.log('Cleanup test completed successfully.'); + +} catch (error) { + console.error('Error checking container status:', error.message); + process.exit(1); +} \ No newline at end of file diff --git a/scripts/dev-couchdb.js b/scripts/dev-couchdb.js old mode 100644 new mode 100755 index b0e103e6e..80619a73b --- a/scripts/dev-couchdb.js +++ b/scripts/dev-couchdb.js @@ -37,16 +37,57 @@ if (!validCommands.includes(command)) { process.exit(1); } -// Check if CouchDB is already running if command is 'start' +// Docker container name constant +const CONTAINER_NAME = 'skuilder-test-couch'; + +// Check if Docker is available +try { + execSync('docker --version', { stdio: 'pipe' }); +} catch (error) { + console.error('Error: Docker is not available or not running.'); + console.error('Please ensure Docker is installed and running before using this script.'); + process.exit(1); +} + +// For the 'start' command, always clean up existing container first to avoid conflicts if (command === 'start') { try { - const status = execSync(`${scriptPath} status`, { stdio: 'pipe' }).toString(); - if (status.includes('Up') || status.includes('running')) { - console.log('CouchDB is already running'); - process.exit(0); + // Check if the container exists + const containerExists = execSync(`docker ps -a -q -f name=^${CONTAINER_NAME}$`, { stdio: 'pipe' }).toString().trim(); + + if (containerExists) { + console.log(`Found existing container '${CONTAINER_NAME}'. Cleaning up before starting...`); + + try { + // Check if container is running + const isRunning = execSync(`docker ps -q -f name=^${CONTAINER_NAME}$`, { stdio: 'pipe' }).toString().trim(); + if (isRunning) { + console.log('Container is running. Stopping it...'); + execSync(`docker stop ${CONTAINER_NAME}`, { stdio: 'pipe' }); + console.log('Container stopped successfully.'); + } else { + console.log('Container is not running (already stopped).'); + } + } catch (error) { + console.error('Error stopping container:', error.message); + console.log('Attempting to remove anyway...'); + } + + try { + // Remove the container + console.log('Removing container...'); + execSync(`docker rm ${CONTAINER_NAME}`, { stdio: 'pipe' }); + console.log('Container removed successfully.'); + } catch (error) { + console.error('Error removing container:', error.message); + console.error('This may cause issues when starting CouchDB.'); + } + } else { + console.log(`No container named '${CONTAINER_NAME}' found. Clean start possible.`); } } catch (error) { - // If the status check fails, continue with the start command + console.error('Docker command failed:', error.message); + console.log('Continuing anyway, but this may cause issues.'); } } @@ -55,6 +96,7 @@ console.log(`Managing CouchDB: ${command}`); const child = spawn(scriptPath, [command], { cwd: testCouchDir, stdio: 'inherit', + env: { ...process.env, CONTAINER_NAME } }); child.on('error', (error) => {