herm
herm

Reputation: 16315

Different dns entries in docker container than host

When I ping a URL from my Ubuntu system it resolves correctly to the IP of the desired server. When I do the same thing from within my container it resolves to some other IP (I think its an old DNS entry).

Does anyone know why this happens and how to fix it?

Update

I run the container with the following command:

docker run -it -v $(pwd):/data-storage dotnet-testbed /bin/bash

Pinging Google works:

PING google.com (172.217.19.174): 56 data bytes
64 bytes from 172.217.19.174: icmp_seq=0 ttl=54 time=4.537 ms

pinging the myUrl shows the wrong ip and gets stuck:

PING myUrl (**.***.***.**): 56 data bytes

Upvotes: 0

Views: 3022

Answers (2)

Birchlabs
Birchlabs

Reputation: 8056

You may find the following techniques useful to investigate how your Docker container resolves a host…

Search the hosts database:

πŸ” docker run alpine getent hosts stackoverflow.com       
151.101.1.69      stackoverflow.com  stackoverflow.com

Check the hosts file:

πŸ” docker run alpine cat /etc/hosts
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3  6a431f90415d

Check the hosts added to the container's hosts file explicitly by docker run --add-host:

πŸ” docker run --name=my_cool_container -i -d --add-host stackoverflow.com:127.0.0.1 alpine sh
πŸ” docker inspect -f '{{range $index, $value := .HostConfig.ExtraHosts}}{{$value}}
{{end}}' my_cool_container
stackoverflow.com:127.0.0.1

But maybe this doesn't have so much to do with Docker. You should run ifconfig inside the container and check whether its DNS server is the same as your host machine's. You can also use dig to check the DNS answer that it receives.

I'd be very surprised if this was an "old DNS entry" in the container (containers are often short-lived). If so: you could fix that by redeploying the container (or learning how to purge its DNS cache).

Most likely: there's some proxy that your host machine uses, but the container does not. By default, Docker containers use bridge networking. If you switch it to use host networking: the container will use your host's networking stack. This may be enough to make it benefit from your proxy.

Upvotes: 1

adrian.riobo
adrian.riobo

Reputation: 495

Are you behind a proxy?... You can set http_proxy as environment variable in your container .. or pass as an argument for building purpouses

Upvotes: 0

Related Questions