Browse code

Add alias for hostname if hostname != container name which happens if user manually specify hostname

Signed-off-by: Olli Janatuinen <olli.janatuinen@gmail.com>

Olli Janatuinen authored on 2019/05/11 22:09:42
Showing 2 changed files
... ...
@@ -674,6 +674,18 @@ func (daemon *Daemon) updateNetworkConfig(container *container.Container, n libn
674 674
 		if addShortID {
675 675
 			endpointConfig.Aliases = append(endpointConfig.Aliases, shortID)
676 676
 		}
677
+		if container.Name != container.Config.Hostname {
678
+			addHostname := true
679
+			for _, alias := range endpointConfig.Aliases {
680
+				if alias == container.Config.Hostname {
681
+					addHostname = false
682
+					break
683
+				}
684
+			}
685
+			if addHostname {
686
+				endpointConfig.Aliases = append(endpointConfig.Aliases, container.Config.Hostname)
687
+			}
688
+		}
677 689
 	}
678 690
 
679 691
 	if err := validateNetworkingConfig(n, endpointConfig); err != nil {
... ...
@@ -10,6 +10,7 @@ import (
10 10
 	containertypes "github.com/docker/docker/api/types/container"
11 11
 	"github.com/docker/docker/api/types/versions"
12 12
 	"github.com/docker/docker/integration/internal/container"
13
+	net "github.com/docker/docker/integration/internal/network"
13 14
 	"gotest.tools/assert"
14 15
 	is "gotest.tools/assert/cmp"
15 16
 	"gotest.tools/poll"
... ...
@@ -93,3 +94,37 @@ func TestNISDomainname(t *testing.T) {
93 93
 	assert.Equal(t, 0, res.ExitCode)
94 94
 	assert.Check(t, is.Equal(domainname, strings.TrimSpace(res.Stdout())))
95 95
 }
96
+
97
+func TestHostnameDnsResolution(t *testing.T) {
98
+	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
99
+
100
+	defer setupTest(t)()
101
+	client := testEnv.APIClient()
102
+	ctx := context.Background()
103
+
104
+	const (
105
+		hostname = "foobar"
106
+	)
107
+
108
+	// using user defined network as we want to use internal DNS
109
+	netName := "foobar-net"
110
+	net.CreateNoError(t, context.Background(), client, netName, net.WithDriver("bridge"))
111
+
112
+	cID := container.Run(t, ctx, client, func(c *container.TestContainerConfig) {
113
+		c.Config.Hostname = hostname
114
+		c.HostConfig.NetworkMode = containertypes.NetworkMode(netName)
115
+	})
116
+
117
+	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
118
+
119
+	inspect, err := client.ContainerInspect(ctx, cID)
120
+	assert.NilError(t, err)
121
+	assert.Check(t, is.Equal(hostname, inspect.Config.Hostname))
122
+
123
+	// Clear hosts file so ping will use DNS for hostname resolution
124
+	res, err := container.Exec(ctx, client, cID,
125
+		[]string{"sh", "-c", "echo 127.0.0.1 localhost | tee /etc/hosts && ping -c 1 foobar"})
126
+	assert.NilError(t, err)
127
+	assert.Check(t, is.Equal("", res.Stderr()))
128
+	assert.Equal(t, 0, res.ExitCode)
129
+}