class Docker::Container

Attributes

id[R]
removal_instructions[R]
to_s[R]

Public Class Methods

load_from(container) click to toggle source
# File lib/docker/container.rb, line 26
def self.load_from(container)
  new(container.id) if container.from_a?(self)
end
new(id) click to toggle source
# File lib/docker/container.rb, line 6
def initialize(id)
  @id = Docker::ID(id) 
end
run(image, flags = "", command = "", path = nil) click to toggle source
# File lib/docker/container.rb, line 10
def self.run(image, flags = "", command = "", path = nil)
  path  = Docker::PATH(path)
  flags = "#{flags} -e DOCKER_TYPE=#{self}"
  flags = "#{flags} -e LOCAL_PATH=#{path.path}" if path

  id = Docker::ID(`docker run #{flags} #{image} #{command}`)

  # Nasty hack to return a removed container if the --rm flag
  # is passed and the container alread exited
  return new(nil).disable! unless id || flags !~ /--rm/

  id ||= Docker.containers(:stopped).first.id

  new(id) 
end

Public Instance Methods

add_removal_instructions(&block) click to toggle source
# File lib/docker/container.rb, line 52
def add_removal_instructions(&block)
  @removal_instructions = Proc.new { block.call(self) }
  self
end
attach!() click to toggle source
# File lib/docker/container.rb, line 57
def attach!
  `docker attach --sig-proxy=false #{id}`
end
disable!() click to toggle source
# File lib/docker/container.rb, line 69
def disable!
  self.instance_eval { 
    undef :attach!
    undef :remove!
    undef :add_removal_instructions
  }
  self.instance_eval { def state; @id; end } 
  @id   = 'REMOVED'
  self
end
exists_in?(state) click to toggle source
# File lib/docker/container.rb, line 88
def exists_in?(state)
  Docker.containers(state).any?  { |container| container == self }
end
from_a?(klass) click to toggle source
# File lib/docker/container.rb, line 80
def from_a?(klass)
  env["DOCKER_TYPE"] == klass.to_s
end
from_klass() click to toggle source
# File lib/docker/container.rb, line 43
def from_klass
  return nil unless klass = env["DOCKER_TYPE"]
  Kernel.const_get(klass)
end
inspekt() click to toggle source
# File lib/docker/container.rb, line 34
def inspekt
  Docker.inspekt(id)
end
ip() click to toggle source
# File lib/docker/container.rb, line 93
def ip; ip_address; end
logs() click to toggle source
# File lib/docker/container.rb, line 30
def logs
  @logs || `docker logs #{id}` # @logs given with #disable!
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/docker/container.rb, line 96
def method_missing(method, *args, &block)
  super if removed?
  inspekt_methods = [:env, :state, :ports, :ip_address, :name]
  state_methods   = [:running? , :stopped?, :paused?]
  action_methods  = [:stop!, :start!, :restart!, :pause!, :unpause!]
  if inspekt_methods.include?(method)
    inspekt.__send__(method) 
  elsif state_methods.include?(method)
    method = method.to_s.delete('?').to_sym
    exists_in?(method) 
  elsif action_methods.include?(method)
    method = method.to_s.delete('!').to_sym
    `docker #{method} #{id}`
  else
    super
  end
end
path() click to toggle source
# File lib/docker/container.rb, line 48
def path
  Docker::PATH(env["LOCAL_PATH"])
end
remove!() click to toggle source
# File lib/docker/container.rb, line 61
def remove!
  @logs = logs
  @removal_instructions.call if @removal_instructions
  `docker rm -f #{id}`
  disable!
  id
end
removed?() click to toggle source
# File lib/docker/container.rb, line 84
def removed?
  id == "REMOVED"
end
state() click to toggle source
# File lib/docker/container.rb, line 75
def state; @id; end
to_klass() click to toggle source
# File lib/docker/container.rb, line 38
def to_klass
  return self unless klass = from_klass
  klass.load_from(self)
end
up?() click to toggle source
# File lib/docker/container.rb, line 92
def up?; running?; end