Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang's handling of localhost

Tags:

go

dns

resolve

musl

I have an alpine linux system which uses the musl libc and therefore has no /etc/nsswitch.conf.

According to tcpdump on this system, net.LookupIP("localhost") actually does a query for localhost.a.b.c, where a.b.c is taken from domain a.b.c in /etc/resolv.conf. But if I add an /etc/nsswitch.conf with hosts: files dns, it correctly looks up localhost from /etc/hosts.

As far as I can tell, no libc implementation tries to add a domain to localhost when looking it up in DNS. So (a) is there some good reason it's done this way or is it a bug? and (b) is there a way to work around it without adding an nsswitch.conf and while still using just the name localhost?

Edit More details:

ldd ./dnstest
    /lib/ld-musl-armhf.so.1 (0x76f2c000)
    libc.so => /lib/ld-musl-armhf.so.1 (0x76f2c000)

So yes, there is a dependency on libc.so. I have tried this with CGO_ENABLED=1 and with CGO_ENABLED=0 in the build commandline, too.

like image 830
Tom Avatar asked Dec 14 '25 14:12

Tom


1 Answers

  • alpine think that it's golang bug: https://github.com/gliderlabs/docker-alpine/issues/367
  • golang think it's not be compatible with glibc: https://github.com/golang/go/issues/35305
  • golang is waiting changes of glibc: https://github.com/golang/go/issues/33019
  • maybe glibc is making change: https://sourceware.org/bugzilla/show_bug.cgi?id=28700

So currently, it still needs RUN [ ! -e /etc/nsswitch.conf ] && echo 'hosts: files dns' > /etc/nsswitch.conf as workaround to use alpine image as base image to run golang binary.

like image 128
chestack Avatar answered Dec 18 '25 11:12

chestack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!