瀏覽代碼

Fix regression on --link on bridge network

Signed-off-by: Alessandro Boch <aboch@docker.com>
Alessandro Boch 9 年之前
父節點
當前提交
3a3f800ff4
共有 2 個文件被更改,包括 32 次插入6 次删除
  1. 3 3
      daemon/container_operations.go
  2. 29 3
      integration-cli/docker_cli_links_test.go

+ 3 - 3
daemon/container_operations.go

@@ -177,11 +177,12 @@ func (daemon *Daemon) buildSandboxOptions(container *container.Container) ([]lib
 	// Legacy Link feature is supported only for the default bridge network.
 	// return if this call to build join options is not for default bridge network
 	// Legacy Link is only supported by docker run --link
-	if _, ok := container.NetworkSettings.Networks[defaultNetName]; !container.HostConfig.NetworkMode.IsDefault() || !ok {
+	bridgeSettings, ok := container.NetworkSettings.Networks[defaultNetName]
+	if !ok {
 		return sboxOptions, nil
 	}
 
-	if container.NetworkSettings.Networks[defaultNetName].EndpointID == "" {
+	if bridgeSettings.EndpointID == "" {
 		return sboxOptions, nil
 	}
 
@@ -209,7 +210,6 @@ func (daemon *Daemon) buildSandboxOptions(container *container.Container) ([]lib
 		}
 	}
 
-	bridgeSettings := container.NetworkSettings.Networks[defaultNetName]
 	for alias, parent := range daemon.parents(container) {
 		if daemon.configStore.DisableBridge || !container.HostConfig.NetworkMode.IsPrivate() {
 			continue

+ 29 - 3
integration-cli/docker_cli_links_test.go

@@ -31,10 +31,33 @@ func (s *DockerSuite) TestLinksInvalidContainerTarget(c *check.C) {
 
 func (s *DockerSuite) TestLinksPingLinkedContainers(c *check.C) {
 	testRequires(c, DaemonIsLinux)
-	dockerCmd(c, "run", "-d", "--name", "container1", "--hostname", "fred", "busybox", "top")
-	dockerCmd(c, "run", "-d", "--name", "container2", "--hostname", "wilma", "busybox", "top")
+	// Test with the three different ways of specifying the default network on Linux
+	testLinkPingOnNetwork(c, "")
+	testLinkPingOnNetwork(c, "default")
+	testLinkPingOnNetwork(c, "bridge")
+}
+
+func testLinkPingOnNetwork(c *check.C, network string) {
+	var postArgs []string
+	if network != "" {
+		postArgs = append(postArgs, []string{"--net", network}...)
+	}
+	postArgs = append(postArgs, []string{"busybox", "top"}...)
+	runArgs1 := append([]string{"run", "-d", "--name", "container1", "--hostname", "fred"}, postArgs...)
+	runArgs2 := append([]string{"run", "-d", "--name", "container2", "--hostname", "wilma"}, postArgs...)
+
+	// Run the two named containers
+	dockerCmd(c, runArgs1...)
+	dockerCmd(c, runArgs2...)
+
+	postArgs = []string{}
+	if network != "" {
+		postArgs = append(postArgs, []string{"--net", network}...)
+	}
+	postArgs = append(postArgs, []string{"busybox", "sh", "-c"}...)
 
-	runArgs := []string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sh", "-c"}
+	// Format a run for a container which links to the other two
+	runArgs := append([]string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2"}, postArgs...)
 	pingCmd := "ping -c 1 %s -W 1 && ping -c 1 %s -W 1"
 
 	// test ping by alias, ping by name, and ping by hostname
@@ -45,6 +68,9 @@ func (s *DockerSuite) TestLinksPingLinkedContainers(c *check.C) {
 	// 3. Ping by hostname
 	dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
 
+	// Clean for next round
+	dockerCmd(c, "rm", "-f", "container1")
+	dockerCmd(c, "rm", "-f", "container2")
 }
 
 func (s *DockerSuite) TestLinksPingLinkedContainersAfterRename(c *check.C) {