From 400d92871cb300ee8696dbcedcfd6a95f4d9d5a7 Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Tue, 13 Jan 2015 09:38:12 +0800 Subject: [PATCH 1/2] Add --link accept container ID Signed-off-by: Lei Jitang --- daemon/daemon.go | 5 +---- runconfig/parse.go | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/daemon/daemon.go b/daemon/daemon.go index 5972b4f87d..68850ad878 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -752,10 +752,7 @@ func (daemon *Daemon) RegisterLinks(container *Container, hostConfig *runconfig. if err != nil { return err } - child, err := daemon.GetByName(parts["name"]) - if err != nil { - return err - } + child := daemon.Get(parts["name"]) if child == nil { return fmt.Errorf("Could not get container for %s", parts["name"]) } diff --git a/runconfig/parse.go b/runconfig/parse.go index 3bab8ac765..e6807890ac 100644 --- a/runconfig/parse.go +++ b/runconfig/parse.go @@ -66,7 +66,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(&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 :alias") 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") From 2292167b02ce0489d38e0a6beeb792bcb7534f34 Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Wed, 14 Jan 2015 15:25:58 +0800 Subject: [PATCH 2/2] Add tests for --link Signed-off-by: Lei Jitang --- integration-cli/docker_cli_run_test.go | 54 ++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index 18495be410..fb8e8d6947 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -314,6 +314,60 @@ func TestRunWithoutNetworking(t *testing.T) { 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 func TestRunWithVolumesAsFiles(t *testing.T) { runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/etc/hosts:/target-file", "busybox", "true")