diff --git a/discovery/moby/docker.go b/discovery/moby/docker.go index 34eea7d4c..3a5a11e7b 100644 --- a/discovery/moby/docker.go +++ b/discovery/moby/docker.go @@ -54,6 +54,7 @@ var DefaultDockerSDConfig = DockerSDConfig{ RefreshInterval: model.Duration(60 * time.Second), Port: 80, Filters: []Filter{}, + HostNetworkHost: "localhost", HTTPClientConfig: config.DefaultHTTPClientConfig, } @@ -65,9 +66,10 @@ func init() { type DockerSDConfig struct { HTTPClientConfig config.HTTPClientConfig `yaml:",inline"` - Host string `yaml:"host"` - Port int `yaml:"port"` - Filters []Filter `yaml:"filters"` + Host string `yaml:"host"` + Port int `yaml:"port"` + Filters []Filter `yaml:"filters"` + HostNetworkHost string `yaml:"host_network_host"` RefreshInterval model.Duration `yaml:"refresh_interval"` } @@ -104,9 +106,10 @@ func (c *DockerSDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error type DockerDiscovery struct { *refresh.Discovery - client *client.Client - port int - filters filters.Args + client *client.Client + port int + hostNetworkHost string + filters filters.Args } // NewDockerDiscovery returns a new DockerDiscovery which periodically refreshes its targets. @@ -114,7 +117,8 @@ func NewDockerDiscovery(conf *DockerSDConfig, logger log.Logger) (*DockerDiscove var err error d := &DockerDiscovery{ - port: conf.Port, + port: conf.Port, + hostNetworkHost: conf.HostNetworkHost, } hostURL, err := url.Parse(conf.Host) @@ -245,7 +249,15 @@ func (d *DockerDiscovery) refresh(ctx context.Context) ([]*targetgroup.Group, er labels[model.LabelName(k)] = model.LabelValue(v) } - addr := net.JoinHostPort(n.IPAddress, strconv.FormatUint(uint64(d.port), 10)) + // Containers in host networking mode don't have ports, + // so they only end up here, not in the previous loop. + var addr string + if c.HostConfig.NetworkMode != "host" { + addr = net.JoinHostPort(n.IPAddress, strconv.FormatUint(uint64(d.port), 10)) + } else { + addr = d.hostNetworkHost + } + labels[model.AddressLabel] = model.LabelValue(addr) tg.Targets = append(tg.Targets, labels) }