docker ping unknown Host exception

4/11/2018

docker container gives unknowHost exception when

ping "private Network hostname" 

ping: unknown host

But when I ping by IP it gives result

8 packets transmitted, 8 packets received, 0% packet loss

The work around seems to be adding host entry to /etc/hosts file in the running docker container but I am using the docker in K8 platform that dynamically creates new container so I can not manually add host entries. I was wondering why it can not resolve host name. Any help appreciated :)

-- Shiningstar
docker
docker-compose
dockerfile
kubectl
kubernetes

1 Answer

4/11/2018

You can add hostAliases in Pod Spec. For details, see the official doc.

Here is an example of Pod where hostAliases are used:

apiVersion: v1
kind: Pod
metadata:
  name: hostaliases-pod
spec:
  restartPolicy: Never
  hostAliases:
  - ip: "8.8.8.8"
    hostnames:
    - "foo.local"
    - "bar.local"
  containers:
  - name: cat-hosts
    image: busybox
    command:
    - ping
    args:
    - "foo.local"

If we see the logs of the pod:

$ kubectl logs po/hostaliases-pod
PING foo.local (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=61 time=51.333 ms
64 bytes from 8.8.8.8: seq=1 ttl=61 time=59.600 ms
....

As it is said in official doc, there is some limitations:

  • HostAlias is only supported in 1.7+.

  • HostAlias support in 1.7 is limited to non-hostNetwork Pods because kubelet only manages the hosts file for non-hostNetwork Pods.

  • In 1.8, HostAlias is supported for all Pods regardless of network configuration.

-- Abdullah Al Maruf - Tuhin
Source: StackOverflow