Class: Dockerspec::Builder::ImageGC

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/dockerspec/builder/image_gc.rb

Overview

A class to manage docker image deletion. The class stores the images created by Dockerspec::Builder objects and deletes them at the end of the Ruby/RSpec run.

Instance Method Summary collapse

Constructor Details

#initializeImageGC

The Image Garbage Collector constructor.



38
39
40
41
# File 'lib/dockerspec/builder/image_gc.rb', line 38

def initialize
  @images = []
  ObjectSpace.define_finalizer(self, proc { finalize })
end

Instance Method Details

#add(image) ⇒ Object

Adds a Docker image to be garbage deleted at the end.

Parameters:

  • image (String)

    Docker image ID.

Returns:

  • void



52
53
54
# File 'lib/dockerspec/builder/image_gc.rb', line 52

def add(image)
  @images << image
end

#finalizeObject

Removes all the Docker images.

Automatically called at the end of the RSpec/Ruby run.

Returns:

  • void



65
66
67
68
# File 'lib/dockerspec/builder/image_gc.rb', line 65

def finalize
  @images.each { |i| ::Docker::Image.remove(i, force: true) }
  @images = []
end