diff --git a/daemon/container_operations.go b/daemon/container_operations.go index 8c731ed0d2..9132684bab 100644 --- a/daemon/container_operations.go +++ b/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 diff --git a/integration-cli/docker_cli_links_test.go b/integration-cli/docker_cli_links_test.go index 322b58c653..d4bfc6a128 100644 --- a/integration-cli/docker_cli_links_test.go +++ b/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") +} - runArgs := []string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sh", "-c"} +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"}...) + + // 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) {