Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Go: dial tcp lookup no such host in containers

dial tcp lookup no such host is name resolution failing, not TCP. Check resolv.conf, musl versus glibc images, search domains, and the actual lookup.

~5 min readGo
#Go#Networking#DNS#Docker
A Go client crate failing at a cyan DNS gate, an amber resolver card and a dead name lookup path beside it

Introduction & Quick Summary

A Go service cannot reach a name that looks correct:

code
Get "http://orders-api:8080/v1/orders": dial tcp: lookup orders-api on 10.96.0.10:53: no such host

The error comes from the resolver, not from TCP. Go asked the configured DNS server at 10.96.0.10:53 for orders-api, and the answer was NXDOMAIN. No connection was attempted, so retries, timeouts, and TLS settings are all irrelevant until the name resolves.

The usual reasons:

  • The name does not exist in the environment: different namespace, missing Service, or a typo in an environment variable.
  • The search domain list does not produce the intended fully qualified name, so orders-api is tried as orders-api.<namespace>.svc.cluster.local and then as a public name.
  • The image is Alpine with cgo enabled, so the musl resolver behaves differently from glibc and from Go's own resolver.
  • CoreDNS or the container's DNS path is unhealthy, which normally shows a timeout instead.

Quick answer: reproduce the lookup with the same binary and the same environment, then decide whether the name is wrong or the resolver is. GODEBUG=netdns=go+2 prints which resolver Go chose and every query it made.

Core Mechanics, Signatures & Reproducible Setup

net.Dialer resolves through a *net.Resolver. On Linux the resolver reads /etc/resolv.conf and applies search and ndots in order. Go has two implementations: a pure Go resolver that parses the file itself, and a cgo resolver that calls getaddrinfo. The choice depends on build tags, CGO_ENABLED, and the contents of resolv.conf.

go
package main import ( "context" "fmt" "net" "time" ) func main() { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() addrs, err := net.DefaultResolver.LookupHost(ctx, "orders-api") if err != nil { var dnsErr *net.DNSError if errors.As(err, &dnsErr) { fmt.Printf("name=%s server=%s isNotFound=%v temporary=%v\n", dnsErr.Name, dnsErr.Server, dnsErr.IsNotFound, dnsErr.IsTemporary) } fmt.Println("lookup failed:", err) return } fmt.Println(addrs) }

Build the same way as production, run it inside the pod, and compare with getent hosts orders-api. Differences between the two point at resolver implementation rather than at DNS data.

MessageMeaning
lookup x ... no such hostNXDOMAIN from the resolver
lookup x ... server misbehavingmalformed or refused response
read udp ... i/o timeoutno answer at all, DNS path broken
dial tcp ... connection refusedresolved, but nothing listens on that port
certificate is valid for ...resolution worked, TLS name mismatch

Key Comparison Table

ResolverSelected byBehavioursRisk
pure Godefault on Linux, or netgo tagreads resolv.conf, no NSS, no mDNSignores /etc/hosts edge cases and NSS modules
cgoCGO_ENABLED=1 with glibcfull getaddrinfo, NSS, search rulesmusl images behave differently
forced GoGODEBUG=netdns=godeterministic, matching testsdifferent from other tools in the same pod
forced cgoGODEBUG=netdns=cgomatches curl and getentrequires cgo build
customnet.Resolver with PreferGo and Dialown server or timeoutbypasses platform settings

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. Print the queries Go actually makes

GODEBUG=netdns=go+2 writes resolver decisions to stderr, including the search list expansion. This is the fastest way to see that orders-api was never tried as orders-api.payments.svc.cluster.local.

bash
GODEBUG=netdns=go+2 ./app 2>&1 | grep -E 'go package net|lookup|search'

If the output shows only orders-api. and no search domain expansions, resolv.conf has an empty or unusable search line, and a short name cannot work.

2. Kubernetes service naming and ndots

Inside a pod, /etc/resolv.conf normally contains search <ns>.svc.cluster.local svc.cluster.local cluster.local and options ndots:5. A name with fewer than five dots is tried with each search domain before the absolute name, which explains both the failure and the latency.

bash
kubectl -n payments exec deploy/api -- cat /etc/resolv.conf # search payments.svc.cluster.local svc.cluster.local cluster.local # options ndots:5

Use the fully qualified name and keep the trailing dot to skip the search list entirely:

