Browse code

Merge pull request #13502 from coolljt0725/conflict_port_and_netmode

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

Sebastiaan van Stijn authored on 2015/06/15 23:25:59
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
... ...
@@ -3177,3 +3177,30 @@ func (s *DockerSuite) TestDevicePermissions(c *check.C) {
3177 3177
 		c.Fatalf("output should begin with %q, got %q", permissions, out)
3178 3178
 	}
3179 3179
 }
3180
+
3181
+func (s *DockerSuite) TestRunContainerNetModeWithExposePort(c *check.C) {
3182
+	cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top")
3183
+	out, _, err := runCommandWithOutput(cmd)
3184
+	if err != nil {
3185
+		c.Fatalf("failed to run container: %v, output: %q", err, out)
3186
+	}
3187
+
3188
+	cmd = exec.Command(dockerBinary, "run", "-p", "5000:5000", "--net=container:parent", "busybox")
3189
+	out, _, err = runCommandWithOutput(cmd)
3190
+	if err == nil || !strings.Contains(out, "Conflicting options: -p, -P, --publish-all, --publish and the network mode (--net)") {
3191
+		c.Fatalf("run --net=container with -p should error out")
3192
+	}
3193
+
3194
+	cmd = exec.Command(dockerBinary, "run", "-P", "--net=container:parent", "busybox")
3195
+	out, _, err = runCommandWithOutput(cmd)
3196
+	if err == nil || !strings.Contains(out, "Conflicting options: -p, -P, --publish-all, --publish and the network mode (--net)") {
3197
+		c.Fatalf("run --net=container with -P should error out")
3198
+	}
3199
+
3200
+	cmd = exec.Command(dockerBinary, "run", "--expose", "5000", "--net=container:parent", "busybox")
3201
+	out, _, err = runCommandWithOutput(cmd)
3202
+	if err == nil || !strings.Contains(out, "Conflicting options: --expose and the network mode (--expose)") {
3203
+		c.Fatalf("run --net=container with --expose should error out")
3204
+	}
3205
+
3206
+}
... ...
@@ -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 {