getent hosts: Usage, Options & Common CI Errors
getent hosts resolves a name the way the application actually will - through NSS.
getent hosts is the most accurate DNS check in a container because it goes through the same Name Service Switch path (/etc/hosts, then DNS) that your app uses - unlike dig, which talks straight to a DNS server and can disagree.
What it does
getent queries the Name Service Switch databases configured in /etc/nsswitch.conf. getent hosts <name> resolves a hostname through /etc/hosts and DNS exactly as a normal program would, making it the truest "will my app resolve this?" check.
Common usage
getent hosts example.com # resolve via NSS (hosts file + DNS)
getent hosts db # resolve a compose/k8s service name
getent ahosts example.com # show all addresses (v4 + v6)
getent hosts example.com || echo "DNS not ready"
getent hosts 93.184.216.34 # reverse lookupOptions
| Database | What it does |
|---|---|
| hosts <name> | Resolve via /etc/hosts then DNS (NSS order) |
| ahosts <name> | All addresses, IPv4 and IPv6 |
| ahostsv4 / ahostsv6 | Restrict to one address family |
| hosts <ip> | Reverse lookup (PTR via NSS) |
Common errors in CI
getent hosts exits non-zero and prints nothing when the name does not resolve - that makes it a clean gate: getent hosts db >/dev/null || wait. The key insight: dig may succeed while getent fails (or vice-versa) because dig bypasses /etc/hosts and nsswitch - in a Docker Compose or Kubernetes network, service names resolve via the embedded DNS that getent honors, so getent hosts is the right probe. Note getent is glibc; on musl/Alpine it may be absent, so use nslookup/wget there.