Class: DC::Container

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/dc.rb

Defined Under Namespace

Classes: PortMap

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ports: []) ⇒ Container

Returns a new instance of Container.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dc.rb', line 7

def initialize name, ports: []
  @name = name
  @container = if ports.empty?
    Docker::Container.create name: name, Image: name
  else
    port_maps = ports.map do |port|
      if port.is_a? PortMap
        port
      else
        to, from = port.split ':'
        PortMap.new from: from, to: to
      end
    end

    Docker::Container.create(
      name: name, Image: name,
      ExposedPorts: port_maps.map do |map|
        ["#{map.internal_port}/tcp", {}]
      end.to_h,
      HostConfig: {
        PortBindings: port_maps.map do |map|
          array = [{ HostPort: map.external_port, HostIp: map.external_ip }]
          ["#{map.internal_port}/tcp", array]
        end.to_h
      }
    )
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/dc.rb', line 36

def name
  @name
end

Instance Method Details

#destroyObject



45
46
47
# File 'lib/dc.rb', line 45

def destroy
  kill!.remove
end

#internal_containerObject



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

def internal_container
  @container
end