2015-04-09 23:33:38 -04:00
# libnetwork - networking for containers
2015-04-14 08:13:34 -04:00
2018-03-26 16:32:38 -04:00
[![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)
2015-04-14 08:13:34 -04:00
2015-04-09 23:33:38 -04:00
Libnetwork provides a native Go implementation for connecting containers
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.
2015-05-06 08:07:32 -04:00
#### Design
Please refer to the [design ](docs/design.md ) for more information.
2015-04-09 23:33:38 -04:00
#### Using libnetwork
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.
2015-04-28 01:57:36 -04:00
2015-04-09 23:33:38 -04:00
```go
2018-07-13 09:12:22 -04:00
import (
"fmt"
"log"
"github.com/docker/docker/pkg/reexec"
"github.com/docker/libnetwork"
"github.com/docker/libnetwork/config"
"github.com/docker/libnetwork/netlabel"
"github.com/docker/libnetwork/options"
)
2015-12-23 14:47:14 -05:00
func main() {
if reexec.Init() {
return
}
2015-09-18 17:00:36 -04:00
// Select and configure the network driver
networkType := "bridge"
// Create a new controller instance
driverOptions := options.Generic{}
genericOption := make(map[string]interface{})
genericOption[netlabel.GenericData] = driverOptions
controller, err := libnetwork.New(config.OptionDriverConfig(networkType, genericOption))
if err != nil {
2015-12-23 14:47:14 -05:00
log.Fatalf("libnetwork.New: %s", err)
2015-09-18 17:00:36 -04:00
}
// Create a network for containers to join.
// NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can use.
2016-02-29 14:49:04 -05:00
network, err := controller.NewNetwork(networkType, "network1", "")
2015-09-18 17:00:36 -04:00
if err != nil {
2015-12-23 14:47:14 -05:00
log.Fatalf("controller.NewNetwork: %s", err)
2015-09-18 17:00:36 -04:00
}
// For each new container: allocate IP and interfaces. The returned network
// settings will be used for container infos (inspect and such), as well as
// iptables rules for port publishing. This info is contained or accessible
// from the returned endpoint.
ep, err := network.CreateEndpoint("Endpoint1")
if err != nil {
2015-12-23 14:47:14 -05:00
log.Fatalf("network.CreateEndpoint: %s", err)
2015-09-18 17:00:36 -04:00
}
2015-09-22 11:27:11 -04:00
// Create the sandbox for the container.
// NewSandbox accepts Variadic optional arguments which libnetwork can use.
2015-09-18 17:00:36 -04:00
sbx, err := controller.NewSandbox("container1",
libnetwork.OptionHostname("test"),
libnetwork.OptionDomainname("docker.io"))
2015-12-23 14:47:14 -05:00
if err != nil {
log.Fatalf("controller.NewSandbox: %s", err)
}
2015-09-18 17:00:36 -04:00
// A sandbox can join the endpoint via the join api.
err = ep.Join(sbx)
if err != nil {
2015-12-23 14:47:14 -05:00
log.Fatalf("ep.Join: %s", err)
2015-09-18 17:00:36 -04:00
}
// libnetwork client can check the endpoint's operational data via the Info() API
epInfo, err := ep.DriverInfo()
2015-12-23 14:47:14 -05:00
if err != nil {
log.Fatalf("ep.DriverInfo: %s", err)
2015-09-18 17:00:36 -04:00
}
2015-12-23 14:47:14 -05:00
macAddress, ok := epInfo[netlabel.MacAddress]
if !ok {
log.Fatalf("failed to get mac address from endpoint info")
}
fmt.Printf("Joined endpoint %s (%s) to sandbox %s (%s)\n", ep.Name(), macAddress, sbx.ContainerID(), sbx.Key())
}
2015-04-09 23:33:38 -04:00
```
2015-04-01 10:06:54 -04:00
## Contributing
Want to hack on libnetwork? [Docker's contributions guidelines ](https://github.com/docker/docker/blob/master/CONTRIBUTING.md ) apply.
2015-04-09 23:33:38 -04:00
## Copyright and license
Code and documentation copyright 2015 Docker, inc. Code released under the Apache 2.0 license. Docs released under Creative commons.