فهرست منبع

Update --net flags and container mode
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby 11 سال پیش
والد
کامیت
2c2cc051d8
4فایلهای تغییر یافته به همراه54 افزوده شده و 53 حذف شده
  1. 20 15
      daemon/container.go
  2. 12 14
      runconfig/hostconfig.go
  3. 21 23
      runconfig/parse.go
  4. 1 1
      runconfig/parse_test.go

+ 20 - 15
daemon/container.go

@@ -338,27 +338,32 @@ func populateCommand(c *Container, env []string) error {
 		Interface: nil,
 		Interface: nil,
 	}
 	}
 
 
-	if !c.Config.NetworkDisabled {
-		network := c.NetworkSettings
-		en.Interface = &execdriver.NetworkInterface{
-			Gateway:     network.Gateway,
-			Bridge:      network.Bridge,
-			IPAddress:   network.IPAddress,
-			IPPrefixLen: network.IPPrefixLen,
+	parts := strings.SplitN(c.hostConfig.NetworkMode, ":", 2)
+	switch parts[0] {
+	case "none":
+	case "bridge":
+		if !c.Config.NetworkDisabled {
+			network := c.NetworkSettings
+			en.Interface = &execdriver.NetworkInterface{
+				Gateway:     network.Gateway,
+				Bridge:      network.Bridge,
+				IPAddress:   network.IPAddress,
+				IPPrefixLen: network.IPPrefixLen,
+			}
 		}
 		}
-	}
-
-	// TODO: this can be removed after lxc-conf is fully deprecated
-	mergeLxcConfIntoOptions(c.hostConfig, context)
-
-	if netContainer := c.hostConfig.UseContainerNetwork; netContainer != "" {
-		nc := c.daemon.Get(netContainer)
+	case "container":
+		nc := c.daemon.Get(parts[1])
 		if nc == nil {
 		if nc == nil {
-			return fmt.Errorf("no such container to join network: %q", netContainer)
+			return fmt.Errorf("no such container to join network: %q", parts[1])
 		}
 		}
 		en.ContainerID = nc.ID
 		en.ContainerID = nc.ID
+	default:
+		return fmt.Errorf("invalid network mode: %s", c.hostConfig.NetworkMode)
 	}
 	}
 
 
+	// TODO: this can be removed after lxc-conf is fully deprecated
+	mergeLxcConfIntoOptions(c.hostConfig, context)
+
 	resources := &execdriver.Resources{
 	resources := &execdriver.Resources{
 		Memory:     c.Config.Memory,
 		Memory:     c.Config.Memory,
 		MemorySwap: c.Config.MemorySwap,
 		MemorySwap: c.Config.MemorySwap,

+ 12 - 14
runconfig/hostconfig.go

@@ -7,17 +7,17 @@ import (
 )
 )
 
 
 type HostConfig struct {
 type HostConfig struct {
-	Binds               []string
-	ContainerIDFile     string
-	LxcConf             []utils.KeyValuePair
-	Privileged          bool
-	PortBindings        nat.PortMap
-	Links               []string
-	PublishAllPorts     bool
-	Dns                 []string
-	DnsSearch           []string
-	VolumesFrom         []string
-	UseContainerNetwork string
+	Binds           []string
+	ContainerIDFile string
+	LxcConf         []utils.KeyValuePair
+	Privileged      bool
+	PortBindings    nat.PortMap
+	Links           []string
+	PublishAllPorts bool
+	Dns             []string
+	DnsSearch       []string
+	VolumesFrom     []string
+	NetworkMode     string
 }
 }
 
 
 func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
 func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
@@ -25,6 +25,7 @@ func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
 		ContainerIDFile: job.Getenv("ContainerIDFile"),
 		ContainerIDFile: job.Getenv("ContainerIDFile"),
 		Privileged:      job.GetenvBool("Privileged"),
 		Privileged:      job.GetenvBool("Privileged"),
 		PublishAllPorts: job.GetenvBool("PublishAllPorts"),
 		PublishAllPorts: job.GetenvBool("PublishAllPorts"),
+		NetworkMode:     job.Getenv("NetworkMode"),
 	}
 	}
 	job.GetenvJson("LxcConf", &hostConfig.LxcConf)
 	job.GetenvJson("LxcConf", &hostConfig.LxcConf)
 	job.GetenvJson("PortBindings", &hostConfig.PortBindings)
 	job.GetenvJson("PortBindings", &hostConfig.PortBindings)
