Browse code

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

Michael Crosby authored on 2014/05/03 06:06:05
Showing 4 changed files
... ...
@@ -338,27 +338,32 @@ func populateCommand(c *Container, env []string) error {
338 338
 		Interface: nil,
339 339
 	}
340 340
 
341
-	if !c.Config.NetworkDisabled {
342
-		network := c.NetworkSettings
343
-		en.Interface = &execdriver.NetworkInterface{
344
-			Gateway:     network.Gateway,
345
-			Bridge:      network.Bridge,
346
-			IPAddress:   network.IPAddress,
347
-			IPPrefixLen: network.IPPrefixLen,
341
+	parts := strings.SplitN(c.hostConfig.NetworkMode, ":", 2)
342
+	switch parts[0] {
343
+	case "none":
344
+	case "bridge":
345
+		if !c.Config.NetworkDisabled {
346
+			network := c.NetworkSettings
347
+			en.Interface = &execdriver.NetworkInterface{
348
+				Gateway:     network.Gateway,
349
+				Bridge:      network.Bridge,
350
+				IPAddress:   network.IPAddress,
351
+				IPPrefixLen: network.IPPrefixLen,
352
+			}
348 353
 		}
349
-	}
350
-
351
-	// TODO: this can be removed after lxc-conf is fully deprecated
352
-	mergeLxcConfIntoOptions(c.hostConfig, context)
353
-
354
-	if netContainer := c.hostConfig.UseContainerNetwork; netContainer != "" {
355
-		nc := c.daemon.Get(netContainer)
354
+	case "container":
355
+		nc := c.daemon.Get(parts[1])
356 356
 		if nc == nil {
357
-			return fmt.Errorf("no such container to join network: %q", netContainer)
357
+			return fmt.Errorf("no such container to join network: %q", parts[1])
358 358
 		}
359 359
 		en.ContainerID = nc.ID
360
+	default:
361
+		return fmt.Errorf("invalid network mode: %s", c.hostConfig.NetworkMode)
360 362
 	}
361 363
 
364
+	// TODO: this can be removed after lxc-conf is fully deprecated
365
+	mergeLxcConfIntoOptions(c.hostConfig, context)
366
+
362 367
 	resources := &execdriver.Resources{
363 368
 		Memory:     c.Config.Memory,
364 369
 		MemorySwap: c.Config.MemorySwap,
... ...
@@ -7,17 +7,17 @@ import (
7 7
 )
8 8
 
9 9
 type HostConfig struct {
10
-	Binds               []string
11
-	ContainerIDFile     string
12
-	LxcConf             []utils.KeyValuePair
13
-	Privileged          bool
14
-	PortBindings        nat.PortMap
15
-	Links               []string
16
-	PublishAllPorts     bool
17
-	Dns                 []string
18
-	DnsSearch           []string
19
-	VolumesFrom         []string
20
-	UseContainerNetwork string
10
+	Binds           []string
11
+	ContainerIDFile string
12
+	LxcConf         []utils.KeyValuePair
13
+	Privileged      bool
14
+	PortBindings    nat.PortMap
15
+	Links           []string
16
+	PublishAllPorts bool
17
+	Dns             []string
18
+	DnsSearch       []string
19
+	VolumesFrom     []string
20
+	NetworkMode     string
21 21
 }
22 22
 
23 23
 func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
... ...
@@ -25,6 +25,7 @@ func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
25 25
 		ContainerIDFile: job.Getenv("ContainerIDFile"),
26 26
 		Privileged:      job.GetenvBool("Privileged"),
27 27
 		PublishAllPorts: job.GetenvBool("PublishAllPorts"),
28
+		NetworkMode:     job.Getenv("NetworkMode"),
28 29
 	}
29 30
 	job.GetenvJson("LxcConf", &hostConfig.LxcConf)
30 31
 	job.GetenvJson("PortBindings", &hostConfig.PortBindings)
... ...
@@ -43,8 +44,5 @@ func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
43 43
 	if VolumesFrom := job.GetenvList("VolumesFrom"); VolumesFrom != nil {
44 44
 		hostConfig.VolumesFrom = VolumesFrom
45 45
 	}
46
-	if UseContainerNetwork := job.Getenv("UseContainerNetwork"); UseContainerNetwork != "" {
47
-		hostConfig.UseContainerNetwork = UseContainerNetwork
48
-	}
49 46
 	return hostConfig
50 47
 }
... ...
@@ -50,7 +50,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
50 50
 
51 51
 		flAutoRemove      = cmd.Bool([]string{"#rm", "-rm"}, false, "Automatically remove the container when it exits (incompatible with -d)")
52 52
 		flDetach          = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: Run container in the background, print new container id")
53
-		flNetwork         = cmd.Bool([]string{"n", "-networking"}, true, "Enable networking for this container")
53
+		flNetwork         = cmd.Bool([]string{"#n", "#-networking"}, true, "Enable networking for this container")
54 54
 		flPrivileged      = cmd.Bool([]string{"#privileged", "-privileged"}, false, "Give extended privileges to this container")
55 55
 		flPublishAll      = cmd.Bool([]string{"P", "-publish-all"}, false, "Publish all exposed ports to the host interfaces")
56 56
 		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
62 62
 		flUser            = cmd.String([]string{"u", "-user"}, "", "Username or UID")
63 63
 		flWorkingDir      = cmd.String([]string{"w", "-workdir"}, "", "Working directory inside the container")
64 64
 		flCpuShares       = cmd.Int64([]string{"c", "-cpu-shares"}, 0, "CPU shares (relative weight)")
65
-		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)")
65
+		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)")
66 66
 		// For documentation purpose
