Browse Source

Running a container that links to a container with --net host
should throw an error.

Docker-DCO-1.1-Signed-off-by: Jessica Frazelle <jess@docker.com> (github: jfrazelle)

Jessica Frazelle 10 years ago
parent
commit
6743be44ca
2 changed files with 19 additions and 0 deletions
  1. 3 0
      daemon/daemon.go
  2. 16 0
      integration-cli/docker_cli_links_test.go

+ 3 - 0
daemon/daemon.go

@@ -696,6 +696,9 @@ func (daemon *Daemon) RegisterLinks(container *Container, hostConfig *runconfig.
 			if child == nil {
 				return fmt.Errorf("Could not get container for %s", parts["name"])
 			}
+			if child.hostConfig.NetworkMode.IsHost() {
+				return runconfig.ErrConflictHostNetworkAndLinks
+			}
 			if err := daemon.RegisterLink(container, child, parts["alias"]); err != nil {
 				return err
 			}

+ 16 - 0
integration-cli/docker_cli_links_test.go

@@ -215,3 +215,19 @@ func TestLinksHostsFilesInject(t *testing.T) {
 
 	logDone("link - ensure containers hosts files are updated with the link alias.")
 }
+
+func TestLinksNetworkHostContainer(t *testing.T) {
+	defer deleteAllContainers()
+
+	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top"))
+	if err != nil {
+		t.Fatal(err, out)
+	}
+
+	out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true"))
+	if err == nil || !strings.Contains(out, "--net=host can't be used with links. This would result in undefined behavior.") {
+		t.Fatalf("Running container linking to a container with --net host should have failed: %s", out)
+	}
+
+	logDone("link - error thrown when linking to container with --net host")
+}