Skip to content

Commit b38615d

Browse files
authored
docker container cleanup (#689)
## 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.
2 parents f417a71 + c7cf384 commit b38615d

File tree

2 files changed

+94
-6
lines changed

2 files changed

+94
-6
lines changed

scripts/cleanup-test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env node
2+
3+
const { execSync } = require('child_process');
4+
5+
// Docker container name
6+
const CONTAINER_NAME = 'skuilder-test-couch';
7+
8+
console.log('Running container cleanup test...');
9+
10+
try {
11+
// Check if the container exists
12+
const containerExists = execSync(`docker ps -a -q -f name=^${CONTAINER_NAME}$`, { stdio: 'pipe' }).toString().trim();
13+
14+
if (containerExists) {
15+
console.log(`Found existing container '${CONTAINER_NAME}'. Cleaning up...`);
16+
17+
try {
18+
// Check if container is running
19+
const isRunning = execSync(`docker ps -q -f name=^${CONTAINER_NAME}$`, { stdio: 'pipe' }).toString().trim();
20+
if (isRunning) {
21+
console.log('Container is running. Stopping it...');
22+
execSync(`docker stop ${CONTAINER_NAME}`, { stdio: 'pipe' });
23+
console.log('Container stopped successfully.');
24+
} else {
25+
console.log('Container is not running.');
26+
}
27+
28+
// Remove the container
29+
console.log('Removing container...');
30+
execSync(`docker rm ${CONTAINER_NAME}`, { stdio: 'pipe' });
31+
console.log('Container removed successfully.');
32+
33+
} catch (error) {
34+
console.error('Error during cleanup:', error.message);
35+
process.exit(1);
36+
}
37+
} else {
38+
console.log(`No container named '${CONTAINER_NAME}' found. Nothing to clean up.`);
39+
}
40+
41+
console.log('Cleanup test completed successfully.');
42+
43+
} catch (error) {
44+
console.error('Error checking container status:', error.message);
45+
process.exit(1);
46+
}

scripts/dev-couchdb.js

100644100755
Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,57 @@ if (!validCommands.includes(command)) {
3737
process.exit(1);
3838
}
3939

40-
// Check if CouchDB is already running if command is 'start'
40+
// Docker container name constant
41+
const CONTAINER_NAME = 'skuilder-test-couch';
42+
43+
// Check if Docker is available
44+
try {
45+
execSync('docker --version', { stdio: 'pipe' });
46+
} catch (error) {
47+
console.error('Error: Docker is not available or not running.');
48+
console.error('Please ensure Docker is installed and running before using this script.');
49+
process.exit(1);
50+
}
51+
52+
// For the 'start' command, always clean up existing container first to avoid conflicts
4153
if (command === 'start') {
4254
try {
43-
const status = execSync(`${scriptPath} status`, { stdio: 'pipe' }).toString();
44-
if (status.includes('Up') || status.includes('running')) {
45-
console.log('CouchDB is already running');
46-
process.exit(0);
55+
// Check if the container exists
56+
const containerExists = execSync(`docker ps -a -q -f name=^${CONTAINER_NAME}$`, { stdio: 'pipe' }).toString().trim();
57+
58+
if (containerExists) {
59+
console.log(`Found existing container '${CONTAINER_NAME}'. Cleaning up before starting...`);
60+
61+
try {
62+
// Check if container is running
63+
const isRunning = execSync(`docker ps -q -f name=^${CONTAINER_NAME}$`, { stdio: 'pipe' }).toString().trim();
64+
if (isRunning) {
65+
console.log('Container is running. Stopping it...');
66+
execSync(`docker stop ${CONTAINER_NAME}`, { stdio: 'pipe' });
67+
console.log('Container stopped successfully.');
68+
} else {
69+
console.log('Container is not running (already stopped).');
70+
}
71+
} catch (error) {
72+
console.error('Error stopping container:', error.message);
73+
console.log('Attempting to remove anyway...');
74+
}
75+
76+
try {
77+
// Remove the container
78+
console.log('Removing container...');
79+
execSync(`docker rm ${CONTAINER_NAME}`, { stdio: 'pipe' });
80+
console.log('Container removed successfully.');
81+
} catch (error) {
82+
console.error('Error removing container:', error.message);
83+
console.error('This may cause issues when starting CouchDB.');
84+
}
85+
} else {
86+
console.log(`No container named '${CONTAINER_NAME}' found. Clean start possible.`);
4787
}
4888
} catch (error) {
49-
// If the status check fails, continue with the start command
89+
console.error('Docker command failed:', error.message);
90+
console.log('Continuing anyway, but this may cause issues.');
5091
}
5192
}
5293

@@ -55,6 +96,7 @@ console.log(`Managing CouchDB: ${command}`);
5596
const child = spawn(scriptPath, [command], {
5697
cwd: testCouchDir,
5798
stdio: 'inherit',
99+
env: { ...process.env, CONTAINER_NAME }
58100
});
59101

60102
child.on('error', (error) => {

0 commit comments

Comments
 (0)