Browse code

Add --net=container with --publish --publish-all --expose error out

Signed-off-by: Lei Jitang <leijitang@huawei.com>

Lei Jitang authored on 2015/05/27 16:31:06
Showing 3 changed files
... ...
@@ -319,7 +319,8 @@ With the networking mode set to `container` a container will share the
319 319
 network stack of another container.  The other container's name must be
320 320
 provided in the format of `--net container:<name|id>`. Note that `--add-host` 
321 321
 `--hostname` `--dns` `--dns-search` and `--mac-address` is invalid 
322
-in `container` netmode.
322
+in `container` netmode, and `--publish` `--publish-all` `--expose` are also
323
+invalid in `container` netmode.
323 324
 
324 325
 Example running a Redis container with Redis binding to `localhost` then
325 326
 running the `redis-cli` command and connecting to the Redis server over the
... ...
@@ -3186,3 +3186,30 @@ func (s *DockerSuite) TestRunUnshareProc(c *check.C) {
3186 3186
 		c.Fatalf("unshare should have failed with permission denied, got: %s, %v", out, err)
3187 3187
 	}
3188 3188
 }
3189
+
3190
+func (s *DockerSuite) TestRunContainerNetModeWithExposePort(c *check.C) {
3191
+	cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top")
3192
+	out, _, err := runCommandWithOutput(cmd)
3193
+	if err != nil {
3194
+		c.Fatalf("failed to run container: %v, output: %q", err, out)
3195
+	}
3196
+
3197
+	cmd = exec.Command(dockerBinary, "run", "-p", "5000:5000", "--net=container:parent", "busybox")
3198
+	out, _, err = runCommandWithOutput(cmd)
3199
+	if err == nil || !strings.Contains(out, "Conflicting options: -p, -P, --publish-all, --publish and the network mode (--net)") {
3200
+		c.Fatalf("run --net=container with -p should error out")
3201
+	}
3202
+
3203
+	cmd = exec.Command(dockerBinary, "run", "-P", "--net=container:parent", "busybox")
3204
+	out, _, err = runCommandWithOutput(cmd)
3205
+	if err == nil || !strings.Contains(out, "Conflicting options: -p, -P, --publish-all, --publish and the network mode (--net)") {
3206
+		c.Fatalf("run --net=container with -P should error out")
3207
+	}
3208
+
3209
+	cmd = exec.Command(dockerBinary, "run", "--expose", "5000", "--net=container:parent", "busybox")
3210
+	out, _, err = runCommandWithOutput(cmd)
3211
+	if err == nil || !strings.Contains(out, "Conflicting options: --expose and the network mode (--expose)") {
3212
+		c.Fatalf("run --net=container with --expose should error out")
3213
+	}
3214
+
3215
+}
... ...
@@ -20,6 +20,8 @@ var (
20 20
 	ErrConflictHostNetworkAndLinks      = fmt.Errorf("Conflicting options: --net=host can't be used with links. This would result in undefined behavior")
21 21
 	ErrConflictContainerNetworkAndMac   = fmt.Errorf("Conflicting options: --mac-address and the network mode (--net)")
22 22
 	ErrConflictNetworkHosts             = fmt.Errorf("Conflicting options: --add-host and the network mode (--net)")
23
+	ErrConflictNetworkPublishPorts      = fmt.Errorf("Conflicting options: -p, -P, --publish-all, --publish and the network mode (--net)")
24
+	ErrConflictNetworkExposePorts       = fmt.Errorf("Conflicting options: --expose and the network mode (--expose)")
23 25
 )
24 26
 
25 27
 func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSet, error) {
... ...
@@ -143,6 +145,13 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
143 143
 		return nil, nil, cmd, ErrConflictContainerNetworkAndMac
144 144
 	}
145 145
 
146
+	if netMode.IsContainer() && (flPublish.Len() > 0 || *flPublishAll == true) {
147
+		return nil, nil, cmd, ErrConflictNetworkPublishPorts
148
+	}
149
+
150
+	if netMode.IsContainer() && flExpose.Len() > 0 {
151
+		return nil, nil, cmd, ErrConflictNetworkExposePorts
152
+	}
146 153
 	// Validate the input mac address
147 154
 	if *flMacAddress != "" {
148 155
 		if _, err := opts.ValidateMACAddress(*flMacAddress); err != nil {