Skip to content

Commit

Permalink
Merge pull request #10056 from coolljt0725/add_link_accept_ID
Browse files Browse the repository at this point in the history
Add --link accept container ID
  • Loading branch information
crosbymichael committed Jan 14, 2015
2 parents 71ab2ef + 2292167 commit 37b6940
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
5 changes: 1 addition & 4 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
}
Expand Down
54 changes: 54 additions & 0 deletions integration-cli/docker_cli_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion runconfig/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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(&flEnv, []string{"e", "-env"}, "Set environment variables")
Expand Down

0 comments on commit 37b6940

Please sign in to comment.