浏览代码

Merge pull request #10056 from coolljt0725/add_link_accept_ID

Add --link accept container ID
Michael Crosby 10 年之前
父节点
当前提交
37b69408f8
共有 3 个文件被更改,包括 56 次插入5 次删除
  1. 1 4
      daemon/daemon.go
  2. 54 0
      integration-cli/docker_cli_run_test.go
  3. 1 1
      runconfig/parse.go

+ 1 - 4
daemon/daemon.go

@@ -753,10 +753,7 @@ func (daemon *Daemon) RegisterLinks(container *Container, hostConfig *runconfig.
 			if err != nil {
 			if err != nil {
 				return err
 				return err
 			}
 			}
-			child, err := daemon.GetByName(parts["name"])
-			if err != nil {
-				return err
-			}
+			child := daemon.Get(parts["name"])
 			if child == nil {
 			if child == nil {
 				return fmt.Errorf("Could not get container for %s", parts["name"])
 				return fmt.Errorf("Could not get container for %s", parts["name"])
 			}
 			}

+ 54 - 0
integration-cli/docker_cli_run_test.go

@@ -314,6 +314,60 @@ func TestRunWithoutNetworking(t *testing.T) {
 	logDone("run - disable networking with -n=false")
 	logDone("run - disable networking with -n=false")
 }
 }
 
 
+//test --link use container name to link target
+func TestRunLinksContainerWithContainerName(t *testing.T) {
+	cmd := exec.Command(dockerBinary, "run", "-t", "-d", "--name", "parent", "busybox")
+	out, _, _, err := runCommandWithStdoutStderr(cmd)
+	if err != nil {
+		t.Fatal("failed to run container: %v, output: %q", err, out)
+	}
+	cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.NetworkSettings.IPAddress}}", "parent")
+	ip, _, _, err := runCommandWithStdoutStderr(cmd)
+	if err != nil {
+		t.Fatal("failed to inspect container: %v, output: %q", err, ip)
+	}
+	ip = strings.TrimSpace(ip)
+	cmd = exec.Command(dockerBinary, "run", "--link", "parent:test", "busybox", "/bin/cat", "/etc/hosts")
+	out, _, err = runCommandWithOutput(cmd)
+	if err != nil {
+		t.Fatal("failed to run container: %v, output: %q", err, out)
+	}
+	if !strings.Contains(out, ip+"	test") {
+		t.Fatalf("use a container name to link target failed")
+	}
+	deleteAllContainers()
+
+	logDone("run - use a container name to link target work")
+}
+
+//test --link use container id to link target
+func TestRunLinksContainerWithContainerId(t *testing.T) {
+	cmd := exec.Command(dockerBinary, "run", "-t", "-d", "busybox")
+	cID, _, _, err := runCommandWithStdoutStderr(cmd)
+	if err != nil {
+		t.Fatal("failed to run container: %v, output: %q", err, cID)
+	}
+	cID = strings.TrimSpace(cID)
+	cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.NetworkSettings.IPAddress}}", cID)
+	ip, _, _, err := runCommandWithStdoutStderr(cmd)
+	if err != nil {
+		t.Fatal("faild to inspect container: %v, output: %q", err, ip)
+	}
+	ip = strings.TrimSpace(ip)
+	cmd = exec.Command(dockerBinary, "run", "--link", cID+":test", "busybox", "/bin/cat", "/etc/hosts")
+	out, _, err := runCommandWithOutput(cmd)
+	if err != nil {
+		t.Fatal("failed to run container: %v, output: %q", err, out)
+	}
+	if !strings.Contains(out, ip+"	test") {
+		t.Fatalf("use a container id to link target failed")
+	}
+
+	deleteAllContainers()
+
+	logDone("run - use a container id to link target work")
+}
+
 // Regression test for #4741
 // Regression test for #4741
 func TestRunWithVolumesAsFiles(t *testing.T) {
 func TestRunWithVolumesAsFiles(t *testing.T) {
 	runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/etc/hosts:/target-file", "busybox", "true")
 	runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/etc/hosts:/target-file", "busybox", "true")

+ 1 - 1
runconfig/parse.go

@@ -67,7 +67,7 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
 
 
 	cmd.Var(&flAttach, []string{"a", "-attach"}, "Attach to STDIN, STDOUT or STDERR.")
 	cmd.Var(&flAttach, []string{"a", "-attach"}, "Attach to STDIN, STDOUT or STDERR.")
 	cmd.Var(&flVolumes, []string{"v", "-volume"}, "Bind mount a volume (e.g., from the host: -v /host:/container, from Docker: -v /container)")
 	cmd.Var(&flVolumes, []string{"v", "-volume"}, "Bind mount a volume (e.g., from the host: -v /host:/container, from Docker: -v /container)")
-	cmd.Var(&flLinks, []string{"#link", "-link"}, "Add link to another container in the form of name:alias")
+	cmd.Var(&flLinks, []string{"#link", "-link"}, "Add link to another container in the form of <name|id>:alias")
 	cmd.Var(&flDevices, []string{"-device"}, "Add a host device to the container (e.g. --device=/dev/sdc:/dev/xvdc:rwm)")
 	cmd.Var(&flDevices, []string{"-device"}, "Add a host device to the container (e.g. --device=/dev/sdc:/dev/xvdc:rwm)")
 
 
 	cmd.Var(&flEnv, []string{"e", "-env"}, "Set environment variables")
 	cmd.Var(&flEnv, []string{"e", "-env"}, "Set environment variables")