67 67
 		_ = cmd.Bool([]string{"#sig-proxy", "-sig-proxy"}, true, "Proxify all received signal to the process (even in non-tty mode)")
68 68
 		_ = 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
198 198
 	// boo, there's no debug output for docker run
199 199
 	//utils.Debugf("Environment variables for the container: %#v", envVariables)
200 200
 
201
-	netMode, useContainerNetwork, err := parseNetMode(*flNetMode)
201
+	netMode, err := parseNetMode(*flNetMode)
202 202
 	if err != nil {
203 203
 		return nil, nil, cmd, fmt.Errorf("-net: invalid net mode: %v", err)
204 204
 	}
... ...
@@ -210,7 +210,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
210 210
 		ExposedPorts:    ports,
211 211
 		User:            *flUser,
212 212
 		Tty:             *flTty,
213
-		NetworkDisabled: !*flNetwork || netMode == "disable",
213
+		NetworkDisabled: !*flNetwork,
214 214
 		OpenStdin:       *flStdin,
215 215
 		Memory:          flMemory,
216 216
 		CpuShares:       *flCpuShares,
... ...
@@ -226,17 +226,17 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
226 226
 	}
227 227
 
228 228
 	hostConfig := &HostConfig{
229
-		Binds:               binds,
230
-		ContainerIDFile:     *flContainerIDFile,
231
-		LxcConf:             lxcConf,
232
-		Privileged:          *flPrivileged,
233
-		PortBindings:        portBindings,
234
-		Links:               flLinks.GetAll(),
235
-		PublishAllPorts:     *flPublishAll,
236
-		Dns:                 flDns.GetAll(),
237
-		DnsSearch:           flDnsSearch.GetAll(),
238
-		VolumesFrom:         flVolumesFrom.GetAll(),
239
-		UseContainerNetwork: useContainerNetwork,
229
+		Binds:           binds,
230
+		ContainerIDFile: *flContainerIDFile,
231
+		LxcConf:         lxcConf,
232
+		Privileged:      *flPrivileged,
233
+		PortBindings:    portBindings,
234
+		Links:           flLinks.GetAll(),
235
+		PublishAllPorts: *flPublishAll,
236
+		Dns:             flDns.GetAll(),
237
+		DnsSearch:       flDnsSearch.GetAll(),
238
+		VolumesFrom:     flVolumesFrom.GetAll(),
239
+		NetworkMode:     netMode,
240 240
 	}
241 241
 
242 242
 	if sysInfo != nil && flMemory > 0 && !sysInfo.SwapLimit {
... ...
@@ -282,19 +282,17 @@ func parseKeyValueOpts(opts opts.ListOpts) ([]utils.KeyValuePair, error) {
282 282
 	return out, nil
283 283
 }
284 284
 
285
-func parseNetMode(netMode string) (string, string, error) {
285
+func parseNetMode(netMode string) (string, error) {
286 286
 	parts := strings.Split(netMode, ":")
287 287
 	switch mode := parts[0]; mode {
288
-	case "bridge", "disable":
289
-		return mode, "", nil
288
+	case "bridge", "none":
289
+		return mode, nil
290 290
 	case "container":
291
-		var container string
292 291
 		if len(parts) < 2 || parts[1] == "" {
293
-			return "", "", fmt.Errorf("'container:' netmode requires a container id or name", netMode)
292
+			return "", fmt.Errorf("'container:' netmode requires a container id or name", netMode)
294 293
 		}
295
-		container = parts[1]
296
-		return mode, container, nil
294
+		return netMode, nil
297 295
 	default:
298
-		return "", "", fmt.Errorf("invalid netmode: %q", netMode)
296
+		return "", fmt.Errorf("invalid netmode: %q", netMode)
299 297
 	}
300 298
 }
... ...
@@ -40,7 +40,7 @@ func TestParseNetMode(t *testing.T) {
40 40
 	}
41 41
 
42 42
 	for _, to := range testFlags {
43
-		mode, container, err := parseNetMode(to.flag)
43
+		mode, err := parseNetMode(to.flag)
44 44
 		if mode != to.mode {
45 45
 			t.Fatalf("-net %s: expected net mode: %q, got: %q", to.flag, to.mode, mode)
46 46
 		}