Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container cannot resolve hosts

Tags:

docker

ubuntu

dns

From my host I can ping google

$ cat /etc/resolv.conf 
nameserver 127.0.1.1
search my.company.server

$ ping google.com
PING google.com (172.217.16.174) 56(84) bytes of data.
64 bytes from fra15s11-in-f14.1e100.net (172.217.16.174): icmp_seq=1 ttl=54 time=11.0 ms
64 bytes from fra15s11-in-f14.1e100.net (172.217.16.174): icmp_seq=2 ttl=54 time=10.7 ms

From the container I can reach internet:

$ docker run ubuntu:14.04 cat /etc/resolv.conf
search my.company.server
nameserver 8.8.8.8
nameserver 8.8.4.4


$ docker run ubuntu:14.04 ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=45 time=16.4 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=45 time=16.3 ms

But can't ping a hostname:

$ docker run ubuntu:14.04 ping google.com
<no answer>

Some environment info:

$ docker --version
Docker version 1.10.1, build 9e83765

$ cat /proc/version
Linux version 4.2.0-27-generic (buildd@lgw01-12) (gcc version 5.2.1 20151010 (Ubuntu 5.2.1-22ubuntu2) ) #32-Ubuntu SMP Fri Jan 22 04:49:08 UTC 2016

$ cat /proc/sys/net/ipv4/ip_forward
1

$ ps -ef|grep [d]ocker
root   ....... /usr/bin/docker daemon -H fd://

Similar to this: Docker container can reach DNS but not resolve hosts

but a reboot doesn't help...

like image 819
hosselausso Avatar asked Apr 29 '16 09:04

hosselausso


People also ask

What is docker DNS?

Docker uses embedded DNS to provide service discovery for containers running on a single Docker Engine and tasks running in a Docker Swarm. Docker Engine has an internal DNS server that provides name resolution to all of the containers on the host in user-defined bridge, overlay, and MACVLAN networks.

Can't resolve host Kali Linux?

Solution: Please make sure you have an active Internet connection and your Kali DNS settings are correct. We recommend using Google public DNS servers. Please set both DNS servers to 8.8.

What is docker host address?

AFAIK, in the case of Docker for Linux (standard distribution), the IP address of the host will always be 172.17. 0.1 (on the main network of docker, see comments to learn more). This is true of containers attached to the docker0 default bridge interface.

Could not resolve host host?

This error means that the hostname you are trying to connect to cannot be resolved to an IP address. (Hostnames are resolved to IP addresses by a DNS (Domain NameServer)). Please check what you have entered in the Address field. You will need the valid hostname of an FTP server or a valid IP address.


2 Answers

By default creating a new docker container also creates a virtual network that separates the docker network environment from the host network environment (somewhat). This allows one to easily spin up multiple containers which might all listen on the same port (e.g. 80), but in a way that can be mapped to unique ports on the host machine (e.g. service1:80 -> host:8080, service2:80 -> host:8081).

docker run YOUR_IMAGE --network="host" will bind the container network adapter to that of the host machine. This should allow you to access the host machine via listening port of the host machine. e.g. localhost:8080 However you need to remember that ports are a scarce resource and you cannot have conflicting port listeners in different containers when you do this.

You can also retrieve the host's ip address from within a docker container depending on your OS and docker version:

Mac/Windows: As of Docker v18.03+ you can use the host.docker.internal hostname to connect to your Docker host.

Linux: docker container run -e "DOCKER_HOST=$(ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+')" will make the host IP available from within the docker container as an environment variable: DOCKER_HOST

like image 62
carter Avatar answered Sep 17 '22 11:09

carter


Your issue can be triggered by a bad docker status.
You can try

sudo ip link delete docker0
sudo systemctl restart docker
like image 32
Damien C Avatar answered Sep 18 '22 11:09

Damien C