title: java.net.UnknownHostException on Docker
body: <p>I am trying to create docker containers for ZooKeeper and configure them in cluster mode (full code is <a href="https://github.com/dockmob/docker-zookeeper">here</a> and <a href="https://github.com/dockmob/docker-java">here</a>).</p>
  
  <p>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).</p>
  
  <p>I use the following commands to start the cluster: </p>
  
  <pre><code>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
  </code></pre>
  
  <p>(They are available on docker hub, you can try them).</p>
  
  <p>If I wait some time before starting the second and third containers, then the host names <code>zk2</code> and <code>zk3</code> are put in <code>/etc/hosts</code> too late (by docker), and Java is unable to find them: I get <code>java.net.UnknownHostException</code> in the logs of <code>zk1</code> for both <code>zk2</code> and <code>zk3</code>.</p>
  
  <p>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 <code>Dockerfile</code> in order to update the <code>java.security</code> settings:</p>
  
  <pre><code>RUN grep '^networkaddress.cache.ttl=' /usr/lib/jvm/java-1.7-openjdk/jre/lib/security/java.security || echo 'networkaddress.cache.ttl=10' &gt;&gt; /usr/lib/jvm/java-1.7-openjdk/jre/lib/security/java.security
  </code></pre>
  
  <p>It sets the DNS TTL property (<code>networkaddress.cache.ttl</code>) to <code>10</code> seconds.</p>
  
  <p>The variable <code>networkaddress.cache.negative.ttl</code> is already set to its default value (<code>10</code>).</p>
  
  <p>The behavior does not change. I get lots of <code>java.net.UnknownHostException</code> repeatedly.</p>
  
  <p>What can be the cause of the problem?</p>