Skip to content

Commit fbdd423

Browse files
committed
fix NXDOMAIN error in Docker Compose environments
Add fallback to /etc/hosts lookup when DNS resolution fails in hackney_happy.erl. This fixes issue #764 where hostnames resolved via /etc/hosts (common in Docker Compose) would fail with NXDOMAIN when using inet_res:getbyname alone. The hybrid approach tries DNS first (faster), then falls back to inet:gethostbyname which checks /etc/hosts when DNS fails.
1 parent e044cd5 commit fbdd423

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/hackney_happy.erl

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,34 @@ getaddrs(Name) ->
124124
{IP6Addrs, IP4Addrs}.
125125

126126
getbyname(Hostname, Type) ->
127+
%% First try DNS resolution using inet_res:getbyname
127128
case (catch inet_res:getbyname(Hostname, Type)) of
128-
{'ok', #hostent{h_addr_list=AddrList}} -> lists:usort(AddrList);
129-
{error, _Reason} -> [];
129+
{'ok', #hostent{h_addr_list=AddrList}} ->
130+
lists:usort(AddrList);
131+
{error, _Reason} ->
132+
%% DNS failed, try fallback to /etc/hosts using inet:gethostbyname
133+
%% This fixes NXDOMAIN errors in Docker Compose environments where
134+
%% hostnames are resolved via /etc/hosts entries
135+
fallback_hosts_lookup(Hostname, Type);
130136
Else ->
131-
%% ERLANG 22 has an issue when g matching somee DNS server messages
137+
%% ERLANG 22 has an issue when g matching some DNS server messages
132138
?report_debug("DNS error", [{hostname, Hostname}
133139
,{type, Type}
134140
,{error, Else}]),
141+
%% Try fallback on unexpected errors too
142+
fallback_hosts_lookup(Hostname, Type)
143+
end.
144+
145+
%% Fallback to check /etc/hosts when DNS resolution fails
146+
fallback_hosts_lookup(Hostname, Type) ->
147+
InetType = case Type of
148+
a -> inet;
149+
aaaa -> inet6
150+
end,
151+
case (catch inet:gethostbyname(Hostname, InetType)) of
152+
{'ok', #hostent{h_addr_list=AddrList}} ->
153+
lists:usort(AddrList);
154+
_ ->
135155
[]
136156
end.
137157

0 commit comments

Comments
 (0)