@@ -43,8 +44,5 @@ func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
 	if VolumesFrom := job.GetenvList("VolumesFrom"); VolumesFrom != nil {
 	if VolumesFrom := job.GetenvList("VolumesFrom"); VolumesFrom != nil {
 		hostConfig.VolumesFrom = VolumesFrom
 		hostConfig.VolumesFrom = VolumesFrom
 	}
 	}
-	if UseContainerNetwork := job.Getenv("UseContainerNetwork"); UseContainerNetwork != "" {
-		hostConfig.UseContainerNetwork = UseContainerNetwork
-	}
 	return hostConfig
 	return hostConfig
 }
 }

+ 21 - 23
runconfig/parse.go

@@ -50,7 +50,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
 
 
 		flAutoRemove      = cmd.Bool([]string{"#rm", "-rm"}, false, "Automatically remove the container when it exits (incompatible with -d)")
 		flAutoRemove      = cmd.Bool([]string{"#rm", "-rm"}, false, "Automatically remove the container when it exits (incompatible with -d)")
 		flDetach          = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: Run container in the background, print new container id")
 		flDetach          = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: Run container in the background, print new container id")
-		flNetwork         = cmd.Bool([]string{"n", "-networking"}, true, "Enable networking for this container")
+		flNetwork         = cmd.Bool([]string{"#n", "#-networking"}, true, "Enable networking for this container")
 		flPrivileged      = cmd.Bool([]string{"#privileged", "-privileged"}, false, "Give extended privileges to this container")
 		flPrivileged      = cmd.Bool([]string{"#privileged", "-privileged"}, false, "Give extended privileges to this container")
 		flPublishAll      = cmd.Bool([]string{"P", "-publish-all"}, false, "Publish all exposed ports to the host interfaces")
 		flPublishAll      = cmd.Bool([]string{"P", "-publish-all"}, false, "Publish all exposed ports to the host interfaces")
 		flStdin           = cmd.Bool([]string{"i", "-interactive"}, false, "Keep stdin open even if not attached")
 		flStdin           = cmd.Bool([]string{"i", "-interactive"}, false, "Keep stdin open even if not attached")
@@ -62,7 +62,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
 		flUser            = cmd.String([]string{"u", "-user"}, "", "Username or UID")
 		flUser            = cmd.String([]string{"u", "-user"}, "", "Username or UID")
 		flWorkingDir      = cmd.String([]string{"w", "-workdir"}, "", "Working directory inside the container")
 		flWorkingDir      = cmd.String([]string{"w", "-workdir"}, "", "Working directory inside the container")
 		flCpuShares       = cmd.Int64([]string{"c", "-cpu-shares"}, 0, "CPU shares (relative weight)")
 		flCpuShares       = cmd.Int64([]string{"c", "-cpu-shares"}, 0, "CPU shares (relative weight)")
-		flNetMode         = cmd.String([]string{"#net", "-net"}, "bridge", "Set the Network mode for the container ('bridge': creates a new network stack for the container on the docker bridge, 'disable': disable networking for this container, 'container:name_or_id': reuses another container network stack)")
+		flNetMode         = cmd.String([]string{"-net"}, "bridge", "Set the Network mode for the container ('bridge': creates a new network stack for the container on the docker bridge, 'none': no networking for this container, 'container:name_or_id': reuses another container network stack)")
 		// For documentation purpose
 		// For documentation purpose
 		_ = cmd.Bool([]string{"#sig-proxy", "-sig-proxy"}, true, "Proxify all received signal to the process (even in non-tty mode)")
 		_ = cmd.Bool([]string{"#sig-proxy", "-sig-proxy"}, true, "Proxify all received signal to the process (even in non-tty mode)")
 		_ = cmd.String([]string{"#name", "-name"}, "", "Assign a name to the container")
 		_ = cmd.String([]string{"#name", "-name"}, "", "Assign a name to the container")
@@ -198,7 +198,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
 	// boo, there's no debug output for docker run
 	// boo, there's no debug output for docker run
 	//utils.Debugf("Environment variables for the container: %#v", envVariables)
 	//utils.Debugf("Environment variables for the container: %#v", envVariables)
 
 
