Browse code

Merge pull request #10818 from estesp/link-add-aliases

Steve Francia authored on 2015/02/20 07:22:53
Showing 3 changed files
... ...
@@ -458,7 +458,13 @@ func (container *Container) buildHostsFiles(IP string) error {
458 458
 
459 459
 	for linkAlias, child := range children {
460 460
 		_, alias := path.Split(linkAlias)
461
-		extraContent = append(extraContent, etchosts.Record{Hosts: alias, IP: child.NetworkSettings.IPAddress})
461
+		// allow access to the linked container via the alias, real name, and container hostname
462
+		aliasList := alias + " " + child.Config.Hostname
463
+		// only add the name if alias isn't equal to the name
464
+		if alias != child.Name[1:] {
465
+			aliasList = aliasList + " " + child.Name[1:]
466
+		}
467
+		extraContent = append(extraContent, etchosts.Record{Hosts: aliasList, IP: child.NetworkSettings.IPAddress})
462 468
 	}
463 469
 
464 470
 	for _, extraHost := range container.hostConfig.ExtraHosts {
... ...
@@ -252,20 +252,23 @@ In addition to the environment variables, Docker adds a host entry for the
252 252
 source container to the `/etc/hosts` file. Here's an entry for the `web`
253 253
 container:
254 254
 
255
-    $ sudo docker run -t -i --rm --link db:db training/webapp /bin/bash
255
+    $ sudo docker run -t -i --rm --link db:webdb training/webapp /bin/bash
256 256
     root@aed84ee21bde:/opt/webapp# cat /etc/hosts
257 257
     172.17.0.7  aed84ee21bde
258 258
     . . .
259
-    172.17.0.5  db
259
+    172.17.0.5  webdb 6e5cdeb2d300 db
260 260
 
261 261
 You can see two relevant host entries. The first is an entry for the `web`
262 262
 container that uses the Container ID as a host name. The second entry uses the
263
-link alias to reference the IP address of the `db` container. You can ping
264
-that host now via this host name.
263
+link alias to reference the IP address of the `db` container. In addition to 
264
+the alias you provide, the linked container's name--if unique from the alias
265
+provided to the `--link` parameter--and the linked container's hostname will
266
+also be added in `/etc/hosts` for the linked container's IP address. You can ping
267
+that host now via any of these entries:
265 268
 
266 269
     root@aed84ee21bde:/opt/webapp# apt-get install -yqq inetutils-ping
267
-    root@aed84ee21bde:/opt/webapp# ping db
268
-    PING db (172.17.0.5): 48 data bytes
270
+    root@aed84ee21bde:/opt/webapp# ping webdb
271
+    PING webdb (172.17.0.5): 48 data bytes
269 272
     56 bytes from 172.17.0.5: icmp_seq=0 ttl=64 time=0.267 ms
270 273
     56 bytes from 172.17.0.5: icmp_seq=1 ttl=64 time=0.250 ms
271 274
     56 bytes from 172.17.0.5: icmp_seq=2 ttl=64 time=0.256 ms
... ...
@@ -83,16 +83,27 @@ func TestLinksInvalidContainerTarget(t *testing.T) {
83 83
 }
84 84
 
85 85
 func TestLinksPingLinkedContainers(t *testing.T) {
86
-	var out string
87
-	out, _, _ = dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
88
-	idA := stripTrailingCharacters(out)
89
-	out, _, _ = dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
90
-	idB := stripTrailingCharacters(out)
91
-	dockerCmd(t, "run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
92
-	dockerCmd(t, "kill", idA)
93
-	dockerCmd(t, "kill", idB)
94
-	deleteAllContainers()
86
+	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "container1", "--hostname", "fred", "busybox", "top")
87
+	if _, err := runCommand(runCmd); err != nil {
88
+		t.Fatal(err)
89
+	}
90
+	runCmd = exec.Command(dockerBinary, "run", "-d", "--name", "container2", "--hostname", "wilma", "busybox", "top")
91
+	if _, err := runCommand(runCmd); err != nil {
92
+		t.Fatal(err)
93
+	}
95 94
 
95
+	runArgs := []string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sh", "-c"}
96
+	pingCmd := "ping -c 1 %s -W 1 && ping -c 1 %s -W 1"
97
+
98
+	// test ping by alias, ping by name, and ping by hostname
99
+	// 1. Ping by alias
100
+	dockerCmd(t, append(runArgs, fmt.Sprintf(pingCmd, "alias1", "alias2"))...)
101
+	// 2. Ping by container name
102
+	dockerCmd(t, append(runArgs, fmt.Sprintf(pingCmd, "container1", "container2"))...)
103
+	// 3. Ping by hostname
104
+	dockerCmd(t, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
105
+
106
+	deleteAllContainers()
96 107
 	logDone("links - ping linked container")
97 108
 }
98 109