1# libnetwork - networking for containers 2 3[![Circle CI](https://circleci.com/gh/docker/libnetwork/tree/master.svg?style=svg)](https://circleci.com/gh/docker/libnetwork/tree/master) [![Coverage Status](https://coveralls.io/repos/docker/libnetwork/badge.svg)](https://coveralls.io/r/docker/libnetwork) [![GoDoc](https://godoc.org/github.com/docker/libnetwork?status.svg)](https://godoc.org/github.com/docker/libnetwork) [![Go Report Card](https://goreportcard.com/badge/github.com/docker/libnetwork)](https://goreportcard.com/report/github.com/docker/libnetwork) 4 5Libnetwork provides a native Go implementation for connecting containers 6 7The goal of libnetwork is to deliver a robust Container Network Model that provides a consistent programming interface and the required network abstractions for applications. 8 9#### Design 10Please refer to the [design](docs/design.md) for more information. 11 12#### Using libnetwork 13 14There 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. 15 16 17```go 18import ( 19 "fmt" 20 "log" 21 22 "github.com/docker/docker/pkg/reexec" 23 "github.com/docker/libnetwork" 24 "github.com/docker/libnetwork/config" 25 "github.com/docker/libnetwork/netlabel" 26 "github.com/docker/libnetwork/options" 27) 28 29func 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## Future 93Please refer to [roadmap](ROADMAP.md) for more information. 94 95## Contributing 96 97Want to hack on libnetwork? [Docker's contributions guidelines](https://github.com/docker/docker/blob/master/CONTRIBUTING.md) apply. 98 99## Copyright and license 100Code and documentation copyright 2015 Docker, inc. Code released under the Apache 2.0 license. Docs released under Creative commons. 101