2015-04-10 03:33:38 +00:00
# libnetwork - networking for containers
2015-04-14 12:13:34 +00:00
2015-04-10 03:33:38 +00: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 12:07:32 +00:00
#### Design
Please refer to the [design ](docs/design.md ) for more information.
2015-04-10 03:33:38 +00: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 05:57:36 +00:00
2015-04-10 03:33:38 +00:00
```go
2021-06-09 10:31:52 +00:00
package main
2018-07-13 13:12:22 +00:00
import (
"fmt"
"log"
2021-06-09 10:31:52 +00:00
"github.com/docker/docker/libnetwork"
"github.com/docker/docker/libnetwork/config"
"github.com/docker/docker/libnetwork/netlabel"
"github.com/docker/docker/libnetwork/options"
2018-07-13 13:12:22 +00:00
)
2015-12-23 19:47:14 +00:00
func main() {
2015-09-18 21:00:36 +00: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 19:47:14 +00:00
log.Fatalf("libnetwork.New: %s", err)
2015-09-18 21:00:36 +00:00
}
// Create a network for containers to join.
// NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can use.
2016-02-29 19:49:04 +00:00
network, err := controller.NewNetwork(networkType, "network1", "")
2015-09-18 21:00:36 +00:00
if err != nil {
2015-12-23 19:47:14 +00:00
log.Fatalf("controller.NewNetwork: %s", err)
2015-09-18 21:00:36 +00: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 19:47:14 +00:00
log.Fatalf("network.CreateEndpoint: %s", err)
2015-09-18 21:00:36 +00:00
}
2015-09-22 15:27:11 +00:00
// Create the sandbox for the container.
// NewSandbox accepts Variadic optional arguments which libnetwork can use.
2015-09-18 21:00:36 +00:00
sbx, err := controller.NewSandbox("container1",
libnetwork.OptionHostname("test"),
2023-01-10 14:16:51 +00:00
libnetwork.OptionDomainname("example.com"))
2015-12-23 19:47:14 +00:00
if err != nil {
log.Fatalf("controller.NewSandbox: %s", err)
}
2015-09-18 21:00:36 +00:00
// A sandbox can join the endpoint via the join api.
err = ep.Join(sbx)
if err != nil {
2015-12-23 19:47:14 +00:00
log.Fatalf("ep.Join: %s", err)
2015-09-18 21:00:36 +00:00
}
// libnetwork client can check the endpoint's operational data via the Info() API
epInfo, err := ep.DriverInfo()
2015-12-23 19:47:14 +00:00
if err != nil {
log.Fatalf("ep.DriverInfo: %s", err)
2015-09-18 21:00:36 +00:00
}
2015-12-23 19:47:14 +00:00
macAddress, ok := epInfo[netlabel.MacAddress]
if !ok {
2023-05-10 10:01:06 +00:00
log.Fatal("failed to get mac address from endpoint info")
2015-12-23 19:47:14 +00:00
}
fmt.Printf("Joined endpoint %s (%s) to sandbox %s (%s)\n", ep.Name(), macAddress, sbx.ContainerID(), sbx.Key())
}
2015-04-10 03:33:38 +00:00
```
2015-04-01 14:06:54 +00:00
## Contributing
Want to hack on libnetwork? [Docker's contributions guidelines ](https://github.com/docker/docker/blob/master/CONTRIBUTING.md ) apply.
2015-04-10 03:33:38 +00:00
## Copyright and license
Code and documentation copyright 2015 Docker, inc. Code released under the Apache 2.0 license. Docs released under Creative commons.