2015-04-16 04:58:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-05-04 18:49:53 +00:00
|
|
|
"fmt"
|
2015-12-23 19:47:14 +00:00
|
|
|
"log"
|
2015-05-04 18:49:53 +00:00
|
|
|
|
2021-04-06 00:24:47 +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"
|
2021-05-28 00:15:56 +00:00
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2015-04-16 04:58:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2015-12-23 19:47:14 +00:00
|
|
|
if reexec.Init() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-22 23:47:07 +00:00
|
|
|
// Select and configure the network driver
|
|
|
|
networkType := "bridge"
|
2015-05-01 00:57:06 +00:00
|
|
|
|
2015-09-18 21:00:36 +00:00
|
|
|
// Create a new controller instance
|
2015-05-01 00:57:06 +00:00
|
|
|
driverOptions := options.Generic{}
|
|
|
|
genericOption := make(map[string]interface{})
|
2015-05-06 04:19:57 +00:00
|
|
|
genericOption[netlabel.GenericData] = driverOptions
|
2015-09-18 21:00:36 +00:00
|
|
|
controller, err := libnetwork.New(config.OptionDriverConfig(networkType, genericOption))
|
2015-04-16 04:58:15 +00:00
|
|
|
if err != nil {
|
2015-12-23 19:47:14 +00:00
|
|
|
log.Fatalf("libnetwork.New: %s", err)
|
2015-04-16 04:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a network for containers to join.
|
2015-07-02 05:00:48 +00:00
|
|
|
// 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-04-16 04:58:15 +00:00
|
|
|
if err != nil {
|
2015-12-23 19:47:14 +00:00
|
|
|
log.Fatalf("controller.NewNetwork: %s", err)
|
2015-04-16 04:58:15 +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
|
2015-04-17 22:42:23 +00:00
|
|
|
// iptables rules for port publishing. This info is contained or accessible
|
|
|
|
// from the returned endpoint.
|
2015-05-01 00:57:06 +00:00
|
|
|
ep, err := network.CreateEndpoint("Endpoint1")
|
2015-04-16 04:58:15 +00:00
|
|
|
if err != nil {
|
2015-12-23 19:47:14 +00:00
|
|
|
log.Fatalf("network.CreateEndpoint: %s", err)
|
2015-04-16 04:58:15 +00:00
|
|
|
}
|
|
|
|
|
2015-12-23 19:47:14 +00:00
|
|
|
// Create the sandbox for the container.
|
|
|
|
// NewSandbox accepts Variadic optional arguments which libnetwork can use.
|
2015-07-02 05:00:48 +00:00
|
|
|
sbx, err := controller.NewSandbox("container1",
|
|
|
|
libnetwork.OptionHostname("test"),
|
|
|
|
libnetwork.OptionDomainname("docker.io"))
|
2015-12-23 19:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("controller.NewSandbox: %s", err)
|
|
|
|
}
|
2015-07-02 05:00:48 +00:00
|
|
|
|
|
|
|
// A sandbox can join the endpoint via the join api.
|
|
|
|
err = ep.Join(sbx)
|
2015-04-28 05:57:36 +00:00
|
|
|
if err != nil {
|
2015-12-23 19:47:14 +00:00
|
|
|
log.Fatalf("ep.Join: %s", err)
|
2015-04-16 04:58:15 +00:00
|
|
|
}
|
2015-05-04 18:49:53 +00:00
|
|
|
|
2015-06-26 10:01:23 +00:00
|
|
|
// libnetwork client can check the endpoint's operational data via the Info() API
|
2015-05-14 06:23:45 +00:00
|
|
|
epInfo, err := ep.DriverInfo()
|
2015-12-23 19:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("ep.DriverInfo: %s", err)
|
2015-05-04 18:49:53 +00:00
|
|
|
}
|
2015-12-23 19:47:14 +00:00
|
|
|
|
|
|
|
macAddress, ok := epInfo[netlabel.MacAddress]
|
|
|
|
if !ok {
|
2016-11-22 01:29:53 +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-16 04:58:15 +00:00
|
|
|
}
|