Using --add-host or extra_hosts with docker-compose
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Using --add-host or extra_hosts with Docker Compose is an essential technique for managing and customizing network settings within Docker containers. This feature allows developers to map hostnames to IP addresses, providing more control over domain name resolution.
Understanding Docker Networking
Docker containers run in isolated environments, meaning they have their own networking stack. By default, Docker creates a bridge network for containers, enabling them to communicate with each other. Containers can resolve each other's IP addresses and automatically join this shared network. However, there are times when you may need to customize this configuration, especially when dealing with services that must resolve a hostname to a specific IP address.
What are --add-host and extra_hosts?
The --add-host flag is used with the docker run command to add a single hostname-to-IP mapping in the container's /etc/hosts file. This is useful for forcing connections to specific IP addresses.
Basic Example with docker run
In the example above, the docker run command starts a new container from the nginx image and modifies the /etc/hosts file within the container to resolve example.com to 127.0.0.1.
Using extra_hosts with Docker Compose
When working with Docker Compose, the equivalent feature is extra_hosts. This YAML configuration key allows you to define multiple host-IP mappings easily. This is particularly beneficial in a complex microservice architecture where multiple containers might need to resolve custom hostnames.
Example docker-compose.yml
In this example, the webapp service container's /etc/hosts file will include entries that map db.example.com and api.example.com to their respective IP addresses (172.20.0.2 and 172.20.0.3).
Key Benefits of extra_hosts
- Predictability: Overrides DNS settings for services that need consistent access to IPs.
- Testing: Simulates production environments by allowing different hostnames to resolve to specific IPs during development.
- Ease of Use: Managed within a straightforward YAML format, allowing greater ease in tracking and updating configurations compared to the command-line approach.
Considerations for Production Environments
While extra_hosts and --add-host are useful tools, they must be used judiciously in production:
- Avoid Hardcoding: Don't hard-code IP addresses unless necessary. Use service discovery tools when dynamic IPs are required.
- Security: Pay attention to security implications when manipulating network settings. Ensure that unauthorized access isn't facilitated by these mappings.
- Maintenance: Host-IP mappings need frequent updates in dynamic environments.
Table: Quick Reference for Host Mapping Features
| Feature | Command/Directive | Use Case | Limitation |
--add-host | docker run | Single container usage | Limited to CLI, not scalable |
extra_hosts | docker-compose | Multi-container orchestration | Requires maintaining compose file |
| Host Resolution | Global DNS | Dynamic and large-scale setups | Can be complex and needs discovery tool |
Conclusion
Using --add-host or extra_hosts in Docker and Docker Compose offers a robust way to handle hostname resolution within containers, aiding in service connectivity. While it's a neat solution in certain scenarios, it's crucial to keep best practices and environmental needs in mind when implementing these mappings. Container orchestration tools like Kubernetes offer advanced networking solutions, but for many, extra_hosts provides a simple and effective method to control network behavior.

