micro/go-micro · error

could not determine host: %v

Error message

could not determine host: %v

What it means

When hostName is empty, NewMDNSService falls back to os.Hostname() to auto-detect the machine's hostname. If that OS call fails, this error wraps the underlying reason. This is rare and usually indicates a broken system environment rather than a library problem.

Source

Thrown at internal/util/mdns/zone.go:89

	}
	if port == 0 {
		return nil, fmt.Errorf("missing service port")
	}

	// Set default domain
	if domain == "" {
		domain = "local."
	}
	if err := validateFQDN(domain); err != nil {
		return nil, fmt.Errorf("domain %q is not a fully-qualified domain name: %v", domain, err)
	}

	// Get host information if no host is specified.
	if hostName == "" {
		var err error
		hostName, err = os.Hostname()
		if err != nil {
			return nil, fmt.Errorf("could not determine host: %v", err)
		}
		hostName = fmt.Sprintf("%s.", hostName)
	}
	if err := validateFQDN(hostName); err != nil {
		return nil, fmt.Errorf("hostName %q is not a fully-qualified domain name: %v", hostName, err)
	}

	if len(ips) == 0 {
		var err error
		ips, err = net.LookupIP(trimDot(hostName))
		if err != nil {
			// Try appending the host domain suffix and lookup again
			// (required for Linux-based hosts)
			tmpHostName := fmt.Sprintf("%s%s", hostName, domain)

			ips, err = net.LookupIP(trimDot(tmpHostName))

			if err != nil {

View on GitHub (pinned to 24529f1404)

Solutions

  1. Pass an explicit hostName ending in '.' so os.Hostname() is never called, e.g. "myhost.local."
  2. Check the wrapped %v cause; fix the container/OS hostname configuration (set --hostname / UTS settings)
  3. Pre-compute os.Hostname() yourself and handle the failure with a fallback name before registering

Example fix

// before
svc, err := NewMDNSService("myapp", "_http._tcp", "local.", "", 8080, ips, nil) // relies on os.Hostname()
// after
host, err := os.Hostname()
if err != nil {
	host = "localhost"
}
svc, err2 := NewMDNSService("myapp", "_http._tcp", "local.", host+".", 8080, ips, nil)
Defensive patterns

Strategy: fallback

Validate before calling

host, hostErr := os.Hostname()
if hostErr != nil {
	host = "localhost" // or read from config
}
hostName := host + "."

Type guard

func canAutoDetectHost() bool { _, err := os.Hostname(); return err == nil }

Try / catch

svc, err := NewMDNSService(instance, service, domain, hostName, port, ips, txt)
if err != nil {
	if strings.Contains(err.Error(), "could not determine host") {
		return fmt.Errorf("pass an explicit hostName; os.Hostname() is unavailable in this environment: %w", err)
	}
	return err
}

Prevention

When it happens

Trigger: Calling NewMDNSService(instance, service, domain, "", port, ips, txt) in an environment where os.Hostname() returns an error — e.g. restricted sandbox/container with no UTS namespace access, or a stripped-down runtime lacking /proc/sys/kernel/hostname.

Common situations: Minimal Docker/CI containers with unusual security profiles; embedded systems with misconfigured kernels; test sandboxes that stub out hostname lookup.

Related errors


AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01). Data as JSON: /api/errors/a0ae5c8a0aeb8217. Report an issue: GitHub.