readme.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/docker/libnetwork"
  5. "github.com/docker/libnetwork/netutils"
  6. "github.com/docker/libnetwork/pkg/options"
  7. )
  8. func main() {
  9. // Create a new controller instance
  10. controller := libnetwork.New()
  11. // Select and configure the network driver
  12. networkType := "bridge"
  13. driverOptions := options.Generic{}
  14. genericOption := make(map[string]interface{})
  15. genericOption[options.GenericData] = driverOptions
  16. err := controller.ConfigureNetworkDriver(networkType, genericOption)
  17. if err != nil {
  18. return
  19. }
  20. // Create a network for containers to join.
  21. // NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can make of
  22. network, err := controller.NewNetwork(networkType, "network1")
  23. if err != nil {
  24. return
  25. }
  26. // For each new container: allocate IP and interfaces. The returned network
  27. // settings will be used for container infos (inspect and such), as well as
  28. // iptables rules for port publishing. This info is contained or accessible
  29. // from the returned endpoint.
  30. ep, err := network.CreateEndpoint("Endpoint1")
  31. if err != nil {
  32. return
  33. }
  34. // A container can join the endpoint by providing the container ID to the join
  35. // api which returns the sandbox key which can be used to access the sandbox
  36. // created for the container during join.
  37. // Join acceps Variadic arguments which will be made use of by libnetwork and Drivers
  38. _, err = ep.Join("container1",
  39. libnetwork.JoinOptionHostname("test"),
  40. libnetwork.JoinOptionDomainname("docker.io"))
  41. if err != nil {
  42. return
  43. }
  44. // libentwork client can check the endpoint's operational data via the Info() API
  45. epInfo, err := ep.Info()
  46. mapData, ok := epInfo[options.PortMap]
  47. if ok {
  48. portMapping, ok := mapData.([]netutils.PortBinding)
  49. if ok {
  50. fmt.Printf("Current port mapping for endpoint %s: %v", ep.Name(), portMapping)
  51. }
  52. }
  53. }