Containers always resolve names to IPv6 Addresses

Docker version 1.9.1, build 78ee77d/1.9.1

I’m building an image from a Centos7 base. From within the container, I can only resolve the name as IPv6, not the actual IPv4 address of the host.

What am I doing wrong?

[root@host-100-104 ~]# docker exec -i -t nuage_percona_1 bash

Welcome to VSD. (4.0.2_ff80b7)

[root@host-100-104 /]# getent hosts $(hostname)
fe80::f816:3eff:fe75:4e67 host-100-104.openstacklocal
[root@host-100-104 /]# cat /etc/hosts host-100-104.openstacklocal localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@host-100-104 /]# ping host-100-104.openstacklocal
PING host-100-104.openstacklocal ( 56(84) bytes of data.
64 bytes from host-100-104.openstacklocal ( icmp_seq=1 ttl=64 time=0.065 ms
64 bytes from host-100-104.openstacklocal ( icmp_seq=2 ttl=64 time=0.055 ms
64 bytes from host-100-104.openstacklocal ( icmp_seq=3 ttl=64 time=0.179 ms
64 bytes from host-100-104.openstacklocal ( icmp_seq=4 ttl=64 time=0.064 ms
— host-100-104.openstacklocal ping statistics —

Inside nsswitch.conf in the centos:7 image, I have the following line:

hosts:      files dns myhostname

I can reproduce your issue too:

$ docker run --rm -it centos:7 /bin/bash
[root@ab43fd06ea46 /]# getent hosts $(hostname)
fe80::42:acff:fe11:3 ab43fd06ea46

If I take out the myhostname bit, I get the ipv4 address.

[root@ab43fd06ea46 /]# sed -i 's/ myhostname//g' /etc/nsswitch.conf
[root@ab43fd06ea46 /]# getent hosts $(hostname)      ab43fd06ea46

Something about the way that the myhostname method does the name resolution seems to prefer ipv6.