moby / libnetwork

networking for containers
Apache License 2.0
2.16k stars 881 forks source link

Warning libnetwork was moved to https://github.com/moby/moby/tree/master/libnetwork

libnetwork has been merged to the main repo of Moby since Docker 22.06.

The old libnetwork repo (https://github.com/moby/libnetwork) now only accepts PR for Docker 20.10, and will be archived after the EOL of Docker 20.10.


libnetwork - networking for containers

Circle CI Coverage Status GoDoc Go Report Card

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.

Design

Please refer to the design for more information.

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.

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"
)

func main() {
    if reexec.Init() {
        return
    }

    // 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 {
        log.Fatalf("libnetwork.New: %s", err)
    }

    // Create a network for containers to join.
    // NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can use.
    network, err := controller.NewNetwork(networkType, "network1", "")
    if err != nil {
        log.Fatalf("controller.NewNetwork: %s", err)
    }

    // 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 {
        log.Fatalf("network.CreateEndpoint: %s", err)
    }

    // Create the sandbox for the container.
    // NewSandbox accepts Variadic optional arguments which libnetwork can use.
    sbx, err := controller.NewSandbox("container1",
        libnetwork.OptionHostname("test"),
        libnetwork.OptionDomainname("docker.io"))
    if err != nil {
        log.Fatalf("controller.NewSandbox: %s", err)
    }

    // A sandbox can join the endpoint via the join api.
    err = ep.Join(sbx)
    if err != nil {
        log.Fatalf("ep.Join: %s", err)
    }

    // libnetwork client can check the endpoint's operational data via the Info() API
    epInfo, err := ep.DriverInfo()
    if err != nil {
        log.Fatalf("ep.DriverInfo: %s", err)
    }

    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())
}

Contributing

Want to hack on libnetwork? Docker's contributions guidelines apply.

Copyright and license

Code and documentation copyright 2015 Docker, inc. Code released under the Apache 2.0 license. Docs released under Creative commons.