How to Add Host Entries to Your Docker Containers: A Practical Guide
When working with Docker containers, sometimes you need your containerized apps to resolve specific hostnames to custom IP addresses—maybe for local development or testing purposes. But by default, containers rely on Docker's internal DNS settings and don’t have entries from your host machine’s /etc/hosts
.
This is where adding a host entry to a Docker container comes in handy. In this post, I'll show you how to easily add host
entries into your containers with practical examples and explain why it matters.
Why Add Host Entries to Your Docker Container?
Imagine you're developing a microservices system locally where Service A needs to communicate with Service B by hostname. Maybe you want serviceb.local
inside your container to point at a specific IP address, like your local machine or another service’s container.
Since Docker containers have isolated networking, they don’t automatically know about those names unless:
- You set up custom DNS
- Or you manually add host entries
Adding hosts manually is quick for many common dev workflows — for example:
- Pointing domain names to test servers
- Avoiding DNS issues during development
- Overriding domain lookups without changing global DNS settings
How to Add Host Entries Using docker run
The simplest way is using the --add-host
parameter when running a container.
Syntax:
docker run --add-host <hostname>:<IP_address> <image>
Example:
Say you want your container to resolve mytesthost.local
to IP 192.168.1.100
.
docker run --rm -it --add-host mytesthost.local:192.168.1.100 alpine sh
Inside the container, check /etc/hosts
:
cat /etc/hosts
You should see something like:
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
192.168.1.100 mytesthost.local
Now if you ping the host from inside the container:
ping mytesthost.local
It will ping 192.168.1.100
.
Adding Multiple Hosts
You can specify multiple hosts by repeating --add-host
multiple times:
docker run --rm -it \
--add-host mytesthost.local:192.168.1.100 \
--add-host anotherhost.local:10.0.0.5 \
alpine sh
Again, your /etc/hosts
file inside the container will contain both mappings.
Use Case: Override Host in Docker Compose
If you’re using Docker Compose, you can add host entries under the service’s configuration using the extra_hosts
option.
Example docker-compose.yml
snippet:
version: '3'
services:
app:
image: alpine
command: sh -c "ping -c 4 mydb.local"
extra_hosts:
- "mydb.local:172.20.0.10"
Run it with:
docker-compose up
This will make sure that inside the app container, mydb.local
resolves as expected.
Important Tips & Considerations
- The
--add-host
flag modifies only the container’s internal/etc/hosts
, not your system hosts. - This is great for environments where DNS config changes aren’t possible or too slow.
- When containers shut down and restart, added hosts are lost unless scripted or configured (e.g., via Compose).
- For complex DNS needs across many containers, consider setting up a user-defined network with built-in service name resolution or use tools like Consul or CoreDNS.
Conclusion
Adding host entries inside a Docker container is a straightforward method to control hostname resolution during development and testing phases — especially useful when DNS isn’t easily configurable or when you need quick overrides inside isolated environments.
Remember:
- Use
--add-host
flag ondocker run
- Use
extra_hosts
in Docker Compose files for multi-container apps
Give this trick a try next time you need fine-grained control over networking in your containers!
Happy Dockering! 🚢🐳
If you found this useful or have questions—drop a comment below or share your own tips!