# Can the ES client wait until the ES server hostname resolves?

**URL:** <https://discuss.elastic.co/t/can-the-es-client-wait-until-the-es-server-hostname-resolves/54068>\
**Category:** Elasticsearch\
**Created:** [June 27, 2016, 6:44pm UTC](https://discuss.elastic.co/t/can-the-es-client-wait-until-the-es-server-hostname-resolves/54068 "2016-06-27T18:44:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![KajMagnus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kajmagnus/32/142465_2.png) [@KajMagnus](https://discuss.elastic.co/u/KajMagnus)\
**Post date:** [June 27, 2016, 6:44pm UTC](https://discuss.elastic.co/t/can-the-es-client-wait-until-the-es-server-hostname-resolves/54068/1 "2016-06-27T18:44:18Z")

</div>

Hi, I'm using Elasticsearch (ES) and Docker-Compose. Sometimes the ES Docker Container hasn't yet started, when my main container (the one that runs my code and connects to ES) starts. Then `InetAddress.getByName("search")` below throws a `java.net.UnknownHostException` (because Docker's DNS server hasn't yet assigned an IP address to the `search` container, as far as I've understood).

I would like ES to repeatedly try to [resolve the hostname of the ES Docker container], and, eventually, when the name resolves, connect to the host.

I didn't find any built-in functionality for this (in ES 5 alpha 3). I'm getting the impression that ES wants an IP number always, and doesn't work with hostnames at all: all examples I found use `InetAddress.getByName` and passes the result to ES.

1. Is there any way to have the ES client resolve the host, and politely wait until the hostname resolves to an IP number?

Also, if the ES server suddenly disappears, then, when the client attempts to reconnect, if now _again_ the DNS server cannot resolve the address, the client ought to wait until the hostname resolves, and then continue with attempting to reconnect.

1. Do any other people have this same situation? What did you do?

I suppose I can write my own appserver logic that won't create the ES client before the ES server hostname resolves. But I'd rather not :- )

This is my code, and `getByName` throws the `UnknownHostException`:

```auto
val elasticSearchClient = es.client.transport.TransportClient.builder().build()
    .addTransportAddress(
      new es.common.transport.InetSocketTransportAddress(
        java.net.InetAddress.getByName("search"), 9300))

```

(I hope all this is understandable :- ))

Best regards,  
KajMagnus

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [June 27, 2016, 7:38pm UTC](https://discuss.elastic.co/t/can-the-es-client-wait-until-the-es-server-hostname-resolves/54068/2 "2016-06-27T19:38:32Z")

</div>

> [@KajMagnus](#):
>
> 1. Is there any way to have the ES client resolve the host, and politely wait until the hostname resolves to an IP number?

No. This is not an ES issue; ES assumes full functioning and properly configured network. You should solve that in docker-compose. If not possible, write a shell script that waits for the network. If that is also not possible, roll your own Java code, and use a loop-controlled `InetAdress.isReachable()` before starting the ES client.

> [@KajMagnus](#):
>
> Also, if the ES server suddenly disappears, then, when the client attempts to reconnect, if now again the DNS server cannot resolve the address, the client ought to wait until the hostname resolves, and then continue with attempting to reconnect.
> 
> 1. Do any other people have this same situation? What did you do?

All ES clients have a failover mechanism implemented. ES does not make a difference between DNS server failure, network failure, or node failure. If failover is not possible because all ES nodes are controlled by a single DNS which fails, it is considered fatal - the ES client will stop to work and throw exceptions after a timeout.

---

<div class="post-metadata">

**Author:** ![KajMagnus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kajmagnus/32/142465_2.png) [@KajMagnus](https://discuss.elastic.co/u/KajMagnus)\
**Post date:** [June 27, 2016, 9:15pm UTC](https://discuss.elastic.co/t/can-the-es-client-wait-until-the-es-server-hostname-resolves/54068/3 "2016-06-27T21:15:38Z")

</div>

Thanks, that was lots of useful info.

(In my particular case, this won't work: _"write a shell script that waits for the network"_ because the app server should start directly, without waiting for ES. It's OK if search is unavailable for a short while. Docker-Compose does have built in functionality for starting things _in the correct order_, but it doesn't know how long it should wait in between each container it starts (it doesn't know when the services inside, are ready). So I'll write Scala code to deal with all this.)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:40pm UTC](https://discuss.elastic.co/t/can-the-es-client-wait-until-the-es-server-hostname-resolves/54068/4 "2017-07-05T22:40:05Z")

</div>


