golang在Docker容器中使用net.LookupHost返回127.0.53.53。

huangapple go评论102阅读模式
英文:

golang net.LookupHost in docker container return 127.0.53.53

问题

我正在编写一个小应用程序,它应该在一个Docker容器中运行,并且应该通过主机名与其他Docker容器进行交互。

我试图使用net.LookupHost来获取服务器的IP地址,但我总是得到127.0.53.53。

奇怪的是,我可以使用dig等DNS工具从同一个容器中获取正确的IP地址。只有从Go程序中无法正常工作。

以下是我从代码中获取IP地址的部分。

zk_server_ips, err := net.LookupHost("zookeeper")
addrs, err := net.LookupIP("testserver")
if err != nil {
    fmt.Fprintf(w, "无法查找testserver的IP地址")
    return
}
for _, addr := range addrs {
    if ipv4 := addr.To4(); ipv4 != nil {
        fmt.Fprintf(w, "IPv4: %s", ipv4)
    }
}

我读到很多关于127.0.53.53是ICANN告诉我DNS设置有问题的方式,但我不知道为什么从Go代码中无法正常工作,而从dig和drill中可以正常工作!

另外,设置Docker容器以解析主机名的正确方法是什么?

容器正在运行Alpine Linux镜像。

以下是容器中的resolv.conf文件内容:

search fritz.box
nameserver 127.0.0.11
options ndots:0
英文:

I am writing a small app that should run in a docker container, and should interact with other docker container via its hostname.

I am trying to get the IP Address of the server using net.LookupHost, but I am always getting 127.0.53.53

The weird thing is that I can get the right IP Address using dns tools like dig from the same container. It only does not work from the go program.

Below is the section from my code where I get the IP Address.

zk_server_ips, err := net.LookupHost("zookeeper")
addrs, err := net.LookupIP("testserver")
if err != nil {
    fmt.Fprintf(w, "Failed to lookup ip address for testserver")
    return
}
for _, addr := range addrs {
if ipv4 := addr.To4(); ipv4 != nil {
    fmt.Fprintf(w, "IPv4: %s", ipv4)
}

I have read a lot that 127.0.53.53 is the way ICANN is telling me that there is something wrong with dsn setup, but I do not know why is it working with dig and drill and not from the go code. !

Also what is the right way to setup the docker containers to resolve hostnames?

The containers are running on Alpine linux image.

Below is my resolv.conf in the container:

search fritz.box
nameserver 127.0.0.11
options ndots:0

答案1

得分: 1

我在使用Telegraf时遇到了相同的问题。

不幸的是,我不知道根本原因,但我通过在使用docker run时设置--dns-search=.或在Docker Compose v1 yaml文件中设置dns_search: .来解决了这个问题:

telegraf:
  image: telegraf:1.2.1-alpine
  dns_search: .
  volumes:
    - ./telegraf.conf:/etc/telegraf/telegraf.conf:ro

有关--dns-search开关的详细信息,请参阅Docker DNS文档

英文:

I've run into the same issue when using Telegraf.

Unfortunately I don't know the root cause, but I was able to work around the issue by setting --dns-search=. when using docker run or by setting dns_search: . in Docker Compose v1 yaml files:

telegraf:
  image: telegraf:1.2.1-alpine
  dns_search: .
  volumes:
    - ./telegraf.conf:/etc/telegraf/telegraf.conf:ro

See the Docker DNS documentation for details about the --dns-search switch.

答案2

得分: 0

.box是一个真实的顶级域名(TLD)。”(https://gtldresult.icann.org/application-result/applicationstatus/applicationdetails/160)请不要将其用于内部系统。

如果你需要为本地系统提供主机名,请使用保留用于此目的的.local顶级域名(TLD)。

英文:

.box is a real TLD. Don't use it for internal systems.

If you need to give hostnames to local systems, use the .local TLD, which is reserved for this purpose.

huangapple
  • 本文由 发表于 2016年12月13日 06:43:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/41110935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定