Hi i have problems resolving localhost in a docker container

[devcontainer]$ cat /etc/hosts 127.0.0.1 localhost [devcontainer]$ curl localhost curl: (6) Could not resolve host: localhost 

also node has the same problem

[devcontainer]$ /home/plutus/.vscode-server/bin/e713fe9b05fc24facbec8f34fb1017133858842b/node Welcome to Node.js v14.16.0. Type ".help" for more information. > dns.lookup('localhost', {hints: dns.ADDRCONFIG|dns.V4MAPPED}, console.log) GetAddrInfoReqWrap { callback: [Function: log], family: 0, hostname: 'localhost', oncomplete: [Function: onlookup] } > Error: getaddrinfo ENOTFOUND localhost at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26) at GetAddrInfoReqWrap.callbackTrampoline (internal/async_hooks.js:131:14) { errno: -3007, code: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'localhost' } 

any idea what the problem could be despite /etc/hosts? It definitly contains localhost.

And yes normal hosts are working:

[devcontainer]$ curl google.de <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> 
4

1 Answer

The standard hostname lookup functions on Linux use /etc/nsswitch.conf to determine where they should look for information. Make sure that file exists and is world-readable.

The nsswitch.conf file specifies which modules to use for each database. For example, there is a standard 'files' module that reads /etc/hosts (and /etc/passwd); there is a 'dns' module which makes DNS queries; and it's possible to install modules for LDAP, NIS, MySQL, etc.

Make sure nsswitch.conf contains a hosts: line, and that the line mentions the files module somewhere in it (modules are processed left-to-right).

For example, typically it will be configured sorta like this (modern distros might include more modules, but that's the basic minimum that everyone has):

passwd: files hosts: files dns ... 

This configuration tells the "hosts" lookup functions to first use the 'files' module (which reads /etc/hosts), and then if it has no succesful results, then use the 'dns' module (which makes DNS queries according to /etc/resolv.conf).

Each module exists as a library in /lib (or /usr/lib). So if you want to use the files module, then make sure that your system actually has /lib/libnss_files.so.2 installed.


Note that if the nsswitch.conf file is missing or unreadable, Glibc uses this hardcoded default configuration:

passwd: files hosts: dns [!UNAVAIL=return] files ... 

This means that DNS will be tried first, and if it provides any answer (even a "not found"), the query will immediately stop there – the "files" module (to read /etc/hosts) won't be called at all.


You can test the name service configuration using getent:

  • To query according to nsswitch.conf:
    getent hosts localhost
    getent passwd root

  • To query a specific module (bypassing nsswitch.conf):
    getent -s dns hosts example.com
    getent -s files hosts localhost

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy