From cd818950919f0da868b36e32033607d5e6c98466 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Mon, 5 May 2014 10:47:55 -0700 Subject: [PATCH 1/3] Add alex as devmapper and btrfs maintainer Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- daemon/graphdriver/btrfs/MAINTAINERS | 1 + daemon/graphdriver/devmapper/MAINTAINERS | 1 + 2 files changed, 2 insertions(+) create mode 100644 daemon/graphdriver/btrfs/MAINTAINERS create mode 100644 daemon/graphdriver/devmapper/MAINTAINERS diff --git a/daemon/graphdriver/btrfs/MAINTAINERS b/daemon/graphdriver/btrfs/MAINTAINERS new file mode 100644 index 0000000000..9e629d5fcc --- /dev/null +++ b/daemon/graphdriver/btrfs/MAINTAINERS @@ -0,0 +1 @@ +Alexander Larsson (@alexlarsson) diff --git a/daemon/graphdriver/devmapper/MAINTAINERS b/daemon/graphdriver/devmapper/MAINTAINERS new file mode 100644 index 0000000000..9e629d5fcc --- /dev/null +++ b/daemon/graphdriver/devmapper/MAINTAINERS @@ -0,0 +1 @@ +Alexander Larsson (@alexlarsson) From 53f38a14cd6b61a6b5df68cc3694dcba2b0c1eb7 Mon Sep 17 00:00:00 2001 From: Bryan Murphy Date: Mon, 7 Apr 2014 18:34:07 +0000 Subject: [PATCH 2/3] add linked containers to hosts file Docker-DCO-1.1-Signed-off-by: Bryan Murphy (github: bmurphy1976) Docker-DCO-1.1-Signed-off-by: Solomon Hykes (github: shykes) Tested-by: Solomon Hykes (github: shykes) --- daemon/container.go | 15 ++++++- docs/sources/reference/run.md | 9 +++- docs/sources/use/working_with_links_names.md | 29 +++++++++++++ integration-cli/docker_cli_links_test.go | 45 ++++++++++++++++++++ pkg/networkfs/etchosts/etchosts.go | 11 ++++- 5 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 integration-cli/docker_cli_links_test.go diff --git a/daemon/container.go b/daemon/container.go index f4cc125ca4..7b6b65494e 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -357,7 +357,20 @@ func (container *Container) buildHostnameAndHostsFiles(IP string) error { } container.HostsPath = path.Join(container.root, "hosts") - return etchosts.Build(container.HostsPath, IP, container.Config.Hostname, container.Config.Domainname) + + extraContent := make(map[string]string) + + children, err := container.daemon.Children(container.Name) + if err != nil { + return err + } + + for linkAlias, child := range children { + _, alias := path.Split(linkAlias) + extraContent[alias] = child.NetworkSettings.IPAddress + } + + return etchosts.Build(container.HostsPath, IP, container.Config.Hostname, container.Config.Domainname, &extraContent) } func (container *Container) allocateNetwork() error { diff --git a/docs/sources/reference/run.md b/docs/sources/reference/run.md index 521e8010e2..b6cb0a08fe 100644 --- a/docs/sources/reference/run.md +++ b/docs/sources/reference/run.md @@ -1,4 +1,4 @@ -page_title: Docker Run Reference +page_title: Docker Run Reference page_description: Configure containers at runtime page_keywords: docker, run, configure, runtime @@ -407,6 +407,13 @@ And we can use that information to connect from another container as a client: $ docker run -i -t --rm --link redis-name:redis_alias --entrypoint /bin/bash dockerfiles/redis -c '/redis-stable/src/redis-cli -h $REDIS_ALIAS_PORT_6379_TCP_ADDR -p $REDIS_ALIAS_PORT_6379_TCP_PORT' 172.17.0.32:6379> +Docker will also map the private IP address to the alias of a linked +container by inserting an entry into `/etc/hosts`. You can use this +mechanism to communicate with a linked container by its alias: + + $ docker run -d --name servicename busybox sleep 30 + $ docker run -i -t --link servicename:servicealias busybox ping -c 1 servicealias + ## VOLUME (Shared Filesystems) -v=[]: Create a bind mount with: [host-dir]:[container-dir]:[rw|ro]. diff --git a/docs/sources/use/working_with_links_names.md b/docs/sources/use/working_with_links_names.md index dab66cef06..6951e3c26f 100644 --- a/docs/sources/use/working_with_links_names.md +++ b/docs/sources/use/working_with_links_names.md @@ -109,3 +109,32 @@ the Redis container. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4c01db0b339c ubuntu:12.04 bash 17 seconds ago Up 16 seconds webapp d7886598dbe2 crosbymichael/redis:latest /redis-server --dir 33 minutes ago Up 33 minutes 6379/tcp redis,webapp/db + +## Resolving Links by Name + +New in version v0.11. + +Linked containers can be accessed by hostname. Hostnames are mapped by +appending entries to '/etc/hosts' using the linked container's alias. + +For example, linking a container using '--link redis:db' will generate the +following '/etc/hosts' file: + + root@6541a75d44a0:/# cat /etc/hosts + 172.17.0.3 6541a75d44a0 + 172.17.0.2 db + + 127.0.0.1 localhost + ::1 localhost ip6-localhost ip6-loopback + fe00::0 ip6-localnet + ff00::0 ip6-mcastprefix + ff02::1 ip6-allnodes + ff02::2 ip6-allrouters + root@6541a75d44a0:/# + +Using this mechanism, you can communicate with the linked container by +name: + + root@6541a75d44a0:/# echo PING | redis-cli -h db + PONG + root@6541a75d44a0:/# diff --git a/integration-cli/docker_cli_links_test.go b/integration-cli/docker_cli_links_test.go new file mode 100644 index 0000000000..5b43b3f8a9 --- /dev/null +++ b/integration-cli/docker_cli_links_test.go @@ -0,0 +1,45 @@ +package main + +import ( + "fmt" + "os/exec" + "testing" +) + +func TestPingUnlinkedContainers(t *testing.T) { + runCmd := exec.Command(dockerBinary, "run", "--rm", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1") + exitCode, err := runCommand(runCmd) + + if exitCode == 0 { + t.Fatal("run ping did not fail") + } else if exitCode != 1 { + errorOut(err, t, fmt.Sprintf("run ping failed with errors: %v", err)) + } +} + +func TestPingLinkedContainers(t *testing.T) { + cmd := exec.Command(dockerBinary, "run", "-d", "--name", "container1", "busybox", "sleep", "10") + out, _, err := runCommandWithOutput(cmd) + errorOut(err, t, fmt.Sprintf("run container1 failed with errors: %v", err)) + idA := stripTrailingCharacters(out) + + cmd = exec.Command(dockerBinary, "run", "-d", "--name", "container2", "busybox", "sleep", "10") + out, _, err = runCommandWithOutput(cmd) + errorOut(err, t, fmt.Sprintf("run container2 failed with errors: %v", err)) + idB := stripTrailingCharacters(out) + + cmd = exec.Command(dockerBinary, "run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1") + out, _, err = runCommandWithOutput(cmd) + fmt.Printf("OUT: %s", out) + errorOut(err, t, fmt.Sprintf("run ping failed with errors: %v", err)) + + cmd = exec.Command(dockerBinary, "kill", idA) + _, err = runCommand(cmd) + errorOut(err, t, fmt.Sprintf("failed to kill container1: %v", err)) + + cmd = exec.Command(dockerBinary, "kill", idB) + _, err = runCommand(cmd) + errorOut(err, t, fmt.Sprintf("failed to kill container2: %v", err)) + + deleteAllContainers() +} diff --git a/pkg/networkfs/etchosts/etchosts.go b/pkg/networkfs/etchosts/etchosts.go index 169797071a..144a039bff 100644 --- a/pkg/networkfs/etchosts/etchosts.go +++ b/pkg/networkfs/etchosts/etchosts.go @@ -15,7 +15,7 @@ var defaultContent = map[string]string{ "ip6-allrouters": "ff02::2", } -func Build(path, IP, hostname, domainname string) error { +func Build(path, IP, hostname, domainname string, extraContent *map[string]string) error { content := bytes.NewBuffer(nil) if IP != "" { if domainname != "" { @@ -30,5 +30,14 @@ func Build(path, IP, hostname, domainname string) error { return err } } + + if extraContent != nil { + for hosts, ip := range *extraContent { + if _, err := content.WriteString(fmt.Sprintf("%s\t%s\n", ip, hosts)); err != nil { + return err + } + } + } + return ioutil.WriteFile(path, content.Bytes(), 0644) } From dc605c8be76760951d0d12e67409602c7b4b7973 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Mon, 5 May 2014 19:51:03 -0700 Subject: [PATCH 3/3] Simplify integration test for link + hostname. Docker-DCO-1.1-Signed-off-by: Solomon Hykes (github: shykes) --- integration-cli/docker_cli_links_test.go | 27 ++++++------------------ 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/integration-cli/docker_cli_links_test.go b/integration-cli/docker_cli_links_test.go index 5b43b3f8a9..a159d1c799 100644 --- a/integration-cli/docker_cli_links_test.go +++ b/integration-cli/docker_cli_links_test.go @@ -18,28 +18,13 @@ func TestPingUnlinkedContainers(t *testing.T) { } func TestPingLinkedContainers(t *testing.T) { - cmd := exec.Command(dockerBinary, "run", "-d", "--name", "container1", "busybox", "sleep", "10") - out, _, err := runCommandWithOutput(cmd) - errorOut(err, t, fmt.Sprintf("run container1 failed with errors: %v", err)) + var out string + out, _, _ = cmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10") idA := stripTrailingCharacters(out) - - cmd = exec.Command(dockerBinary, "run", "-d", "--name", "container2", "busybox", "sleep", "10") - out, _, err = runCommandWithOutput(cmd) - errorOut(err, t, fmt.Sprintf("run container2 failed with errors: %v", err)) + out, _, _ = cmd("run", "-d", "--name", "container2", "busybox", "sleep", "10") idB := stripTrailingCharacters(out) - - cmd = exec.Command(dockerBinary, "run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1") - out, _, err = runCommandWithOutput(cmd) - fmt.Printf("OUT: %s", out) - errorOut(err, t, fmt.Sprintf("run ping failed with errors: %v", err)) - - cmd = exec.Command(dockerBinary, "kill", idA) - _, err = runCommand(cmd) - errorOut(err, t, fmt.Sprintf("failed to kill container1: %v", err)) - - cmd = exec.Command(dockerBinary, "kill", idB) - _, err = runCommand(cmd) - errorOut(err, t, fmt.Sprintf("failed to kill container2: %v", err)) - + cmd("run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1") + cmd("kill", idA) + cmd("kill", idB) deleteAllContainers() }