2016-09-08 13:11:39 -04:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types/network"
|
|
|
|
"github.com/spf13/cobra"
|
2017-05-15 08:45:19 -04:00
|
|
|
"golang.org/x/net/context"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type connectOptions struct {
|
|
|
|
network string
|
|
|
|
container string
|
|
|
|
ipaddress string
|
|
|
|
ipv6address string
|
|
|
|
links opts.ListOpts
|
|
|
|
aliases []string
|
|
|
|
linklocalips []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConnectCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := connectOptions{
|
2016-12-23 14:09:12 -05:00
|
|
|
links: opts.NewListOpts(opts.ValidateLink),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "connect [OPTIONS] NETWORK CONTAINER",
|
|
|
|
Short: "Connect a container to a network",
|
|
|
|
Args: cli.ExactArgs(2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-05-15 08:45:19 -04:00
|
|
|
options.network = args[0]
|
|
|
|
options.container = args[1]
|
|
|
|
return runConnect(dockerCli, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.StringVar(&options.ipaddress, "ip", "", "IPv4 address (e.g., 172.30.100.104)")
|
|
|
|
flags.StringVar(&options.ipv6address, "ip6", "", "IPv6 address (e.g., 2001:db8::33)")
|
|
|
|
flags.Var(&options.links, "link", "Add link to another container")
|
|
|
|
flags.StringSliceVar(&options.aliases, "alias", []string{}, "Add network-scoped alias for the container")
|
|
|
|
flags.StringSliceVar(&options.linklocalips, "link-local-ip", []string{}, "Add a link-local address for the container")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
func runConnect(dockerCli *command.DockerCli, options connectOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
|
|
|
|
epConfig := &network.EndpointSettings{
|
|
|
|
IPAMConfig: &network.EndpointIPAMConfig{
|
2017-05-15 08:45:19 -04:00
|
|
|
IPv4Address: options.ipaddress,
|
|
|
|
IPv6Address: options.ipv6address,
|
|
|
|
LinkLocalIPs: options.linklocalips,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2017-05-15 08:45:19 -04:00
|
|
|
Links: options.links.GetAll(),
|
|
|
|
Aliases: options.aliases,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
return client.NetworkConnect(context.Background(), options.network, options.container, epConfig)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|