# Discovering dynamic port assignment

**URL:** <https://discuss.elastic.co/t/discovering-dynamic-port-assignment/137328>\
**Category:** Elasticsearch\
**Created:** [June 26, 2018, 12:23am UTC](https://discuss.elastic.co/t/discovering-dynamic-port-assignment/137328 "2018-06-26T00:23:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_White](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_white/32/32674_2.png) [@Chris\_White](https://discuss.elastic.co/u/Chris_White)\
**Post date:** [June 26, 2018, 12:23am UTC](https://discuss.elastic.co/t/discovering-dynamic-port-assignment/137328/1 "2018-06-26T00:23:38Z")

</div>

Is there a method by which you can easily / programmatically obtain the HTTP / Transport port number when starting a node using the `-Ehttp.port=0` or `-Etransport.tcp.port=0` arguments?

Specifically, i'm bringing up an ES node as part of an integration test, which will eventually run on a CI server. As multiple jobs may run concurrently, I need to avoid hard coding the ports numbers when bringing up the node. I can use the above arguments (or a range) to ensure that if there is a port conflict with an already running process, the node will still come up on another port - but short of parsing the `elasticsearch.log` file (or inspecting the output of a command like netstat for the node pid), is there a way to obtain the ports used.

For example, you can use the `-p` option to save off the PID to a file, but there isn't a similar (documented) option for ports.

(Yes Docker would be one solution, but not what i'm looking for comment on here)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 26, 2018, 6:47am UTC](https://discuss.elastic.co/t/discovering-dynamic-port-assignment/137328/2 "2018-06-26T06:47:56Z")

</div>

You can not run elasticsearch embedded. Read this [blog post](https://www.elastic.co/blog/elasticsearch-the-server).

Note that to run integration tests (not unit tests) you would probably prefer running that in something close to a production environment, like a real elasticsearch server instance. I shared some ideas about integration testing in this thread: [In memory testing with RestHighLevelClient](https://discuss.elastic.co/t/in-memory-testing-with-resthighlevelclient/106196/6)

---

<div class="post-metadata">

**Author:** ![Chris\_White](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_white/32/32674_2.png) [@Chris\_White](https://discuss.elastic.co/u/Chris_White)\
**Post date:** [June 26, 2018, 1:28pm UTC](https://discuss.elastic.co/t/discovering-dynamic-port-assignment/137328/3 "2018-06-26T13:28:34Z")

</div>

David, I'm not trying to run it embedded, i am starting a node as its own process with the -Ehttp.port=0 cli argument to bin/elasticseach, which will start the http service on a random unassigned port. what i need is a way to discover that port assignment for use with the high-level rest client.

As noted, i could log scrap to get it but I was hoping for a better method.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 26, 2018, 1:49pm UTC](https://discuss.elastic.co/t/discovering-dynamic-port-assignment/137328/4 "2018-06-26T13:49:02Z")

</div>

I see. I was unsure with that:

> i'm bringing up an ES node as part of an integration test

TBH I was not aware that you can start a node with `-Ehttp.port=0` or `-Etransport.tcp.port=0`.

> As noted, i could log scrap to get it but I was hoping for a better method.

I guess that's the only method for now. Wondering if this is something that would deserve a feature request... First time I'm seeing that need TBH so I'm unsure about what the team will say.

@rjernst WDYT?

---

<div class="post-metadata">

**Author:** ![Chris\_White](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_white/32/32674_2.png) [@Chris\_White](https://discuss.elastic.co/u/Chris_White)\
**Post date:** [June 26, 2018, 2:43pm UTC](https://discuss.elastic.co/t/discovering-dynamic-port-assignment/137328/5 "2018-06-26T14:43:52Z")

</div>

Using port 0 just means find a free port: [ServerSocket(0)](https://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html#ServerSocket(int)):

> port - the port number, or 0 to use a port number that is automatically allocated.

Are there any JMX metrics exposed that might be useful here, for connection via JMX Local Monitoring?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 26, 2018, 3:24pm UTC](https://discuss.elastic.co/t/discovering-dynamic-port-assignment/137328/6 "2018-06-26T15:24:54Z")

</div>

I don't think there is anything like that.

---

<div class="post-metadata">

**Author:** ![Chris\_White](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_white/32/32674_2.png) [@Chris\_White](https://discuss.elastic.co/u/Chris_White)\
**Post date:** [June 26, 2018, 5:28pm UTC](https://discuss.elastic.co/t/discovering-dynamic-port-assignment/137328/7 "2018-06-26T17:28:51Z")

</div>

Well in the hope this can be actioned:

> <https://github.com/elastic/elasticsearch/issues/31584>

---

<div class="post-metadata">

**Author:** ![jasontedor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasontedor/32/66992_2.png) [@jasontedor](https://discuss.elastic.co/u/jasontedor)\
**Post date:** [June 27, 2018, 3:59am UTC](https://discuss.elastic.co/t/discovering-dynamic-port-assignment/137328/8 "2018-06-27T03:59:31Z")

</div>

> [@dadoonet](#):
>
> TBH I was not aware that you can start a node with `-Ehttp.port=0` or `-Etransport.tcp.port=0` .

This is what we do in tests. It means to use an ephemeral port.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 27, 2018, 5:53am UTC](https://discuss.elastic.co/t/discovering-dynamic-port-assignment/137328/9 "2018-06-27T05:53:28Z")

</div>

I naively thought it was something set by gradle (like a random port with a check that nothing is running). 😁

---

<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 25, 2018, 5:53am UTC](https://discuss.elastic.co/t/discovering-dynamic-port-assignment/137328/10 "2018-07-25T05:53:37Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
