Class: Docker::Container

Inherits:
Object
  • Object
show all
Includes:
Equality
Defined in:
lib/docker/container.rb

Direct Known Subclasses

Postgres, Rails::Console, Rails::Server

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Equality

#==

Constructor Details

#initialize(id) ⇒ Container

Returns a new instance of Container.



6
7
8
# File 'lib/docker/container.rb', line 6

def initialize(id)
  @id = Docker::ID(id) 
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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

Instance Attribute Details

#idObject (readonly) Also known as: to_s

Returns the value of attribute id.



5
6
7
# File 'lib/docker/container.rb', line 5

def id
  @id
end

#removal_instructionsObject (readonly)

Returns the value of attribute removal_instructions.



5
6
7
# File 'lib/docker/container.rb', line 5

def removal_instructions
  @removal_instructions
end

Class Method Details

.load_from(container) ⇒ Object



26
27
28
# File 'lib/docker/container.rb', line 26

def self.load_from(container)
  new(container.id) if container.from_a?(self)
end

.run(image, flags = "", command = "", path = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Method Details

#add_removal_instructions(&block) ⇒ Object



52
53
54
55
# File 'lib/docker/container.rb', line 52

def add_removal_instructions(&block)
  @removal_instructions = Proc.new { block.call(self) }
  self
end

#attach!Object



57
58
59
# File 'lib/docker/container.rb', line 57

def attach!
  `docker attach --sig-proxy=false #{id}`
end

#disable!Object



69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/docker/container.rb', line 88

def exists_in?(state)
  Docker.containers(state).any?  { |container| container == self }
end

#from_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/docker/container.rb', line 80

def from_a?(klass)
  env["DOCKER_TYPE"] == klass.to_s
end

#from_klassObject



43
44
45
46
# File 'lib/docker/container.rb', line 43

def from_klass
  return nil unless klass = env["DOCKER_TYPE"]
  Kernel.const_get(klass)
end

#inspektObject



34
35
36
# File 'lib/docker/container.rb', line 34

def inspekt
  Docker.inspekt(id)
end

#ipObject



93
# File 'lib/docker/container.rb', line 93

def ip; ip_address; end

#logsObject



30
31
32
# File 'lib/docker/container.rb', line 30

def logs
  @logs || `docker logs #{id}` # @logs given with #disable!
end

#pathObject



48
49
50
# File 'lib/docker/container.rb', line 48

def path
  Docker::PATH(env["LOCAL_PATH"])
end

#remove!Object



61
62
63
64
65
66
67
# 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?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/docker/container.rb', line 84

def removed?
  id == "REMOVED"
end

#to_klassObject



38
39
40
41
# File 'lib/docker/container.rb', line 38

def to_klass
  return self unless klass = from_klass
  klass.load_from(self)
end

#up?Boolean

Returns:

  • (Boolean)


92
# File 'lib/docker/container.rb', line 92

def up?; running?; end