Class: Dockerspec::Runner

Inherits:
Object
  • Object
show all
Includes:
Helper::MultipleSourcesDescription
Defined in:
lib/dockerspec/runner.rb

Overview

This class runs a docker image (without using Serverspec for that).

This class is not used much, only inherited by Serverspec::Runner, which uses Serverspec to run the images. Some of the methods here are used there, others not.

Direct Known Subclasses

Serverspec::Runner

Instance Method Summary collapse

Methods included from Helper::MultipleSourcesDescription

#description

Constructor Details

#initialize(*opts) ⇒ Dockerspec::Runner

Constructs a Docker runner class to run Docker images.

Examples:

From a Running Docker Image

Dockerspec::Runner.new('debian:8') #=> #<Dockerspec::Runner:0x0124>

From a Running Docker Container ID

# This does not start any new container
Dockerspec::Runner.new(id: 'c51f86c28340')
  #=> #<Dockerspec::Runner:0x0124>

From a Running Docker Container Image Name

Dockerspec::Runner.new('my-debian') #=> #<Dockerspec::Runner:0x0125>

Parameters:

  • opts (String, Hash)

    The :tag or a list of options.

Options Hash (*opts):

  • :tag (String)

    The Docker image tag name to run.

  • :id (String)

    The Docker container ID to use instead of starting a new container.

  • :rm (Boolean) — default: calculated

    Whether to remove the Docker container afterwards.

  • :path (String)

    The environment PATH value of the container.

  • :env (Hash, Array)

    Some ENV instructions to add to the container.



67
68
69
70
71
# File 'lib/dockerspec/runner.rb', line 67

def initialize(*opts)
  @options = parse_options(opts)
  send("setup_from_#{source}", @options[source])
  ObjectSpace.define_finalizer(self, proc { finalize })
end

Instance Method Details

#finalizeObject

Stops and deletes the Docker Container.

Automatically called when :rm option is enabled.

Returns:

  • void



135
136
137
138
139
# File 'lib/dockerspec/runner.rb', line 135

def finalize
  return unless @options[:rm] && !@container.nil?
  @container.stop
  @container.delete
end

#idString

Gets the Docker container ID.

Examples:

builder = Dockerspec::Builder.new('.').build
runner = Dockerspec::Runner.new(builder).run
runner.id #=> "b8ba0befc716[...]"

Returns:

  • (String)

    Container ID.



104
105
106
107
# File 'lib/dockerspec/runner.rb', line 104

def id
  return nil unless @container.respond_to?(:id)
  @container.id
end

#image_idString

Gets the Docker image ID.

Examples:

builder = Dockerspec::Builder.new('.').build
runner = Dockerspec::Runner.new(builder)
runner.image_id #=> "c51f86c28340[...]"

Returns:

  • (String)

    Image ID.



121
122
123
124
# File 'lib/dockerspec/runner.rb', line 121

def image_id
  return @build.id unless @build.nil?
  @container.json['Image']
end

#runDockerspec::Runner

Runs the Docker Container.

Examples:

builder = Dockerspec::Builder.new('.')
builder.build
runner = Dockerspec::Runner.new(builder)
runner.run #=> #<Dockerspec::Runner:0x0123>

Returns:



86
87
88
89
90
# File 'lib/dockerspec/runner.rb', line 86

def run
  create_container
  run_container
  self
end

#to_sString

Gets a descriptions of the object.

Examples:

Running from a Container Image ID

r = Dockerspec::Runner.new('debian')
r.to_s #=> "Docker Run from tag: \"debian\""

Attaching to a Running Container ID

r = Dockerspec::Runner.new(id: '92cc98ab560a')
r.to_s #=> "Docker Run from id: \"92cc98ab560a\""

Returns:

  • (String)

    The object description.



156
157
158
# File 'lib/dockerspec/runner.rb', line 156

def to_s
  description('Docker Run from')
end