Resolve container name in extra_hosts option in docker-compose
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
extra_hosts in Docker Compose is often misunderstood. It does not ask Docker to resolve another service name for you; it writes fixed entries into /etc/hosts inside the container. If you want one service to find another service by name, the normal solution is Docker networking and service DNS, not extra_hosts.
What extra_hosts Actually Does
When Compose starts a container, extra_hosts adds static hostname-to-IP mappings. That is equivalent to editing /etc/hosts inside the container.
Inside the app container, resolving legacy-db now returns 10.10.0.15. This is useful for external systems with fixed addresses, on-prem endpoints, or temporary test mappings. It is not a dynamic reference to another Compose service.
Use Service Names for Container-to-Container Resolution
Compose already creates DNS records for service names on the same network. If your goal is "web should reach db", you usually need nothing more than the service name.
From web, the hostname db resolves automatically through Docker's embedded DNS. That is the correct Compose-native approach.
If you need an additional hostname, use a network alias rather than extra_hosts.
Now other containers on the same network can resolve both api and internal-api.
Why Container Names Do Not Belong in extra_hosts
The common question is whether extra_hosts can say "map this hostname to the current IP of container db". In practice, that is the wrong abstraction because container IPs are ephemeral. They can change when you recreate services, scale services, or restart a project.
Static host entries and dynamic container IPs do not mix well. If you manually discover a container IP and place it in extra_hosts, the mapping becomes stale the next time networking changes.
That is why Compose gives you service DNS. It solves exactly this problem without hardcoding internal IP addresses.
Special Case: Reaching the Host Machine
Sometimes the real goal is not another container, but the host machine. In that case extra_hosts can be useful with host-gateway.
This is different from resolving a Compose service. It maps a known host alias to the Docker host gateway in a way Docker understands.
How to Debug Name Resolution
When networking behavior is unclear, inspect the container directly. First, verify the generated hosts file.
Then check DNS resolution for service names:
If db resolves but the custom hostname does not, the problem is probably alias configuration. If only the extra_hosts name resolves, then you are reading a static mapping rather than Docker DNS.
Common Pitfalls
- Using
extra_hoststo point to another Compose service instead of relying on service DNS. - Hardcoding container IP addresses that change after restart or redeploy.
- Confusing
container_namewith a stable network identity across environments. - Forgetting that
extra_hostsedits/etc/hosts, not Docker's DNS server. - Solving host-machine access and container-to-container access with the same pattern even though they are different problems.
Summary
- '
extra_hostscreates static hostname entries inside a container.' - It is useful for fixed external IP mappings, not for dynamic Compose service discovery.
- Compose service names already resolve automatically on the same network.
- Use network aliases when you need an extra internal hostname.
- For host-machine access,
host-gatewayis a validextra_hostsuse case.
Related reading
- Restart container within pod
- Restart Docker Containers when they Crash Automatically
- RHEL8/Fedora - yum/dnf causes cannot download repodata/repomd.xml for docker-ce
- Rights to read /dev/tty0 from pod
- REST API - Benefit of adding extra layer
- REST API - DTOs or not?
- rke kubernetes - node reboot
- Root password inside a Docker container

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.