From 6ff56c8691de8b9d60bf3d609af2405e84b90a52 Mon Sep 17 00:00:00 2001 From: Christopher Horrell Date: Tue, 10 Feb 2026 16:34:00 -0500 Subject: [PATCH] Fix unsafe container cleanup in delete_image Previously, delete_image stopped ALL containers on the system before removing test images, which could disrupt unrelated containers. Changes: - Only stop and remove containers created from the specific test image - Match containers using substring comparison to handle sha256: prefix - Check container state before stopping to avoid unnecessary operations - Handle NotModifiedError for already-stopped containers This makes the test suite safe to run on development machines where other Docker containers may be running. Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> --- spec/spec_helper.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index abda1b7..ecf1ced 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -15,9 +15,21 @@ def create_image(version) def delete_image puts "Deleting image..." - # Stop all containers so we can remove the image - for container in Docker::Container.all(:all => true) - container.stop + # Stop and remove only containers created from this image + Docker::Container.all(:all => true).each do |container| + container_image = container.info['Image'] + container_image_id = container.info['ImageID'] + + # Match image IDs - container IDs may have sha256: prefix and be full hashes + # while @image.id is the short ID + if container_image&.include?(@image.id) || container_image_id&.include?(@image.id) + begin + container.stop unless container.info['State'] == 'exited' + container.delete(:force => true) + rescue Docker::Error::NotModifiedError + # Container already stopped, ignore + end + end end @image.remove(:force => true)