<p>If you want a DNS lookup of one domain to resolve to another one, you have 2 options.</p>

<ol>
<li>Modify the Container /etc/hosts file. By default any DNS lookup is done there. You schould do that in the Dockerfile. </li>
</ol>

<p><a href="https://stackoverflow.com/questions/38302867/how-to-update-etc-hosts-file-in-docker-image-during-docker-build">Modify /etc/hosts in a Dockerfile</a></p>

<ol start="2">
<li>Maintain your own DNS servers with all mapping records. On container runtime just pass the DNS server.</li>
</ol>

<p><a href="https://docs.docker.com/config/containers/container-networking/" rel="nofollow noreferrer">Docker docs</a></p>

<blockquote>
  <p>--dns   The IP address of a DNS server. </p>
</blockquote>

CC BY-SA 4.0
2020-12-06T21:31:18.913Z
Modifying the container's hosts file will likely fail because docker will clobber the /etc/hosts file
Charlie
CC BY-SA 4.0
2020-06-16T07:43:45.553Z
2020-06-16T07:43:45.553Z
Recoba20
<p>Is this what you are trying to do?</p>
<pre><code>$ docker run --add-host foo:127.0.0.42 alpine ping foo -c 3
PING foo (127.0.0.42): 56 data bytes
64 bytes from 127.0.0.42: seq=0 ttl=64 time=0.045 ms
64 bytes from 127.0.0.42: seq=1 ttl=64 time=0.061 ms
64 bytes from 127.0.0.42: seq=2 ttl=64 time=0.070 ms

--- foo ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.045/0.058/0.070 ms
</code></pre>
<p>Look for --add-host parameter for <a href="https://docs.docker.com/engine/reference/commandline/run/" rel="nofollow noreferrer">docker run</a> or <a href="https://docs.docker.com/compose/compose-file/05-services/#extra_hosts" rel="nofollow noreferrer">extra_hosts</a> for docker-compose.</p>

CC BY-SA 4.0
2020-06-18T08:12:01.390Z
2023-06-10T18:50:26.507Z
2023-06-10T18:50:26.507Z
clorz
<p>The <code>extra_hosts</code> setting in <code>docker-compose</code> might help you. <a href="https://docs.docker.com/compose/compose-file/#extra_hosts" rel="nofollow noreferrer">Docs</a></p>

CC BY-SA 4.0
2020-06-17T15:01:42.200Z
2020-06-17T15:01:42.200Z
smavroud
<p>I am not sure I know the terminology to describe this correctly, my apologies.</p>

<p>But for a contrived example. Say I have a docker container that runs code that accesses "google.com". But I want all requests that the container sets to "google.com" to actually go to "facebook.com". In other words I want the DNS lookup of one domain name to use another domain when reloving. Is there a way to do this with docker or docker-compose?</p>

<p>My ask is a bit more than this, because I know that often multiple websites are hosted on the same server (same IP), so I need a way for the conversion to happen at the domain name level, not at the IP level.</p>

<p>Thank you</p>

CC BY-SA 4.0
2020-06-15T16:22:26.523Z
I'm bit confuse, you wanna that container continuous using "google.com" but resolv this name to another address?
Vader
<docker><docker-compose><dns>
Trick a docker containers DNS to resolve a different url
JasoonS
# Trick a docker containers DNS to resolve a different url

(Asked by: username_0 on 2020-06-14)

I am not sure I know the terminology to describe this correctly, my apologies.

But for a contrived example. Say I have a docker container that runs code that accesses "google.com". But I want all requests that the container sets to "google.com" to actually go to "facebook.com". In other words I want the DNS lookup of one domain name to use another domain when reloving. Is there a way to do this with docker or docker-compose?

My ask is a bit more than this, because I know that often multiple websites are hosted on the same server (same IP), so I need a way for the conversion to happen at the domain name level, not at the IP level.

Thank you

**Tags:** docker, docker-compose, dns

> **Comments:**
> **username_1**: I'm bit confuse, you wanna that container continuous using "google.com" but resolv this name to another address?  

---

## 3 answers

### Answer by username_2 on 2020-06-16. Score: 3

If you want a DNS lookup of one domain to resolve to another one, you have 2 options.

1. Modify the Container /etc/hosts file. By default any DNS lookup is done there. You schould do that in the Dockerfile.

[Modify /etc/hosts in a Dockerfile](https://stackoverflow.com/questions/38302867/how-to-update-etc-hosts-file-in-docker-image-during-docker-build)

2. Maintain your own DNS servers with all mapping records. On container runtime just pass the DNS server.

[Docker docs](https://docs.docker.com/config/containers/container-networking/)

> --dns The IP address of a DNS server.

> **Comments:**
> **username_3**: Modifying the container's hosts file will likely fail because docker will clobber the /etc/hosts file  

---

### Answer by username_4 on 2020-06-18. Score: 2

Is this what you are trying to do?
$ docker run --add-host foo:127.0.0.42 alpine ping foo -c 3
PING foo (127.0.0.42): 56 data bytes
64 bytes from 127.0.0.42: seq=0 ttl=64 time=0.045 ms
64 bytes from 127.0.0.42: seq=1 ttl=64 time=0.061 ms
64 bytes from 127.0.0.42: seq=2 ttl=64 time=0.070 ms

--- foo ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.045/0.058/0.070 ms

Look for --add-host parameter for docker run or extra_hosts for docker-compose.


---

### Answer by username_5 on 2020-06-17. Score: 1

The `extra_hosts` setting in `docker-compose` might help you. [Docs](https://docs.docker.com/compose/compose-file/#extra_hosts)


---

