diff --git a/daemon/daemon.go b/daemon/daemon.go index c9fc2f4acf..9f5df4c3c5 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -753,10 +753,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/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index da7f0b1ea2..5108d197dc 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") diff --git a/runconfig/parse.go b/runconfig/parse.go index 781f721f65..390f70dbf2 100644 --- a/runconfig/parse.go +++ b/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(&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")