github.com/kunnos/engine@v1.13.1/cli/command/network/connect.go (about)

     1  package network
     2  
     3  import (
     4  	"golang.org/x/net/context"
     5  
     6  	"github.com/docker/docker/api/types/network"
     7  	"github.com/docker/docker/cli"
     8  	"github.com/docker/docker/cli/command"
     9  	"github.com/docker/docker/opts"
    10  	runconfigopts "github.com/docker/docker/runconfig/opts"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  type connectOptions struct {
    15  	network      string
    16  	container    string
    17  	ipaddress    string
    18  	ipv6address  string
    19  	links        opts.ListOpts
    20  	aliases      []string
    21  	linklocalips []string
    22  }
    23  
    24  func newConnectCommand(dockerCli *command.DockerCli) *cobra.Command {
    25  	opts := connectOptions{
    26  		links: opts.NewListOpts(runconfigopts.ValidateLink),
    27  	}
    28  
    29  	cmd := &cobra.Command{
    30  		Use:   "connect [OPTIONS] NETWORK CONTAINER",
    31  		Short: "Connect a container to a network",
    32  		Args:  cli.ExactArgs(2),
    33  		RunE: func(cmd *cobra.Command, args []string) error {
    34  			opts.network = args[0]
    35  			opts.container = args[1]
    36  			return runConnect(dockerCli, opts)
    37  		},
    38  	}
    39  
    40  	flags := cmd.Flags()
    41  	flags.StringVar(&opts.ipaddress, "ip", "", "IP Address")
    42  	flags.StringVar(&opts.ipv6address, "ip6", "", "IPv6 Address")
    43  	flags.Var(&opts.links, "link", "Add link to another container")
    44  	flags.StringSliceVar(&opts.aliases, "alias", []string{}, "Add network-scoped alias for the container")
    45  	flags.StringSliceVar(&opts.linklocalips, "link-local-ip", []string{}, "Add a link-local address for the container")
    46  
    47  	return cmd
    48  }
    49  
    50  func runConnect(dockerCli *command.DockerCli, opts connectOptions) error {
    51  	client := dockerCli.Client()
    52  
    53  	epConfig := &network.EndpointSettings{
    54  		IPAMConfig: &network.EndpointIPAMConfig{
    55  			IPv4Address:  opts.ipaddress,
    56  			IPv6Address:  opts.ipv6address,
    57  			LinkLocalIPs: opts.linklocalips,
    58  		},
    59  		Links:   opts.links.GetAll(),
    60  		Aliases: opts.aliases,
    61  	}
    62  
    63  	return client.NetworkConnect(context.Background(), opts.network, opts.container, epConfig)
    64  }