Browse code

Merge pull request #11516 from coolljt0725/add_show_error_set_some_flag_in_container_netmode

add support --net=container with --mac-address, --add-host error out

Jessie Frazelle authored on 2015/05/08 09:13:32
Showing 5 changed files
... ...
@@ -1116,7 +1116,7 @@ func (container *Container) setupContainerDns() error {
1116 1116
 		return err
1117 1117
 	}
1118 1118
 
1119
-	if config.NetworkMode != "host" {
1119
+	if config.NetworkMode.IsBridge() || config.NetworkMode.IsNone() {
1120 1120
 		// check configurations for any container/daemon dns settings
1121 1121
 		if len(config.Dns) > 0 || len(daemon.config.Dns) > 0 || len(config.DnsSearch) > 0 || len(daemon.config.DnsSearch) > 0 {
1122 1122
 			var (
... ...
@@ -282,7 +282,8 @@ With the networking mode set to `host` a container will share the host's
282 282
 network stack and all interfaces from the host will be available to the
283 283
 container.  The container's hostname will match the hostname on the host
284 284
 system.  Publishing ports and linking to other containers will not work
285
-when sharing the host's network stack.
285
+when sharing the host's network stack. Note that `--add-host` `--hostname`
286
+`--dns` `--dns-search` and `--mac-address` is invalid in `host` netmode.
286 287
 
287 288
 Compared to the default `bridge` mode, the `host` mode gives *significantly*
288 289
 better networking performance since it uses the host's native networking stack
... ...
@@ -298,7 +299,9 @@ or a High Performance Web Server.
298 298
 
299 299
 With the networking mode set to `container` a container will share the
300 300
 network stack of another container.  The other container's name must be
301
-provided in the format of `--net container:<name|id>`.
301
+provided in the format of `--net container:<name|id>`. Note that `--add-host` 
302
+`--hostname` `--dns` `--dns-search` and `--mac-address` is invalid 
303
+in `container` netmode.
302 304
 
303 305
 Example running a Redis container with Redis binding to `localhost` then
304 306
 running the `redis-cli` command and connecting to the Redis server over the
... ...
@@ -371,6 +371,33 @@ func (s *DockerSuite) TestRunLinkToContainerNetMode(c *check.C) {
371 371
 	}
372 372
 }
373 373
 
374
+func (s *DockerSuite) TestRunContainerNetModeWithDnsMacHosts(c *check.C) {
375
+	cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top")
376
+	out, _, err := runCommandWithOutput(cmd)
377
+	if err != nil {
378
+		c.Fatalf("failed to run container: %v, output: %q", err, out)
379
+	}
380
+
381
+	cmd = exec.Command(dockerBinary, "run", "--dns", "1.2.3.4", "--net=container:parent", "busybox")
382
+	out, _, err = runCommandWithOutput(cmd)
383
+	if err == nil || !strings.Contains(out, "Conflicting options: --dns and the network mode") {
384
+		c.Fatalf("run --net=container with --dns should error out")
385
+	}
386
+
387
+	cmd = exec.Command(dockerBinary, "run", "--mac-address", "92:d0:c6:0a:29:33", "--net=container:parent", "busybox")
388
+	out, _, err = runCommandWithOutput(cmd)
389
+	if err == nil || !strings.Contains(out, "--mac-address and the network mode") {
390
+		c.Fatalf("run --net=container with --mac-address should error out")
391
+	}
392
+
393
+	cmd = exec.Command(dockerBinary, "run", "--add-host", "test:192.168.2.109", "--net=container:parent", "busybox")
394
+	out, _, err = runCommandWithOutput(cmd)
395
+	if err == nil || !strings.Contains(out, "--add-host and the network mode") {
396
+		c.Fatalf("run --net=container with --add-host should error out")
397
+	}
398
+
399
+}
400
+
374 401
 func (s *DockerSuite) TestRunModeNetContainerHostname(c *check.C) {
375 402
 	testRequires(c, ExecSupport)
376 403
 	cmd := exec.Command(dockerBinary, "run", "-i", "-d", "--name", "parent", "busybox", "top")
... ...
@@ -21,6 +21,10 @@ func (n NetworkMode) IsPrivate() bool {
21 21
 	return !(n.IsHost() || n.IsContainer() || n.IsNone())
22 22
 }
23 23
 
24
+func (n NetworkMode) IsBridge() bool {
25
+	return n == "bridge"
26
+}
27
+
24 28
 func (n NetworkMode) IsHost() bool {
25 29
 	return n == "host"
26 30
 }
... ...
@@ -15,10 +15,11 @@ import (
15 15
 
16 16
 var (
17 17
 	ErrConflictContainerNetworkAndLinks = fmt.Errorf("Conflicting options: --net=container can't be used with links. This would result in undefined behavior.")
18
-	ErrConflictContainerNetworkAndDns   = fmt.Errorf("Conflicting options: --net=container can't be used with --dns. This configuration is invalid.")
18
+	ErrConflictNetworkAndDns            = fmt.Errorf("Conflicting options: --dns and the network mode (--net).")
19 19
 	ErrConflictNetworkHostname          = fmt.Errorf("Conflicting options: -h and the network mode (--net)")
20
-	ErrConflictHostNetworkAndDns        = fmt.Errorf("Conflicting options: --net=host can't be used with --dns. This configuration is invalid.")
21 20
 	ErrConflictHostNetworkAndLinks      = fmt.Errorf("Conflicting options: --net=host can't be used with links. This would result in undefined behavior.")
21
+	ErrConflictContainerNetworkAndMac   = fmt.Errorf("Conflicting options: --mac-address and the network mode (--net).")
22
+	ErrConflictNetworkHosts             = fmt.Errorf("Conflicting options: --add-host and the network mode (--net).")
22 23
 )
23 24
 
24 25
 func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSet, error) {
... ...
@@ -101,36 +102,46 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
101 101
 		return nil, nil, cmd, err
102 102
 	}
103 103
 
104
-	// Validate input params starting with the input mac address
105
-	if *flMacAddress != "" {
106
-		if _, err := opts.ValidateMACAddress(*flMacAddress); err != nil {
107
-			return nil, nil, cmd, fmt.Errorf("%s is not a valid mac address", *flMacAddress)
108
-		}
109
-	}
110 104
 	var (
111 105
 		attachStdin  = flAttach.Get("stdin")
112 106
 		attachStdout = flAttach.Get("stdout")
113 107
 		attachStderr = flAttach.Get("stderr")
114 108
 	)
115 109
 
116
-	if *flNetMode != "bridge" && *flNetMode != "none" && *flHostname != "" {
110
+	netMode, err := parseNetMode(*flNetMode)
111
+	if err != nil {
112
+		return nil, nil, cmd, fmt.Errorf("--net: invalid net mode: %v", err)
113
+	}
114
+
115
+	if (netMode.IsHost() || netMode.IsContainer()) && *flHostname != "" {
117 116
 		return nil, nil, cmd, ErrConflictNetworkHostname
118 117
 	}
119 118
 
120
-	if *flNetMode == "host" && flLinks.Len() > 0 {
119
+	if netMode.IsHost() && flLinks.Len() > 0 {
121 120
 		return nil, nil, cmd, ErrConflictHostNetworkAndLinks
122 121
 	}
123 122
 
124
-	if strings.HasPrefix(*flNetMode, "container") && flLinks.Len() > 0 {
123
+	if netMode.IsContainer() && flLinks.Len() > 0 {
125 124
 		return nil, nil, cmd, ErrConflictContainerNetworkAndLinks
126 125
 	}
127 126
 
128
-	if *flNetMode == "host" && flDns.Len() > 0 {
129
-		return nil, nil, cmd, ErrConflictHostNetworkAndDns
127
+	if (netMode.IsHost() || netMode.IsContainer()) && flDns.Len() > 0 {
128
+		return nil, nil, cmd, ErrConflictNetworkAndDns
129
+	}
130
+
131
+	if (netMode.IsContainer() || netMode.IsHost()) && flExtraHosts.Len() > 0 {
132
+		return nil, nil, cmd, ErrConflictNetworkHosts
130 133
 	}
131 134
 
132
-	if strings.HasPrefix(*flNetMode, "container") && flDns.Len() > 0 {
133
-		return nil, nil, cmd, ErrConflictContainerNetworkAndDns
135
+	if (netMode.IsContainer() || netMode.IsHost()) && *flMacAddress != "" {
136
+		return nil, nil, cmd, ErrConflictContainerNetworkAndMac
137
+	}
138
+
139
+	// Validate the input mac address
140
+	if *flMacAddress != "" {
141
+		if _, err := opts.ValidateMACAddress(*flMacAddress); err != nil {
142
+			return nil, nil, cmd, fmt.Errorf("%s is not a valid mac address", *flMacAddress)
143
+		}
134 144
 	}
135 145
 
136 146
 	// If neither -d or -a are set, attach to everything by default
... ...
@@ -267,11 +278,6 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
267 267
 		return nil, nil, cmd, fmt.Errorf("--pid: invalid PID mode")
268 268
 	}
269 269
 
270
-	netMode, err := parseNetMode(*flNetMode)
271
-	if err != nil {
272
-		return nil, nil, cmd, fmt.Errorf("--net: invalid net mode: %v", err)
273
-	}
274
-
275 270
 	restartPolicy, err := ParseRestartPolicy(*flRestartPolicy)
276 271
 	if err != nil {
277 272
 		return nil, nil, cmd, err