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