github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/libnetwork/README.md (about)

     1  # libnetwork - networking for containers
     2  
     3  Libnetwork provides a native Go implementation for connecting containers
     4  
     5  The goal of libnetwork is to deliver a robust Container Network Model that provides a consistent programming interface and the required network abstractions for applications.
     6  
     7  #### Design
     8  Please refer to the [design](docs/design.md) for more information.
     9  
    10  #### Using libnetwork
    11  
    12  There are many networking solutions available to suit a broad range of use-cases. libnetwork uses a driver / plugin model to support all of these solutions while abstracting the complexity of the driver implementations by exposing a simple and consistent Network Model to users.
    13  
    14  
    15  ```go
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  	"log"
    21  
    22  	"github.com/docker/docker/pkg/reexec"
    23  	"github.com/docker/docker/libnetwork"
    24  	"github.com/docker/docker/libnetwork/config"
    25  	"github.com/docker/docker/libnetwork/netlabel"
    26  	"github.com/docker/docker/libnetwork/options"
    27  )
    28  
    29  func main() {
    30  	if reexec.Init() {
    31  		return
    32  	}
    33  
    34  	// Select and configure the network driver
    35  	networkType := "bridge"
    36  
    37  	// Create a new controller instance
    38  	driverOptions := options.Generic{}
    39  	genericOption := make(map[string]interface{})
    40  	genericOption[netlabel.GenericData] = driverOptions
    41  	controller, err := libnetwork.New(config.OptionDriverConfig(networkType, genericOption))
    42  	if err != nil {
    43  		log.Fatalf("libnetwork.New: %s", err)
    44  	}
    45  
    46  	// Create a network for containers to join.
    47  	// NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can use.
    48  	network, err := controller.NewNetwork(networkType, "network1", "")
    49  	if err != nil {
    50  		log.Fatalf("controller.NewNetwork: %s", err)
    51  	}
    52  
    53  	// For each new container: allocate IP and interfaces. The returned network
    54  	// settings will be used for container infos (inspect and such), as well as
    55  	// iptables rules for port publishing. This info is contained or accessible
    56  	// from the returned endpoint.
    57  	ep, err := network.CreateEndpoint("Endpoint1")
    58  	if err != nil {
    59  		log.Fatalf("network.CreateEndpoint: %s", err)
    60  	}
    61  
    62  	// Create the sandbox for the container.
    63  	// NewSandbox accepts Variadic optional arguments which libnetwork can use.
    64  	sbx, err := controller.NewSandbox("container1",
    65  		libnetwork.OptionHostname("test"),
    66  		libnetwork.OptionDomainname("docker.io"))
    67  	if err != nil {
    68  		log.Fatalf("controller.NewSandbox: %s", err)
    69  	}
    70  
    71  	// A sandbox can join the endpoint via the join api.
    72  	err = ep.Join(sbx)
    73  	if err != nil {
    74  		log.Fatalf("ep.Join: %s", err)
    75  	}
    76  
    77  	// libnetwork client can check the endpoint's operational data via the Info() API
    78  	epInfo, err := ep.DriverInfo()
    79  	if err != nil {
    80  		log.Fatalf("ep.DriverInfo: %s", err)
    81  	}
    82  
    83  	macAddress, ok := epInfo[netlabel.MacAddress]
    84  	if !ok {
    85  		log.Fatalf("failed to get mac address from endpoint info")
    86  	}
    87  
    88  	fmt.Printf("Joined endpoint %s (%s) to sandbox %s (%s)\n", ep.Name(), macAddress, sbx.ContainerID(), sbx.Key())
    89  }
    90  ```
    91  
    92  ## Contributing
    93  
    94  Want to hack on libnetwork? [Docker's contributions guidelines](https://github.com/docker/docker/blob/master/CONTRIBUTING.md) apply.
    95  
    96  ## Copyright and license
    97  Code and documentation copyright 2015 Docker, inc. Code released under the Apache 2.0 license. Docs released under Creative commons.