readme.go 1.5 KB

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