go
base := "http://orders-api.payments.svc.cluster.local.:8080"

That form is longer to read, and it removes a class of failures in clusters with custom dnsConfig. When short names are preferred, reduce ndots in the pod spec so two-label names resolve directly.

3. Alpine, musl, and cgo

golang:alpine builds with cgo enabled by default when a C toolchain is present, and the resulting binary uses musl's resolver. musl handles search and ndots differently from glibc, and a name that works in a Debian image can fail in Alpine with the same binary inputs.

dockerfile
FROM golang:1.25-alpine AS build ENV CGO_ENABLED=0 RUN go build -tags netgo -trimpath -o /out/app ./cmd/app FROM alpine:3.21 RUN apk add --no-cache ca-certificates COPY --from=build /out/app /usr/local/bin/app

CGO_ENABLED=0 selects the pure Go resolver, which is statically linked and consistent across images. The trade-off is that NSS-based resolution and some /etc/nsswitch.conf behaviour no longer apply, which matters mostly outside Kubernetes.

4. Verify DNS before blaming the application

A resolver check from a debug pod separates cluster DNS from application configuration. Run the same query from the same namespace, because that is where the search domain applies.

bash
kubectl -n payments run dnstest --rm -it --image=busybox:1.36 --restart=Never -- \ nslookup orders-api kubectl -n payments get svc orders-api -o wide kubectl -n payments get endpoints orders-api

A Service with no endpoints resolves fine and then fails at connect time, which is a different error. no such host with an existing Service means the name or namespace is wrong, not the workload.

5. Timeouts, retries, and error classification

Go does not cache DNS answers, so every new connection may pay the resolver cost. A dialer with an explicit timeout and a clear error type makes incidents shorter.

go
dialer := &net.Dialer{ Timeout: 2 * time.Second, KeepAlive: 30 * time.Second, Resolver: &net.Resolver{ PreferGo: true, Dial: func(ctx context.Context, network, address string) (net.Conn, error) { return (&net.Dialer{Timeout: time.Second}).DialContext(ctx, "udp", "10.96.0.10:53") }, }, } client := &http.Client{Transport: &http.Transport{DialContext: dialer.DialContext}}

Classify with errors.As against *net.DNSError and retry only when the failure is temporary. Retrying a genuine NXDOMAIN delays the real alert and multiplies query load on CoreDNS.

6. Proxies and NO_PROXY

An HTTP proxy configured through the environment can turn a working DNS setup into no such host, because the proxy resolves the name instead of the client, or because NO_PROXY omits the internal suffix so traffic leaves the cluster.

bash
env | grep -i proxy # HTTPS_PROXY=http://proxy.internal:3128 # NO_PROXY=localhost,127.0.0.1

Add the cluster suffix to NO_PROXY (.svc.cluster.local) or remove the proxy variables from the workload. This one is easy to miss because getent hosts succeeds while the application fails.

Common Compilation/Runtime Mistakes & Version Gotchas

  • Assuming Go caches DNS. It does not, which is why a CoreDNS hiccup appears as scattered no such host errors rather than a sustained outage.
  • Using underscores in a hostname. Underscores are legal in Kubernetes object names but not in hostnames; a Service named orders_api cannot be resolved as a hostname reliably.
  • Long names with ndots:5. Each search domain appends to the name, and queries can exceed the 253-character limit, producing NXDOMAIN instead of a clear error.
  • Mixing GODEBUG between environments. A binary that resolves with cgo locally and pure Go in the container will behave differently on edge cases like trailing dots.
  • Resolv.conf rotation. dnsConfig options, node-local DNS caches, and hostNetwork pods each change the resolver path; check inside the pod, not in the cluster defaults.
SymptomCauseFix
fails only from Go, curl worksresolver implementation differsalign cgo and tags
every first request is slowndots search expansionuse FQDN or lower ndots
works in one namespace onlyshort name and search domainqualify the name
intermittent failures under loadDNS timeoutsraise timeout and add a cache

Test the same name the cluster uses with the curl request builder to see whether the failure is DNS or HTTP, and paste the pod's environment into the env file parser when proxy variables are suspected of rewriting the target.

The resolver surface is documented in the net package, and the configuration precedence for the pure Go resolver, including search and ndots handling, is described in net.Resolver.

Official references

primary documentation

The Go documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Go work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Go guides

More in Go

All Go guides

The guides filed next to this one in Go, in library order.