Class: Docker::API::Networks

Inherits:
Collection show all
Defined in:
lib/docker/api/collections/networks.rb

Overview

The networks on a daemon.

Instance Attribute Summary

Attributes inherited from Collection

#client

Instance Method Summary collapse

Methods inherited from Collection

#exist?, #find, #initialize, #to_s

Constructor Details

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

Instance Method Details

#all(filters: nil) ⇒ Array<Docker::API::Network>

Returns partial resources.

Parameters:

  • filters (Hash, nil) (defaults to: nil)

    daemon-side filters

Returns:



12
13
14
15
# File 'lib/docker/api/collections/networks.rb', line 12

def all(filters: nil)
  operations.network_list(filters: filters)
    .json!.map { |payload| Network.new(client: client, raw: payload, partial: true) }
end

#create(name, driver: nil, ipv6: false, internal: false, attachable: false, labels: nil, ipam: nil, options: nil, **attributes) ⇒ Docker::API::Network

Create a network.

Examples:

client.networks.create("dokken", ipv6: true,
  ipam: { "Config" => [{ "Subnet" => "fd00::/64" }] })

Parameters:

  • name (String)

    the network's name

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

    "bridge", "overlay", and so on

  • ipv6 (Boolean) (defaults to: false)

    enable IPv6 addressing

  • internal (Boolean) (defaults to: false)

    withhold external access

  • attachable (Boolean) (defaults to: false)

    allow manual container attachment

  • labels (Hash, nil) (defaults to: nil)

    labels for the network

  • ipam (Hash, nil) (defaults to: nil)

    an IPAM configuration

  • options (Hash, nil) (defaults to: nil)

    driver-specific options

  • attributes (Hash)

    any further body attributes

Returns:



46
47
48
49
50
51
52
53
54
55
# File 'lib/docker/api/collections/networks.rb', line 46

def create(name, driver: nil, ipv6: false, internal: false, attachable: false,
  labels: nil, ipam: nil, options: nil, **attributes)
  body = Body.build(attributes).merge(
    "Name" => name, "Driver" => driver, "EnableIPv6" => ipv6,
    "Internal" => internal, "Attachable" => attachable,
    "Labels" => labels, "IPAM" => ipam, "Options" => options
  ).compact

  get(operations.network_create(body: body).json!["Id"])
end

#ensure(name, **attributes) ⇒ Docker::API::Network

Fetch a network by name, creating it if it does not exist.

Two processes racing to create the same shared network is ordinary rather than exceptional -- parallel test suites do it constantly -- so losing that race is treated as success and the winner's network is returned.

Parameters:

  • name (String)

    the network's name

  • attributes (Hash)

    passed to #create when creating

Returns:



67
68
69
70
71
72
73
74
75
76
# File 'lib/docker/api/collections/networks.rb', line 67

def ensure(name, **attributes)
  found = find(name)
  return found if found

  begin
    create(name, **attributes)
  rescue Conflict
    get(name)
  end
end

#get(id, verbose: false, scope: nil) ⇒ Docker::API::Network

Parameters:

  • id (String)

    a name or id

  • verbose (Boolean) (defaults to: false)

    include service details

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

    "swarm", "global" or "local"

Returns:

Raises:



22
23
24
25
26
27
28
# File 'lib/docker/api/collections/networks.rb', line 22

def get(id, verbose: false, scope: nil)
  Network.new(
    client: client,
    raw: operations.network_inspect(id: id, verbose: verbose, scope: scope).json,
    partial: false
  )
end

#prune(filters: nil) ⇒ Hash

Returns what was deleted.

Parameters:

  • filters (Hash, nil) (defaults to: nil)

    which networks to consider

Returns:

  • (Hash)

    what was deleted



80
81
82
# File 'lib/docker/api/collections/networks.rb', line 80

def prune(filters: nil)
  operations.network_prune(filters: filters).json
end