Home › Java › java.net.UnknownHostException: What It Means and How to Fix It (DNS, Docker, Kubernetes)
Intermediate 3 min · November 28, 2026

java.net.UnknownHostException: What It Means and How to Fix It (DNS, Docker, Kubernetes)

UnknownHostException means the host name never resolved to an IP address, so no connection was even attempted.

N
Naren Founder & Principal Engineer

20+ years shipping production Java in banking & fintech. Lessons pulled from things that broke in production.

Follow
✓ Production
production tested
November 28, 2026
last updated
2,141
articles · all by Naren
Before you start⏱ 6 min
  • ✓Basic Java
  • ✓DNS basics
 ● Production Incident 🔎 Debug Guide
⚡Quick Answer
  • The name didn't resolve to an IP; the server was never contacted.
  • Check the spelling first.
  • Kubernetes: a short name only resolves in your own namespace; use service.namespace (or the full .svc.cluster.local name).
  • Docker: use the compose service name and make sure both containers share a network.
  • Test from where the app runs: nslookup / getent hosts inside the pod or container.
  • Java caches lookups (and failures briefly): restart long-running apps after a DNS fix if needed.
✦ Definition~90s read
What is java.net.UnknownHostException?

java.net.UnknownHostException is thrown when Java can't resolve a host name to an IP address. The DNS lookup (or /etc/hosts) returned nothing, so no packet was ever sent to the target.

★
You asked the phone book for a number and it had no entry under that name.
Plain-English First

You asked the phone book for a number and it had no entry under that name. You never got to dial.

A checkout service in a new cluster failed with UnknownHostException: orders-db. The database was running. The app was asking for a short name from the wrong namespace.

Resolution Happens First

Before connecting, Java resolves the host name. If that fails, it throws UnknownHostException and never opens a socket.

Resolve.javaJAVA
1
InetAddress.getByName("orders-db");   // throws UnknownHostException
🎯 Key Takeaway
No IP, no connection.

Kubernetes Namespaces

A short name like orders-db is expanded with your own namespace's search domains. To reach another namespace, include it.

k8s.shBASH
1
2
kubectl exec checkout -- nslookup orders-db        # NXDOMAIN
kubectl exec checkout -- nslookup orders-db.data   # 10.4.2.17
🎯 Key Takeaway
service.namespace.

Java DNS Caching

Successful lookups are cached (networkaddress.cache.ttl, typically 30 s) and failures briefly (networkaddress.cache.negative.ttl, default 10 s).

java.securityPROPERTIES
1
2
networkaddress.cache.ttl=30
networkaddress.cache.negative.ttl=10
🎯 Key Takeaway
Cached failures can outlive a DNS fix.
● Production incidentPOST-MORTEMseverity: high

The Name That Only Existed Next Door

Symptom
java.net.UnknownHostException: orders-db at startup.
Assumption
The team assumed the database service was missing.
Root cause
Short service names resolve only in the caller's own namespace; orders-db lived in data.
Fix
Changed the URL to orders-db.data.
Key lesson
  • Resolve from where the app runs.
  • Use service.namespace across namespaces.
  • No packet reached the server.
Production debug guideRun them from the app's side3 entries
Symptom · 01
Fails in Kubernetes only
→
Fix
kubectl exec <pod> -- nslookup <name>; try name.namespace.
Symptom · 02
Fails in Docker only
→
Fix
docker network inspect: both containers on the same network?
Symptom · 03
Intermittent
→
Fix
Check the resolver and Java's DNS cache TTLs.
⚙ Quick Reference
3 commands from this guide
FileCommand / CodePurpose
Resolve.javaInetAddress.getByName("orders-db"); // throws UnknownHostExceptionResolution Happens First
k8s.shkubectl exec checkout -- nslookup orders-db # NXDOMAINKubernetes Namespaces
java.securitynetworkaddress.cache.ttl=30Java DNS Caching

Key takeaways

1
Name didn't resolve.
2
Test from the app's side.
3
Use service.namespace.
4
Watch Java's DNS cache.

Common mistakes to avoid

2 patterns
×

Testing from your laptop

Symptom
Resolves fine there.
Fix
Test inside the container.
×

Short names across namespaces

Symptom
NXDOMAIN.
Fix
service.namespace.
INTERVIEW PREP · PRACTICE MODE

Interview Questions on This Topic

Q01JUNIOR
What does UnknownHostException mean?
Q01 of 01JUNIOR

What does UnknownHostException mean?

ANSWER
DNS resolution of the host name failed; no connection was attempted.
FAQ · 2 QUESTIONS

Frequently Asked Questions

01
How do I fix java.net.UnknownHostException?
02
Why does it work on my machine but not in Docker?
N
Naren Founder & Principal Engineer

20+ years shipping production Java in banking & fintech. Lessons pulled from things that broke in production.

Follow
✓ Verified
production tested
November 28, 2026
last updated
2,141
articles · all by Naren
🔥

That's Exceptions. Mark it forged?

3 min read · try the examples if you haven't

←
Previous
java.lang.OutOfMemoryError: Java Heap Space
13 / 14 · Exceptions
Next
java.net.BindException: Address Already in Use
→