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.

Introduction & Quick Summary
A Go service cannot reach a name that looks correct:
Get "http://orders-api:8080/v1/orders": dial tcp: lookup orders-api on 10.96.0.10:53:
no such hostThe 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-apiis tried asorders-api.<namespace>.svc.cluster.localand 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.
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.
| Message | Meaning |
|---|---|
lookup x ... no such host | NXDOMAIN from the resolver |
lookup x ... server misbehaving | malformed or refused response |
read udp ... i/o timeout | no answer at all, DNS path broken |
dial tcp ... connection refused | resolved, but nothing listens on that port |
certificate is valid for ... | resolution worked, TLS name mismatch |
Key Comparison Table
| Resolver | Selected by | Behaviours | Risk |
|---|---|---|---|
| pure Go | default on Linux, or netgo tag | reads resolv.conf, no NSS, no mDNS | ignores /etc/hosts edge cases and NSS modules |
| cgo | CGO_ENABLED=1 with glibc | full getaddrinfo, NSS, search rules | musl images behave differently |
| forced Go | GODEBUG=netdns=go | deterministic, matching tests | different from other tools in the same pod |
| forced cgo | GODEBUG=netdns=cgo | matches curl and getent | requires cgo build |
| custom | net.Resolver with PreferGo and Dial | own server or timeout | bypasses 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.
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.
kubectl -n payments exec deploy/api -- cat /etc/resolv.conf
# search payments.svc.cluster.local svc.cluster.local cluster.local
# options ndots:5Use the fully qualified name and keep the trailing dot to skip the search list entirely:
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.
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 /out/app /usr/local/bin/appCGO_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.
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-apiA 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.
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.
env | grep -i proxy
# HTTPS_PROXY=http://proxy.internal:3128
# NO_PROXY=localhost,127.0.0.1Add 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 hosterrors 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_apicannot 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
GODEBUGbetween 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.
dnsConfigoptions, node-local DNS caches, andhostNetworkpods each change the resolver path; check inside the pod, not in the cluster defaults.
| Symptom | Cause | Fix |
|---|---|---|
fails only from Go, curl works | resolver implementation differs | align cgo and tags |
| every first request is slow | ndots search expansion | use FQDN or lower ndots |
| works in one namespace only | short name and search domain | qualify the name |
| intermittent failures under load | DNS timeouts | raise 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 documentationThe Go documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Go work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Go guides
All Go guidesGo: x509 certificate signed by unknown authority
Go cannot build a chain to a trusted root: no CA bundle in the image, a private CA, or a missing intermediate. Install roots or set RootCAs.
Go: fatal error: concurrent map writes in goroutines
The runtime catches two writes to one map and stops the process. Guard access with a mutex, use sync.Map for caches, or give one owner.
Go: declared and not used compile error
Go rejects unused local variables and imports on purpose. Delete dead declarations, handle err values and keep imports honest instead of blanking them.
Go: missing go.sum entry for module providing package
Read-only module mode refuses to invent checksums. Run go mod tidy locally, download in CI and keep vendor trees and workspaces consistent.
More in Go
All Go guidesThe guides filed next to this one in Go, in library order.