Skip to content

Commit 6ff56c8

Browse files
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>
1 parent 455980a commit 6ff56c8

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

spec/spec_helper.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,21 @@ def create_image(version)
1515

1616
def delete_image
1717
puts "Deleting image..."
18-
# Stop all containers so we can remove the image
19-
for container in Docker::Container.all(:all => true)
20-
container.stop
18+
# Stop and remove only containers created from this image
19+
Docker::Container.all(:all => true).each do |container|
20+
container_image = container.info['Image']
21+
container_image_id = container.info['ImageID']
22+
23+
# Match image IDs - container IDs may have sha256: prefix and be full hashes
24+
# while @image.id is the short ID
25+
if container_image&.include?(@image.id) || container_image_id&.include?(@image.id)
26+
begin
27+
container.stop unless container.info['State'] == 'exited'
28+
container.delete(:force => true)
29+
rescue Docker::Error::NotModifiedError
30+
# Container already stopped, ignore
31+
end
32+
end
2133
end
2234

2335
@image.remove(:force => true)

0 commit comments

Comments
 (0)