github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/libnetwork/README.md (about) 1 > **Warning** 2 > libnetwork was moved to https://github.com/moby/moby/tree/master/libnetwork 3 > 4 > libnetwork has been merged to the main repo of Moby since Docker 22.06. 5 > 6 > The old libnetwork repo (https://github.com/moby/libnetwork) now only accepts PR for Docker 20.10, 7 > and will be archived after the EOL of Docker 20.10. 8 9 - - - 10 # libnetwork - networking for containers 11 12 [](https://circleci.com/gh/docker/libnetwork/tree/master) [](https://coveralls.io/r/docker/libnetwork) [](https://godoc.org/github.com/docker/libnetwork) [](https://goreportcard.com/report/github.com/docker/libnetwork) 13 14 Libnetwork provides a native Go implementation for connecting containers 15 16 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. 17 18 #### Design 19 Please refer to the [design](docs/design.md) for more information. 20 21 #### Using libnetwork 22 23 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. 24 25 26 ```go 27 import ( 28 "fmt" 29 "log" 30 31 "github.com/docker/docker/pkg/reexec" 32 "github.com/docker/libnetwork" 33 "github.com/docker/libnetwork/config" 34 "github.com/docker/libnetwork/netlabel" 35 "github.com/docker/libnetwork/options" 36 ) 37 38 func main() { 39 if reexec.Init() { 40 return 41 } 42 43 // Select and configure the network driver 44 networkType := "bridge" 45 46 // Create a new controller instance 47 driverOptions := options.Generic{} 48 genericOption := make(map[string]interface{}) 49 genericOption[netlabel.GenericData] = driverOptions 50 controller, err := libnetwork.New(config.OptionDriverConfig(networkType, genericOption)) 51 if err != nil { 52 log.Fatalf("libnetwork.New: %s", err) 53 } 54 55 // Create a network for containers to join. 56 // NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can use. 57 network, err := controller.NewNetwork(networkType, "network1", "") 58 if err != nil { 59 log.Fatalf("controller.NewNetwork: %s", err) 60 } 61 62 // For each new container: allocate IP and interfaces. The returned network 63 // settings will be used for container infos (inspect and such), as well as 64 // iptables rules for port publishing. This info is contained or accessible 65 // from the returned endpoint. 66 ep, err := network.CreateEndpoint("Endpoint1") 67 if err != nil { 68 log.Fatalf("network.CreateEndpoint: %s", err) 69 } 70 71 // Create the sandbox for the container. 72 // NewSandbox accepts Variadic optional arguments which libnetwork can use. 73 sbx, err := controller.NewSandbox("container1", 74 libnetwork.OptionHostname("test"), 75 libnetwork.OptionDomainname("docker.io")) 76 if err != nil { 77 log.Fatalf("controller.NewSandbox: %s", err) 78 } 79 80 // A sandbox can join the endpoint via the join api. 81 err = ep.Join(sbx) 82 if err != nil { 83 log.Fatalf("ep.Join: %s", err) 84 } 85 86 // libnetwork client can check the endpoint's operational data via the Info() API 87 epInfo, err := ep.DriverInfo() 88 if err != nil { 89 log.Fatalf("ep.DriverInfo: %s", err) 90 } 91 92 macAddress, ok := epInfo[netlabel.MacAddress] 93 if !ok { 94 log.Fatalf("failed to get mac address from endpoint info") 95 } 96 97 fmt.Printf("Joined endpoint %s (%s) to sandbox %s (%s)\n", ep.Name(), macAddress, sbx.ContainerID(), sbx.Key()) 98 } 99 ``` 100 101 ## Contributing 102 103 Want to hack on libnetwork? [Docker's contributions guidelines](https://github.com/docker/docker/blob/master/CONTRIBUTING.md) apply. 104 105 ## Copyright and license 106 Code and documentation copyright 2015 Docker, inc. Code released under the Apache 2.0 license. Docs released under Creative commons.