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.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dc.rb', line 43

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.



72
73
74
# File 'lib/dc.rb', line 72

def name
  @name
end

Instance Method Details

#destroyObject



81
82
83
# File 'lib/dc.rb', line 81

def destroy
  kill!.remove
end

#internal_containerObject



74
75
76
# File 'lib/dc.rb', line 74

def internal_container
  @container
end