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

  • I'm bit confuse, you wanna that container continuous using "google.com" but resolv this name to another address? Jun 15 '20 at 16:22

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

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

Docker docs

--dns The IP address of a DNS server.

  • Modifying the container's hosts file will likely fail because docker will clobber the /etc/hosts file
    – Charlie
    Dec 6 '20 at 21:31

The extra_hosts setting in docker-compose might help you. Docs


Is this what you are trying to do?

$ docker run --add-host foo: alpine ping foo -c 3
PING foo ( 56 data bytes
64 bytes from seq=0 ttl=64 time=0.045 ms
64 bytes from seq=1 ttl=64 time=0.061 ms
64 bytes from 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.