Skip to content

Commit fe4f432

Browse files
committed
Improve container cleanup to handle short image IDs and running containers
- Match both short (12 char) and long image IDs - Check container state before stopping to avoid unnecessary operations - Handle NotModifiedError when container is already stopped - Fixes CI failure where running containers prevented image removal
1 parent 062844d commit fe4f432

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

spec/spec_helper.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ def delete_image
1717
puts "Deleting image..."
1818
# Stop and remove only containers created from this image
1919
Docker::Container.all(:all => true).each do |container|
20-
if container.info['Image'] == @image.id || container.info['ImageID'] == @image.id
21-
container.stop
22-
container.delete(:force => true)
20+
image_id = container.info['Image']
21+
# Match both short and long image IDs
22+
if image_id == @image.id || image_id == @image.id[0..11] || container.info['ImageID'] == @image.id
23+
begin
24+
container.stop unless container.info['State'] == 'exited'
25+
container.delete(:force => true)
26+
rescue Docker::Error::NotModifiedError
27+
# Container already stopped, ignore
28+
end
2329
end
2430
end
2531

0 commit comments

Comments
 (0)