Class: Docker::API::Network

Inherits:
Resource show all
Defined in:
lib/docker/api/resources/network.rb

Overview

A network on the daemon.

Instance Attribute Summary

Attributes inherited from Resource

#client, #raw

Instance Method Summary collapse

Methods inherited from Resource

#==, #[], #hash, #id, #initialize, #partial?, #stale?, #to_s

Constructor Details

This class inherits a constructor from Docker::API::Resource

Instance Method Details

#connect(container, aliases: [], ipv4_address: nil, ipv6_address: nil) ⇒ self

Attach a container to this network.

Examples:

network.connect(container, aliases: %w{web web.local})

Parameters:

  • container (String, Docker::API::Container)

    the container

  • aliases (Array<String>) (defaults to: [])

    additional names it answers to here

  • ipv4_address (String, nil) (defaults to: nil)

    a fixed address to assign

  • ipv6_address (String, nil) (defaults to: nil)

    a fixed address to assign

Returns:

  • (self)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/docker/api/resources/network.rb', line 50

def connect(container, aliases: [], ipv4_address: nil, ipv6_address: nil)
  endpoint = {}
  endpoint["Aliases"] = aliases unless aliases.nil? || aliases.empty?
  endpoint["IPAMConfig"] = {
    "IPv4Address" => ipv4_address, "IPv6Address" => ipv6_address
  }.compact
  endpoint.delete("IPAMConfig") if endpoint["IPAMConfig"].empty?

  body = { "Container" => container_id(container) }
  body["EndpointConfig"] = endpoint unless endpoint.empty?

  operations.network_connect(id: id, body: body)
  self
end

#containersHash

Returns containers attached to this network, keyed by id.

Returns:

  • (Hash)

    containers attached to this network, keyed by id



26
27
28
# File 'lib/docker/api/resources/network.rb', line 26

def containers
  detail("Containers") || {}
end

#disconnect(container, force: false) ⇒ self

Parameters:

  • container (String, Docker::API::Container)

    the container

  • force (Boolean) (defaults to: false)

    disconnect even if the container is running

Returns:

  • (self)


68
69
70
71
72
73
# File 'lib/docker/api/resources/network.rb', line 68

def disconnect(container, force: false)
  operations.network_disconnect(
    id: id, body: { "Container" => container_id(container), "Force" => force }
  )
  self
end

#driverString?

Returns "bridge", "overlay", "host", and so on.

Returns:

  • (String, nil)

    "bridge", "overlay", "host", and so on



16
17
18
# File 'lib/docker/api/resources/network.rb', line 16

def driver
  detail("Driver")
end

#ipv6?Boolean

Returns whether the network has IPv6 enabled.

Returns:

  • (Boolean)

    whether the network has IPv6 enabled



21
22
23
# File 'lib/docker/api/resources/network.rb', line 21

def ipv6?
  detail("EnableIPv6") == true
end

#nameString?

Returns the network's name.

Returns:

  • (String, nil)

    the network's name



11
12
13
# File 'lib/docker/api/resources/network.rb', line 11

def name
  detail("Name")
end

#reloadself

Returns:

  • (self)


36
37
38
# File 'lib/docker/api/resources/network.rb', line 36

def reload
  replace_raw(operations.network_inspect(id: id || name).json)
end

#removevoid

This method returns an undefined value.



76
77
78
79
# File 'lib/docker/api/resources/network.rb', line 76

def remove
  operations.network_delete(id: id)
  nil
end

#subnetsArray<Hash>

Returns the network's IPAM configuration.

Returns:

  • (Array<Hash>)

    the network's IPAM configuration



31
32
33
# File 'lib/docker/api/resources/network.rb', line 31

def subnets
  detail("IPAM.Config") || []
end