Docker 上的 java.net.UnknownHostException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32537977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:20:58  来源:igfitidea点击:

java.net.UnknownHostException on Docker

javadnsdocker

提问by Nicola Ferraro

I am trying to create docker containers for ZooKeeper and configure them in cluster mode (full code is hereand here).

我正在尝试为 ZooKeeper 创建 docker 容器并在集群模式下配置它们(完整代码在这里这里)。

Containers are based on Alpine Linux (alpine:3.2 on Docker Hub), but the problem that I'm going to describe happens also with the official Java container (java:7).

容器基于 Alpine Linux(Docker Hub 上的 alpine:3.2),但我将要描述的问题也发生在官方 Java 容器(java:7)上。

I use the following commands to start the cluster:

我使用以下命令启动集群:

docker run -d -h zk1 --name zk1 dockmob/zookeeper -s zk1,zk2,zk3
# wait some time ...
docker run -d -h zk2 --name zk2 dockmob/zookeeper -s zk1,zk2,zk3
docker run -d -h zk3 --name zk3 dockmob/zookeeper -s zk1,zk2,zk3

(They are available on docker hub, you can try them).

(它们在 docker hub 上可用,您可以尝试它们)。

If I wait some time before starting the second and third containers, then the host names zk2and zk3are put in /etc/hoststoo late (by docker), and Java is unable to find them: I get java.net.UnknownHostExceptionin the logs of zk1for both zk2and zk3.

如果我等待一段时间开始的第二和第三个容器,那么主机名之前zk2zk3放入/etc/hosts太晚(由码头工人)和Java是无法找到他们:我得到java.net.UnknownHostException在日志zk1两个zk2zk3

I found on the web that I need to disable JVM DNS cache in order to refresh the host names, so I introduced the following command in the Dockerfilein order to update the java.securitysettings:

我在网上发现我需要禁用JVM DNS缓存才能刷新主机名,因此我在其中引入了以下命令Dockerfile以更新java.security设置:

RUN grep '^networkaddress.cache.ttl=' /usr/lib/jvm/java-1.7-openjdk/jre/lib/security/java.security || echo 'networkaddress.cache.ttl=10' >> /usr/lib/jvm/java-1.7-openjdk/jre/lib/security/java.security

It sets the DNS TTL property (networkaddress.cache.ttl) to 10seconds.

它将 DNS TTL 属性 ( networkaddress.cache.ttl) 设置为10秒。

The variable networkaddress.cache.negative.ttlis already set to its default value (10).

该变量networkaddress.cache.negative.ttl已设置为其默认值 ( 10)。

The behavior does not change. I get lots of java.net.UnknownHostExceptionrepeatedly.

行为不会改变。我java.net.UnknownHostException反复得到很多。

What can be the cause of the problem?

问题的原因是什么?

采纳答案by Nicola Ferraro

I managed to get rid of the DNS issues by switching to Oracle JRE 8 and using the following hack in the Dockerfile:

通过切换到 Oracle JRE 8 并在 Dockerfile 中使用以下技巧,我设法摆脱了 DNS 问题:

RUN echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf

I created a working Java 8 docker container container on Docker Hub(the code is on github).

我在Docker Hub上创建了一个可用的 Java 8 docker 容器容器(代码在github 上)。

回答by Dmitriusan

In my case the java application was failing with java.net.UnknownHostExceptionwhen running in docker. The reason was that I used --network=nonedocker flag (getting ip/hostname via dhcp and pipework). In this case, docker does not add automatically to /etc/hostsentry like

在我的情况下,java 应用程序java.net.UnknownHostException在 docker 中运行时失败。原因是我使用了--network=nonedocker 标志(通过 dhcp 和管道获取 ip/hostname)。在这种情况下,docker 不会/etc/hosts像这样自动添加到条目中

127.0.0.1 15e326aecf84

127.0.0.1 15e326aecf84

And getCanonicalHostName()Java function threw this exception.

getCanonicalHostName()Java 函数抛出了这个异常。

Possible solutions:

可能的解决方案:

  • add hostname entry to /etc/hostsfile via docker runparameter --hostname=your-hostname.com
  • switch to docker-managed network configuration
  • /etc/hosts通过docker run参数将主机名条目添加到文件--hostname=your-hostname.com
  • 切换到 docker 管理的网络配置