-	netMode, useContainerNetwork, err := parseNetMode(*flNetMode)
+	netMode, err := parseNetMode(*flNetMode)
 	if err != nil {
 	if err != nil {
 		return nil, nil, cmd, fmt.Errorf("-net: invalid net mode: %v", err)
 		return nil, nil, cmd, fmt.Errorf("-net: invalid net mode: %v", err)
 	}
 	}
@@ -210,7 +210,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
 		ExposedPorts:    ports,
 		ExposedPorts:    ports,
 		User:            *flUser,
 		User:            *flUser,
 		Tty:             *flTty,
 		Tty:             *flTty,
-		NetworkDisabled: !*flNetwork || netMode == "disable",
+		NetworkDisabled: !*flNetwork,
 		OpenStdin:       *flStdin,
 		OpenStdin:       *flStdin,
 		Memory:          flMemory,
 		Memory:          flMemory,
 		CpuShares:       *flCpuShares,
 		CpuShares:       *flCpuShares,
@@ -226,17 +226,17 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
 	}
 	}
 
 
 	hostConfig := &HostConfig{
 	hostConfig := &HostConfig{
-		Binds:               binds,
-		ContainerIDFile:     *flContainerIDFile,
-		LxcConf:             lxcConf,
-		Privileged:          *flPrivileged,
-		PortBindings:        portBindings,
-		Links:               flLinks.GetAll(),
-		PublishAllPorts:     *flPublishAll,
-		Dns:                 flDns.GetAll(),
-		DnsSearch:           flDnsSearch.GetAll(),
-		VolumesFrom:         flVolumesFrom.GetAll(),
-		UseContainerNetwork: useContainerNetwork,
+		Binds:           binds,
+		ContainerIDFile: *flContainerIDFile,
+		LxcConf:         lxcConf,
+		Privileged:      *flPrivileged,
+		PortBindings:    portBindings,
+		Links:           flLinks.GetAll(),
+		PublishAllPorts: *flPublishAll,
+		Dns:             flDns.GetAll(),
+		DnsSearch:       flDnsSearch.GetAll(),
+		VolumesFrom:     flVolumesFrom.GetAll(),
+		NetworkMode:     netMode,
 	}
 	}
 
 
 	if sysInfo != nil && flMemory > 0 && !sysInfo.SwapLimit {
 	if sysInfo != nil && flMemory > 0 && !sysInfo.SwapLimit {
@@ -282,19 +282,17 @@ func parseKeyValueOpts(opts opts.ListOpts) ([]utils.KeyValuePair, error) {
 	return out, nil
 	return out, nil
 }
 }
 
 
-func parseNetMode(netMode string) (string, string, error) {
+func parseNetMode(netMode string) (string, error) {
 	parts := strings.Split(netMode, ":")
 	parts := strings.Split(netMode, ":")
 	switch mode := parts[0]; mode {
 	switch mode := parts[0]; mode {
-	case "bridge", "disable":
-		return mode, "", nil
+	case "bridge", "none":
+		return mode, nil
 	case "container":
 	case "container":
-		var container string
 		if len(parts) < 2 || parts[1] == "" {
 		if len(parts) < 2 || parts[1] == "" {
-			return "", "", fmt.Errorf("'container:' netmode requires a container id or name", netMode)
+			return "", fmt.Errorf("'container:' netmode requires a container id or name", netMode)
 		}
 		}
-		container = parts[1]
-		return mode, container, nil
+		return netMode, nil
 	default:
 	default:
-		return "", "", fmt.Errorf("invalid netmode: %q", netMode)
+		return "", fmt.Errorf("invalid netmode: %q", netMode)
 	}
 	}
 }
 }

+ 1 - 1
runconfig/parse_test.go

@@ -40,7 +40,7 @@ func TestParseNetMode(t *testing.T) {
 	}
 	}
 
 
 	for _, to := range testFlags {
 	for _, to := range testFlags {
-		mode, container, err := parseNetMode(to.flag)
+		mode, err := parseNetMode(to.flag)
 		if mode != to.mode {
 		if mode != to.mode {
 			t.Fatalf("-net %s: expected net mode: %q, got: %q", to.flag, to.mode, mode)
 			t.Fatalf("-net %s: expected net mode: %q, got: %q", to.flag, to.mode, mode)
 		}
 		}