1

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

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

3 Answers 3

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

  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.

1
  • Modifying the container's hosts file will likely fail because docker will clobber the /etc/hosts file
    – Charlie
    Commented Dec 6, 2020 at 21:31
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.

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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