github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/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 "github.com/spf13/cobra" 11 ) 12 13 type connectOptions struct { 14 network string 15 container string 16 ipaddress string 17 ipv6address string 18 links opts.ListOpts 19 aliases []string 20 linklocalips []string 21 } 22 23 func newConnectCommand(dockerCli *command.DockerCli) *cobra.Command { 24 opts := connectOptions{ 25 links: opts.NewListOpts(opts.ValidateLink), 26 } 27 28 cmd := &cobra.Command{ 29 Use: "connect [OPTIONS] NETWORK CONTAINER", 30 Short: "Connect a container to a network", 31 Args: cli.ExactArgs(2), 32 RunE: func(cmd *cobra.Command, args []string) error { 33 opts.network = args[0] 34 opts.container = args[1] 35 return runConnect(dockerCli, opts) 36 }, 37 } 38 39 flags := cmd.Flags() 40 flags.StringVar(&opts.ipaddress, "ip", "", "IPv4 address (e.g., 172.30.100.104)") 41 flags.StringVar(&opts.ipv6address, "ip6", "", "IPv6 address (e.g., 2001:db8::33)") 42 flags.Var(&opts.links, "link", "Add link to another container") 43 flags.StringSliceVar(&opts.aliases, "alias", []string{}, "Add network-scoped alias for the container") 44 flags.StringSliceVar(&opts.linklocalips, "link-local-ip", []string{}, "Add a link-local address for the container") 45 46 return cmd 47 } 48 49 func runConnect(dockerCli *command.DockerCli, opts connectOptions) error { 50 client := dockerCli.Client() 51 52 epConfig := &network.EndpointSettings{ 53 IPAMConfig: &network.EndpointIPAMConfig{ 54 IPv4Address: opts.ipaddress, 55 IPv6Address: opts.ipv6address, 56 LinkLocalIPs: opts.linklocalips, 57 }, 58 Links: opts.links.GetAll(), 59 Aliases: opts.aliases, 60 } 61 62 return client.NetworkConnect(context.Background(), opts.network, opts.container, epConfig) 63 }