Class: Sidedock::Container

Inherits:
Base
  • Object
show all
Defined in:
lib/sidedock/container.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#machine, machine

Constructor Details

#initialize(id, port_mapping: {}, **options) ⇒ Container

Returns a new instance of Container.



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

def initialize(id, port_mapping: {}, **options)
  @id = id
  @port_mapping = port_mapping
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/sidedock/container.rb', line 3

def id
  @id
end

#portsObject

Returns the value of attribute ports.



3
4
5
# File 'lib/sidedock/container.rb', line 3

def ports
  @ports
end

Class Method Details

.create(image_name_or_id, options) ⇒ Object



10
11
12
13
14
# File 'lib/sidedock/container.rb', line 10

def self.create(image_name_or_id, options)
  raise "No image name given" unless image_name_or_id.present?
  id = machine.execute "create -P #{image_name_or_id}"
  new id, **options
end

.using_image(image) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/sidedock/container.rb', line 46

def self.using_image(image)
  ps_output = machine.execute "ps -a -q --filter ancestor=#{image.id}"

  ps_output.each_line.map do |id|
    new id
  end
end

Instance Method Details

#bash(command) ⇒ Object



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

def bash(command)
  machine.execute "exec -t #{@id} bash -c '#{command}'"
end

#ipObject



42
43
44
# File 'lib/sidedock/container.rb', line 42

def ip
  machine.ip
end

#removeObject



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

def remove
  machine.execute "rm -f #{@id}"
end

#running?Boolean

Returns:

  • (Boolean)


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

def running?
  machine.execute('ps -q --no-trunc').include? @id
end

#startObject



16
17
18
19
# File 'lib/sidedock/container.rb', line 16

def start
  raise "already started" if running?
  machine.execute "start #{@id}"
end

#stopObject



21
22
23
24
# File 'lib/sidedock/container.rb', line 21

def stop
  raise "not running" unless running?
  machine.execute "stop #{@id}"
end