Browse code

Allow port mapping only for endpoint created on docker run

Signed-off-by: Santhosh Manohar <santhosh@docker.com>

Santhosh Manohar authored on 2015/11/06 22:34:49
Showing 2 changed files
... ...
@@ -611,7 +611,9 @@ func (container *Container) buildPortMapInfo(ep libnetwork.Endpoint, networkSett
611 611
 		return networkSettings, nil
612 612
 	}
613 613
 
614
-	networkSettings.Ports = nat.PortMap{}
614
+	if networkSettings.Ports == nil {
615
+		networkSettings.Ports = nat.PortMap{}
616
+	}
615 617
 
616 618
 	if expData, ok := driverInfo[netlabel.ExposedPorts]; ok {
617 619
 		if exposedPorts, ok := expData.([]types.TransportPort); ok {
... ...
@@ -810,6 +812,17 @@ func (container *Container) buildCreateEndpointOptions(n libnetwork.Network) ([]
810 810
 		createOptions []libnetwork.EndpointOption
811 811
 	)
812 812
 
813
+	if n.Name() == "bridge" || container.NetworkSettings.IsAnonymousEndpoint {
814
+		createOptions = append(createOptions, libnetwork.CreateOptionAnonymous())
815
+	}
816
+
817
+	// Other configs are applicable only for the endpoint in the network
818
+	// to which container was connected to on docker run.
819
+	if n.Name() != container.hostConfig.NetworkMode.NetworkName() &&
820
+		!(n.Name() == "bridge" && container.hostConfig.NetworkMode.IsDefault()) {
821
+		return createOptions, nil
822
+	}
823
+
813 824
 	if container.Config.ExposedPorts != nil {
814 825
 		portSpecs = container.Config.ExposedPorts
815 826
 	}
... ...
@@ -879,10 +892,6 @@ func (container *Container) buildCreateEndpointOptions(n libnetwork.Network) ([]
879 879
 		createOptions = append(createOptions, libnetwork.EndpointOptionGeneric(genericOption))
880 880
 	}
881 881
 
882
-	if n.Name() == "bridge" || container.NetworkSettings.IsAnonymousEndpoint {
883
-		createOptions = append(createOptions, libnetwork.CreateOptionAnonymous())
884
-	}
885
-
886 882
 	return createOptions, nil
887 883
 }
888 884
 
... ...
@@ -525,6 +525,7 @@ func (s *DockerNetworkSuite) TestDockerNetworkAnonymousEndpoint(c *check.C) {
525 525
 	testRequires(c, ExecSupport)
526 526
 	hostsFile := "/etc/hosts"
527 527
 	cstmBridgeNw := "custom-bridge-nw"
528
+	cstmBridgeNw1 := "custom-bridge-nw1"
528 529
 
529 530
 	dockerCmd(c, "network", "create", "-d", "bridge", cstmBridgeNw)
530 531
 	assertNwIsAvailable(c, cstmBridgeNw)
... ...
@@ -548,6 +549,18 @@ func (s *DockerNetworkSuite) TestDockerNetworkAnonymousEndpoint(c *check.C) {
548 548
 	c.Assert(string(hosts1), checker.Equals, string(hosts1post),
549 549
 		check.Commentf("Unexpected %s change on anonymous container creation", hostsFile))
550 550
 
551
+	// Connect the 2nd container to a new network and verify the
552
+	// first container /etc/hosts file still hasn't changed.
553
+	dockerCmd(c, "network", "create", "-d", "bridge", cstmBridgeNw1)
554
+	assertNwIsAvailable(c, cstmBridgeNw1)
555
+
556
+	dockerCmd(c, "network", "connect", cstmBridgeNw1, cid2)
557
+
558
+	hosts1post, err = readContainerFileWithExec(cid1, hostsFile)
559
+	c.Assert(err, checker.IsNil)
560
+	c.Assert(string(hosts1), checker.Equals, string(hosts1post),
561
+		check.Commentf("Unexpected %s change on container connect", hostsFile))
562
+
551 563
 	// start a named container
552 564
 	cName := "AnyName"
553 565
 	out, _ = dockerCmd(c, "run", "-d", "--net", cstmBridgeNw, "--name", cName, "busybox", "top")
... ...
@@ -782,3 +795,10 @@ func (s *DockerNetworkSuite) TestDockerNetworkDisconnectFromHost(c *check.C) {
782 782
 	c.Assert(err, checker.NotNil, check.Commentf("Should err out disconnect from host"))
783 783
 	c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetwork.Error())
784 784
 }
785
+
786
+func (s *DockerNetworkSuite) TestDockerNetworkConnectWithPortMapping(c *check.C) {
787
+	dockerCmd(c, "network", "create", "test1")
788
+	dockerCmd(c, "run", "-d", "--name", "c1", "-p", "5000:5000", "busybox", "top")
789
+	c.Assert(waitRun("c1"), check.IsNil)
790
+	dockerCmd(c, "network", "connect", "test1", "c1")
791
+}