1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

add linked containers to hosts file

Docker-DCO-1.1-Signed-off-by: Bryan Murphy <bmurphy1976@gmail.com> (github: bmurphy1976)

Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)
Tested-by: Solomon Hykes <solomon@docker.com> (github: shykes)
This commit is contained in:
Bryan Murphy 2014-04-07 18:34:07 +00:00 committed by Solomon Hykes
parent cd81895091
commit 53f38a14cd
5 changed files with 106 additions and 3 deletions

View file

@ -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 {

View file

@ -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].

View file

@ -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:/#

View file

@ -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()
}

View file

@ -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)
}