From a11e4ec136eb1f3c3e04c91e7f527d1da4d2484c Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Fri, 1 Nov 2019 18:56:32 +0000 Subject: [PATCH 1/2] probe: avoid reporting the container ID as its 'hostname' If hostname isn't set on a container it defaults to a random hex number which we don't want to show in the UI. --- probe/docker/container.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/probe/docker/container.go b/probe/docker/container.go index da27ac6d44..3e976ba6f9 100644 --- a/probe/docker/container.go +++ b/probe/docker/container.go @@ -108,6 +108,11 @@ func (c *container) PID() int { func (c *container) Hostname() string { if c.container.Config.Domainname == "" { + // If hostname isn't set on a container it defaults to a random hex + // number which we don't want to show in the UI. + if strings.HasPrefix(c.container.ID, c.container.Config.Hostname) { + return "" + } return c.container.Config.Hostname } @@ -375,13 +380,16 @@ func (c *container) getSanitizedCommand() string { } func (c *container) getBaseNode() report.Node { - result := report.MakeNodeWith(report.MakeContainerNodeID(c.ID()), map[string]string{ - ContainerID: c.ID(), - ContainerCreated: c.container.Created.Format(time.RFC3339Nano), - ContainerCommand: c.getSanitizedCommand(), - ImageID: c.Image(), - ContainerHostname: c.Hostname(), - }).WithParent(report.ContainerImage, report.MakeContainerImageNodeID(c.Image())) + properties := map[string]string{ + ContainerID: c.ID(), + ContainerCreated: c.container.Created.Format(time.RFC3339Nano), + ContainerCommand: c.getSanitizedCommand(), + ImageID: c.Image(), + } + if hostname := c.Hostname(); hostname != "" { + properties[ContainerHostname] = hostname + } + result := report.MakeNodeWith(report.MakeContainerNodeID(c.ID()), properties).WithParent(report.ContainerImage, report.MakeContainerImageNodeID(c.Image())) result = result.AddPrefixPropertyList(LabelPrefix, c.container.Config.Labels) if !c.noEnvironmentVariables { result = result.AddPrefixPropertyList(EnvPrefix, c.env()) From d8f73862501c8d08af1a7dc5dea78f2ac635300e Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 10 Mar 2020 20:12:34 +0000 Subject: [PATCH 2/2] Improve comment --- probe/docker/container.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/probe/docker/container.go b/probe/docker/container.go index 3e976ba6f9..6622fe0c47 100644 --- a/probe/docker/container.go +++ b/probe/docker/container.go @@ -108,7 +108,8 @@ func (c *container) PID() int { func (c *container) Hostname() string { if c.container.Config.Domainname == "" { - // If hostname isn't set on a container it defaults to a random hex + // If hostname isn't set on a container Docker sets it to the + // first 12 characters of the container ID, a random hex // number which we don't want to show in the UI. if strings.HasPrefix(c.container.ID, c.container.Config.Hostname) { return ""