8

I try to write test using elasticsearch container. I run it with https://www.testcontainers.org/ library. That's my configuration:

@ClassRule
public static GenericContainer elasticContainer =
        new GenericContainer("docker.elastic.co/elasticsearch/elasticsearch:5.3.0")
                .withExposedPorts(9300, 9200)
                .withEnv("xpack.security.enabled", "false")
                .withEnv("transport.host", "127.0.0.1")
                .withEnv("http.host", "0.0.0.0");

And I got an exception:

org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{9fuJUZYWS6O6IgLGOgJDaA}{localhost}{127.0.0.1:32792}]

I reconfigured my ports for test and 9200 port is available (on the port that mapped by testcontainers) - I checked it by curl. But 9300 is not.

Does anybody knows how to fix transport host problem?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
knalx
  • 171
  • 1
  • 5

2 Answers2

9

The problem was with elastic search container - not with testcontainers lib.

I have found solution here https://github.com/olivere/elastic/issues/57#issuecomment-88697714

Transport client can't resolve ElasticSearch node in container.

Final code is:

@ClassRule
public static GenericContainer elasticContainer =
        new FixedHostPortGenericContainer("docker.elastic.co/elasticsearch/elasticsearch:5.3.0")
                .withFixedExposedPort(9200, 9200)
                .withFixedExposedPort(9300, 9300)
                .waitingFor(Wait.forHttp("/")) // Wait until elastic start
                .withEnv("xpack.security.enabled", "false")
                .withEnv("network.host", "_site_")
                .withEnv("network.publish_host", "_local_");

Also if you want just start ElasticSearch in docker and use 9300 (transport port) run this:

docker run  -p 9300:9300 -p 9200:9200 -e "xpack.security.enabled=false"  -e "network.host=_site_" -e "network.publish_host=_local_"  docker.elastic.co/elasticsearch/elasticsearch:5.3.0
knalx
  • 171
  • 1
  • 5
  • Just a quick comment from a Testcontainers maintainer: we generally discourage the use of FixedHostPortGenericContainer because of the risk of port collisions. It's there for cases where there really is no other option, but please be aware of the risks! – Richard North Nov 13 '18 at 09:30
  • 2
    Do you have any working example with spring-boot-elasticsearch? – Marcin Erbel Jan 18 '19 at 14:37
9

For anyone coming to this question now, please note that as of Testcontainers 1.10.1, we have Elasticsearch as an official module in the Testcontainers library. You should hopefully find this much easier than 'rolling your own' using GenericContainer!

Usage is documented here.

Richard North
  • 432
  • 4
  • 6