Sleezr
Download
Fix getaddrinfo ENOTFOUND in Docker and Node.js with Hosts File (2026)

Fix getaddrinfo ENOTFOUND in Docker and Node.js with Hosts File (2026)

S
Sleezr Team
··3 min read

Docker container or Node.js app throwing getaddrinfo ENOTFOUND or EAI_AGAIN despite /etc/hosts? Fix Docker network isolation and IPv4 DNS precedence.

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:

1
Docker container network isolation: A Docker container has its own network namespace and virtual /etc/hosts. Host entries are not automatically inherited.
2
Node.js IPv6 precedence (Node 17+): Per RFC 6724, Node.js queries IPv6 addresses first. If your hosts file only declares 127.0.0.1, the lookup may fail.
3
DNS over HTTPS (DoH) clients: Certain HTTP client libraries bypass standard OS resolver calls and query public DNS resolvers directly.

See also our guide to Docker hosts file management.

Comparison: ENOTFOUND vs EAI_AGAIN

Error CodeMeaningCommon CauseRecommended Action
getaddrinfo ENOTFOUNDHostname not foundEntry missing from container hosts fileAdd to extra_hosts or /etc/hosts
getaddrinfo EAI_AGAINTemporary lookup timeoutUnreachable DNS resolver or proxyCheck upstream DNS in /etc/resolv.conf
ECONNREFUSEDIP resolved but port closedTarget service is not runningStart 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:

YAML
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:8080

On 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):

JAVASCRIPT
import dns from 'node:dns';

// Prioritize IPv4 addresses in getaddrinfo lookups
dns.setDefaultResultOrder('ipv4first');

Or pass the runtime flag when launching your application:

BASH
node --dns-result-order=ipv4first server.js

Read 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:

BASH
dscacheutil -q host -a name auth.local.test

On Linux:

BASH
getent hosts auth.local.test

If these commands return nothing while the line is visible in /etc/hosts, remove the line and type it manually:

TEXT
127.0.0.1  auth.local.test
::1        auth.local.test

4. 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:

DOCKERFILE
# In your Dockerfile:
RUN echo "options single-request-reopen" >> /etc/resolv.conf
Also readManage hosts file with Docker on Mac
Also readTroubleshoot hosts file not working
Share this article

Frequently 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

7 min read
hosts fileERR_CONNECTION_REFUSEDDocker

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.

S

Sleezr Team

Developer tools team

3 min read
Node.jshosts filepermissions

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.

S

Sleezr Team

Developer tools team

4 min read
hosts filetroubleshootingDNS

/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.

S

Sleezr Team

Developer tools team

3 min read
DNSChrometroubleshooting

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.

S

Sleezr Team

Developer tools team

3 min read
WordPressERR_TOO_MANY_REDIRECTShosts file

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.

S

Sleezr Team

Developer tools team