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
java.net.UnknownHostException on Docker
提问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 zk2
and zk3
are put in /etc/hosts
too late (by docker), and Java is unable to find them: I get java.net.UnknownHostException
in the logs of zk1
for both zk2
and zk3
.
如果我等待一段时间开始的第二和第三个容器,那么主机名之前zk2
并zk3
放入/etc/hosts
太晚(由码头工人)和Java是无法找到他们:我得到java.net.UnknownHostException
在日志zk1
两个zk2
和zk3
。
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 Dockerfile
in order to update the java.security
settings:
我在网上发现我需要禁用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 10
seconds.
它将 DNS TTL 属性 ( networkaddress.cache.ttl
) 设置为10
秒。
The variable networkaddress.cache.negative.ttl
is already set to its default value (10
).
该变量networkaddress.cache.negative.ttl
已设置为其默认值 ( 10
)。
The behavior does not change. I get lots of java.net.UnknownHostException
repeatedly.
行为不会改变。我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.UnknownHostException
when running in docker. The reason was that I used --network=none
docker flag (getting ip/hostname via dhcp and pipework). In this case, docker does not add automatically to /etc/hosts
entry like
在我的情况下,java 应用程序java.net.UnknownHostException
在 docker 中运行时失败。原因是我使用了--network=none
docker 标志(通过 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/hosts
file viadocker run
parameter--hostname=your-hostname.com
- switch to docker-managed network configuration
/etc/hosts
通过docker run
参数将主机名条目添加到文件--hostname=your-hostname.com
- 切换到 docker 管理的网络配置