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

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

问题描述

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

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

容器基于 Alpine Linux(Docker Hub 上的 alpine:3.2),但我要描述的问题也发生在官方 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 hub 上可用,您可以尝试它们).

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

如果我在启动第二个和第三个容器之前等待一段时间,那么主机名 zk2zk3 被放在 /etc/hosts 中> 太晚了(由 docker),Java 无法找到它们:我在 zk1 的日志中得到 java.net.UnknownHostException 两个 zk2zk3.

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.

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

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.

问题的原因是什么?

推荐答案

通过切换到 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 上).

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

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

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