Java.net.UnknownHostException on Docker

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

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

To start the cluster, I use the following commands:

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 the docker hub, you can try them).

If I wait a while before starting the second and third containers, the host names zk2 and zk3 will be placed in /etc/hosts too late (via docker) and Java will not be able to find them: I get java.net.UnknownHostException in zk1 for zk2 and zk3 .

I found on the Internet that I needed to disable the JVM DNS cache in order to update host names, so I entered the following command in the Dockerfile command 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 

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

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

The behavior does not change. I get a lot of java.net.UnknownHostException several times.

What could be causing the problem?

+6
source share
2 answers

I managed to get rid of DNS problems by switching to Oracle JRE 8 and using the following hack in the Docker file:

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

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

0
source

In my case, the Java application is not java.net.UnknownHostException with java.net.UnknownHostException when running in Docker. The reason was because I used the --network=none of the docker flag (getting ip / hostname via dhcp and pipework). In this case, Docker does not automatically add /etc/hosts for example

127.0.0.1 15e326aecf84

And getCanonicalHostName() Java getCanonicalHostName() is an exception.

Possible solutions:

  • add a hostname entry to the /etc/hosts using the docker run --hostname=your-hostname.com
  • switch to docker-managed network configuration
+4
source

All Articles