
Fix getaddrinfo ENOTFOUND in Docker and Node.js with Hosts File (2026)
Docker container or Node.js app throwing getaddrinfo ENOTFOUND or EAI_AGAIN despite /etc/hosts? Fix Docker network isolation and IPv4 DNS precedence.
Table of Contents
- How to fix getaddrinfo ENOTFOUND in Docker and Node.js
- Why your application ignores the local hosts file
- Comparison: ENOTFOUND vs EAI_AGAIN
- 1. Share host domains with Docker Compose
- 2. Force IPv4 resolution order in Node.js
- 3. Check for hidden characters and syntax issues
- 4. Fix Alpine Linux container resolution (musl libc)
The getaddrinfo ENOTFOUND error (or EAI_AGAIN) means the system resolver failed to find an IP address for the requested hostname. This error frequently occurs in Docker containers or Node.js applications when they do not share the host machine's /etc/hosts configuration.
How to fix getaddrinfo ENOTFOUND in Docker and Node.js
To fix getaddrinfo ENOTFOUND, add `extra_hosts` to your `docker-compose.yml` mapping domains to `host-gateway`, configure Node.js to prefer IPv4 with `dns.setDefaultResultOrder('ipv4first')`, and check your /etc/hosts file for non-breaking spaces or invalid formatting.
Why your application ignores the local hosts file
When you edit /etc/hosts on macOS or Windows, those rules apply only to the host operating system.
Common causes of resolution failures:
/etc/hosts. Host entries are not automatically inherited.127.0.0.1, the lookup may fail.See also our guide to Docker hosts file management.
Comparison: ENOTFOUND vs EAI_AGAIN
| Error Code | Meaning | Common Cause | Recommended Action |
|---|---|---|---|
getaddrinfo ENOTFOUND | Hostname not found | Entry missing from container hosts file | Add to extra_hosts or /etc/hosts |
getaddrinfo EAI_AGAIN | Temporary lookup timeout | Unreachable DNS resolver or proxy | Check upstream DNS in /etc/resolv.conf |
ECONNREFUSED | IP resolved but port closed | Target service is not running | Start the server or check port bindings |
1. Share host domains with Docker Compose
To make local domains resolvable inside a Docker container, configure extra_hosts:
version: '3.8'
services:
api:
build: .
extra_hosts:
- "auth.local.test:host-gateway"
- "services.local.test:host-gateway"
environment:
- AUTH_URL=http://auth.local.test:8080On Linux systems where host-gateway is unsupported by older Docker engines, use the default Docker bridge gateway IP (172.17.0.1).
2. Force IPv4 resolution order in Node.js
If your Node.js app runs outside Docker but fails to resolve local domains consistently, force IPv4 precedence:
Add this to the top of your main entry file (index.js or server.js):
import dns from 'node:dns';
// Prioritize IPv4 addresses in getaddrinfo lookups
dns.setDefaultResultOrder('ipv4first');Or pass the runtime flag when launching your application:
node --dns-result-order=ipv4first server.jsRead 0.0.0.0 vs 127.0.0.1 and 127.0.0.1 vs localhost for deeper networking context.
3. Check for hidden characters and syntax issues
A hosts file saved with an invisible BOM or non-breaking spaces (often copied from web snippets) will fail to parse during C getaddrinfo syscalls.
Test resolution using native system utilities:
On macOS:
dscacheutil -q host -a name auth.local.testOn Linux:
getent hosts auth.local.testIf these commands return nothing while the line is visible in /etc/hosts, remove the line and type it manually:
127.0.0.1 auth.local.test
::1 auth.local.test4. Fix Alpine Linux container resolution (musl libc)
Docker images based on Alpine Linux (node:alpine, golang:alpine) use musl libc instead of glibc. Parallel A and AAAA DNS lookups can lead to intermittent EAI_AGAIN timeouts.
Add this directive to your container build:
# In your Dockerfile:
RUN echo "options single-request-reopen" >> /etc/resolv.confFrequently Asked Questions
If your application runs inside a Docker container, that container uses an isolated /etc/hosts file and cannot read the host machine’s hosts file.
ENOTFOUND means the hostname could not be resolved to any IP address (hard failure). EAI_AGAIN indicates a temporary DNS lookup timeout or unreachable nameserver.
Add the extra_hosts mapping in your docker-compose.yml file, mapping your custom domain to host-gateway.
Node.js and Axios follow RFC 6724 DNS sorting and query IPv6 first. If the domain is only mapped to IPv4 in hosts, Node.js may fail on the initial IPv6 lookup.
Related Articles
ERR_CONNECTION_REFUSED on Hosts File Domains: 6 Fixes (2026)
Custom domain in your hosts file returning ERR_CONNECTION_REFUSED? Fix port mismatches, 0.0.0.0 vs 127.0.0.1 binding, Docker mappings and reverse proxies.
Sleezr Team
Developer tools team
Fix EACCES: permission denied on /etc/hosts (2026)
Fix EACCES: permission denied when editing /etc/hosts in Node.js, CLI tools and shell scripts. Learn safe sudo elevation, tee syntax and permissions.
Sleezr Team
Developer tools team
/etc/hosts Not Working? Fixes for Windows, Mac & Linux
The hosts file is ignored or /etc/hosts changes do not take effect? Fix it on Windows, Mac and Linux: flush DNS, check syntax, IPv6, line endings, resolver caches and save permissions.
Sleezr Team
Developer tools team
Fix DNS_PROBE_FINISHED_NXDOMAIN (2026)
Fix DNS_PROBE_FINISHED_NXDOMAIN in Chrome and Edge: flush DNS, check the hosts file, reset DNS servers and clear the browser cache. Step-by-step solutions.
Sleezr Team
Developer tools team
Fix NET::ERR_CERT_COMMON_NAME_INVALID with Hosts File (2026)
Fix NET::ERR_CERT_COMMON_NAME_INVALID when testing local domains in /etc/hosts. Learn mkcert SAN multi-domain certificates, Nginx and HSTS caveats.
Sleezr Team
Developer tools team
Fix ERR_TOO_MANY_REDIRECTS with Hosts File (2026)
Infinite redirect loop (ERR_TOO_MANY_REDIRECTS) after hosts file edits? Fix WordPress siteurl, Nginx X-Forwarded-Proto, www mismatches and 301 caches.
Sleezr Team
Developer tools team