Docker上的java.net.UnknownHostException [英] java.net.UnknownHostException on Docker

查看:6316
本文介绍了Docker上的java.net.UnknownHostException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为ZooKeeper创建Docker容器,并以集群模式进行配置(完整代码为此处这里)。

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

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

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).

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

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

(它们在Docker中心可用,您可以尝试)

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

如果我在开始第二个和第三个容器之前等待一段时间,那么主机名称 zk2 code> zk3 放在 / etc /主机太晚(通过docker),而Java无法找到它们:我在<$ c的日志中获取 java.net.UnknownHostException $ c> zk1 zk2 zk3

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.

我在Web上发现我需要禁用JVM DNS缓存,以刷新主机名,所以我在$ code> Dockerfile 为了更新 java.security 设置:

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:

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

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

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

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

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

行为不变。我多次收到很多 java.net.UnknownHostException

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

可能是问题的原因? p>

What can be the cause of the problem?

推荐答案

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

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

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

我在 Docker Hub (代码位于 github )。

这篇关于Docker上的java.net.UnknownHostException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