@@ -286,11 +286,77 @@ check_services_running() {
286286 log_success " Monitoring services are running"
287287}
288288
289+ # Clean up any existing containers that might conflict
290+ cleanup_existing_containers () {
291+ log_info " Checking for existing containers that might conflict..."
292+
293+ # List of container names that might conflict
294+ local container_names=(
295+ " sink-postgres"
296+ " sink-prometheus"
297+ " pgwatch-postgres"
298+ " pgwatch-prometheus"
299+ " grafana"
300+ " flask-backend"
301+ " target-db"
302+ " postgres-reports"
303+ " sources-generator"
304+ )
305+
306+ # Check for containers matching our service names
307+ local existing_containers=" "
308+
309+ for container_name in " ${container_names[@]} " ; do
310+ local found_containers
311+ found_containers=$( docker ps -a --filter " name=$container_name " --format " {{.Names}}" 2> /dev/null || true)
312+ if [ -n " $found_containers " ]; then
313+ if [ -n " $existing_containers " ]; then
314+ existing_containers=" $existing_containers " $' \n ' " $found_containers "
315+ else
316+ existing_containers=" $found_containers "
317+ fi
318+ fi
319+ done
320+
321+ if [ -n " $existing_containers " ]; then
322+ log_warning " Found existing containers from previous installations:"
323+ echo " $existing_containers " | sed ' s/^/ • /'
324+ echo
325+ log_warning " These containers may conflict with the new installation."
326+ log_warning " Removing them will stop any running services and delete their data."
327+ echo
328+
329+ read -p " Are you sure you want to remove these existing containers? (Y/n): " -n 1 -r
330+ echo
331+
332+ if [[ ! $REPLY =~ ^[Nn]$ ]]; then
333+ log_info " Stopping and removing existing containers..."
334+
335+ # Stop containers first
336+ echo " $existing_containers " | xargs -r docker stop 2> /dev/null || true
337+
338+ # Remove containers
339+ echo " $existing_containers " | xargs -r docker rm 2> /dev/null || true
340+
341+ # Remove any orphaned volumes
342+ docker volume prune -f 2> /dev/null || true
343+
344+ log_success " Existing containers removed"
345+ else
346+ log_warning " Keeping existing containers - this may cause conflicts during installation"
347+ log_info " If you encounter issues, run '$0 reset' to clean up manually"
348+ fi
349+ else
350+ log_success " No conflicting containers found"
351+ fi
352+ }
353+
289354# Comprehensive precheck for installation commands
290355precheck_for_install () {
291356 check_prerequisites
292357 check_docker_running
293358 check_system_resources
359+ cleanup_existing_containers
294360}
295361
296362# Precheck for service management commands
@@ -1187,13 +1253,28 @@ reset_environment() {
11871253 cd " $SCRIPT_DIR "
11881254
11891255 log_info " Stopping services and removing data..."
1190- $compose_cmd -f " $COMPOSE_FILE " down -v
1256+
1257+ # Stop and remove containers with volumes
1258+ $compose_cmd -f " $COMPOSE_FILE " down -v --remove-orphans
1259+
1260+ # Force remove any remaining containers by name pattern
1261+ local remaining_containers
1262+ remaining_containers=$( docker ps -a --filter " name=postgres-ai-mon" --format " {{.Names}}" 2> /dev/null || true)
1263+
1264+ if [ -n " $remaining_containers " ]; then
1265+ log_info " Removing any remaining containers..."
1266+ echo " $remaining_containers " | xargs -r docker stop 2> /dev/null || true
1267+ echo " $remaining_containers " | xargs -r docker rm 2> /dev/null || true
1268+ fi
1269+
1270+ # Remove any orphaned volumes
1271+ docker volume ls --filter " name=postgres-ai-mon" --format " {{.Name}}" | xargs -r docker volume rm 2> /dev/null || true
11911272
11921273 # Remove any generated config files
11931274 rm -f " $SCRIPT_DIR /config/pgwatch-postgres/sources.yml"
11941275 rm -f " $SCRIPT_DIR /config/pgwatch-prometheus/sources.yml"
11951276
1196- log_success " Environment reset completed"
1277+ log_success " Environment reset completed - all containers and data removed "
11971278 else
11981279 log_info " Reset cancelled"
11991280 fi
0 commit comments