Browse code

Fix bug in initializeNetwork()

- On `docker run --net <network id> ...`
the bug would cause the container to attempt
to connect to the network two times
- Also made sure endpoint creation rollback will
be executed on failures in `func (container *Container) connectToNetwork()`

Signed-off-by: Alessandro Boch <aboch@docker.com>

Alessandro Boch authored on 2015/10/31 12:32:03
Showing 2 changed files
... ...
@@ -914,6 +914,13 @@ func (container *Container) allocateNetwork() error {
914 914
 		if mode.IsDefault() {
915 915
 			networkName = controller.Config().Daemon.DefaultNetwork
916 916
 		}
917
+		if mode.IsUserDefined() {
918
+			n, err := container.daemon.FindNetwork(networkName)
919
+			if err != nil {
920
+				return err
921
+			}
922
+			networkName = n.Name()
923
+		}
917 924
 		container.NetworkSettings.Networks = make(map[string]*network.EndpointSettings)
918 925
 		container.NetworkSettings.Networks[networkName] = new(network.EndpointSettings)
919 926
 		updateSettings = true
... ...
@@ -954,9 +961,7 @@ func (container *Container) ConnectToNetwork(idOrName string) error {
954 954
 	return nil
955 955
 }
956 956
 
957
-func (container *Container) connectToNetwork(idOrName string, updateSettings bool) error {
958
-	var err error
959
-
957
+func (container *Container) connectToNetwork(idOrName string, updateSettings bool) (err error) {
960 958
 	if container.hostConfig.NetworkMode.IsContainer() {
961 959
 		return runconfig.ErrConflictSharedNetwork
962 960
 	}
... ...
@@ -733,3 +733,8 @@ func (s *DockerNetworkSuite) TestDockerNetworkMultipleNetworksUngracefulDaemonRe
733 733
 
734 734
 	verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList)
735 735
 }
736
+
737
+func (s *DockerNetworkSuite) TestDockerNetworkRunNetByID(c *check.C) {
738
+	out, _ := dockerCmd(c, "network", "create", "one")
739
+	dockerCmd(c, "run", "-d", "--net", strings.TrimSpace(out), "busybox", "top")
740
+}