From 6c3fc691e7cd724bcd67195ace415eb9bf6169dd Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 9 Dec 2016 14:25:46 +0100 Subject: [PATCH] Fix fqdn hostnames not added to /etc/hosts This fixes an issue where using a fqdn as hostname not being added to /etc/hosts. The etchosts.Build() function was never called with an IP-address, therefore the fqdn was not added. The subsequent updateHostsFile() was not updated to support fqdn's as hostname, and not adding the record correctly to /etc/hosts. This patch implements the functionality in updateHostsFile() Signed-off-by: Sebastiaan van Stijn --- libnetwork/sandbox_dns_unix.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/libnetwork/sandbox_dns_unix.go b/libnetwork/sandbox_dns_unix.go index f4d1fad605..d40197e53c 100644 --- a/libnetwork/sandbox_dns_unix.go +++ b/libnetwork/sandbox_dns_unix.go @@ -100,8 +100,6 @@ func (sb *sandbox) buildHostsFile() error { } func (sb *sandbox) updateHostsFile(ifaceIP string) error { - var mhost string - if ifaceIP == "" { return nil } @@ -110,11 +108,17 @@ func (sb *sandbox) updateHostsFile(ifaceIP string) error { return nil } + // User might have provided a FQDN in hostname or split it across hostname + // and domainname. We want the FQDN and the bare hostname. + fqdn := sb.config.hostName + mhost := sb.config.hostName if sb.config.domainName != "" { - mhost = fmt.Sprintf("%s.%s %s", sb.config.hostName, sb.config.domainName, - sb.config.hostName) - } else { - mhost = sb.config.hostName + fqdn = fmt.Sprintf("%s.%s", fqdn, sb.config.domainName) + } + + parts := strings.SplitN(fqdn, ".", 2) + if len(parts) == 2 { + mhost = fmt.Sprintf("%s %s", fqdn, parts[0]) } extraContent := []etchosts.Record{{Hosts: mhost, IP: ifaceIP}}