Testcontainers-java: ElasticsearchContainer - Multiple instances created when discovery.type=single-node

Created on 23 Nov 2020  路  2Comments  路  Source: testcontainers/testcontainers-java

I am trying to create an elasticsearchcontainer for my CI process but find that the start up time is too long since there are 3-4 instances being launched. I am using scala 2.12 with testcontainers version 1.15

My code:

  val container: ElasticsearchContainer =
    new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:7.10.0")
      .withExposedPorts(9300, 9200)
      .withEnv("discovery.type", "single-node")
  container.start()

Result logs:

                     2020-11-23 09:29:44,408 [main] INFO  o.t.utility.RegistryAuthLocator -  Failure when attempting to lookup auth config. Please ignore if you don't have images in an authenticated registry. Details: (dockerImageName: testcontainers/ryuk:0.3.0, configFile: /home/doug/.docker/config.json. Falling back to docker-java default behaviour. Exception message: /home/doug/.docker/config.json (No such file or directory)
                     2020-11-23 09:29:47,803 [main] INFO  o.testcontainers.DockerClientFactory -  Ryuk started - will monitor and terminate Testcontainers containers on JVM exit
                     2020-11-23 09:29:47,806 [main] INFO  o.testcontainers.DockerClientFactory -  Checking the system...
                     2020-11-23 09:29:47,806 [main] INFO  o.testcontainers.DockerClientFactory -  鉁旓笌 Docker server version should be at least 1.6.0
                     2020-11-23 09:29:48,164 [main] INFO  o.testcontainers.DockerClientFactory -  鉁旓笌 Docker environment should have more than 2GB free disk space
                     2020-11-23 09:29:48,184 [main] INFO  ?.e.c.10.0] -  Starting an elasticsearch container using [docker.elastic.co/elasticsearch/elasticsearch:7.10.0]
                     2020-11-23 09:29:48,208 [main] INFO  ?.e.c.10.0] -  Creating container for image: docker.elastic.co/elasticsearch/elasticsearch:7.10.0
                     2020-11-23 09:29:48,209 [main] INFO  o.t.utility.RegistryAuthLocator -  Failure when attempting to lookup auth config. Please ignore if you don't have images in an authenticated registry. Details: (dockerImageName: docker.elastic.co/elasticsearch/elasticsearch:7.10.0, configFile: /home/doug/.docker/config.json. Falling back to docker-java default behaviour. Exception message: /home/doug/.docker/config.json (No such file or directory)
                     2020-11-23 09:29:48,489 [main] INFO  ?.e.c.10.0] -  Starting container with ID: 8d74e2b0fccb10114301bdaaefec2203804abbc537f059fbea810a4d6ed82e15
                     2020-11-23 09:29:49,378 [main] INFO  ?.e.c.10.0] -  Container docker.elastic.co/elasticsearch/elasticsearch:7.10.0 is starting: 8d74e2b0fccb10114301bdaaefec2203804abbc537f059fbea810a4d6ed82e15
                     2020-11-23 09:29:49,405 [main] INFO  o.t.c.wait.strategy.HttpWaitStrategy -  /loving_wozniak: Waiting for 300 seconds for URL: http://localhost:32799/
                     2020-11-23 09:30:19,193 [main] INFO  ?.e.c.10.0] -  Container docker.elastic.co/elasticsearch/elasticsearch:7.10.0 started in PT30.982S
                     2020-11-23 09:30:19,498 [main] INFO  ?.e.c.10.0] -  Starting an elasticsearch container using [docker.elastic.co/elasticsearch/elasticsearch:7.10.0]
                     2020-11-23 09:30:19,498 [main] INFO  ?.e.c.10.0] -  Creating container for image: docker.elastic.co/elasticsearch/elasticsearch:7.10.0
                     2020-11-23 09:30:20,472 [main] INFO  ?.e.c.10.0] -  Starting container with ID: a8cf1585aa55f5692afaee95c5643f206161913ce3269b6130fb16db89fc56e3
                     2020-11-23 09:30:21,708 [main] INFO  ?.e.c.10.0] -  Container docker.elastic.co/elasticsearch/elasticsearch:7.10.0 is starting: a8cf1585aa55f5692afaee95c5643f206161913ce3269b6130fb16db89fc56e3
                     2020-11-23 09:30:21,721 [main] INFO  o.t.c.wait.strategy.HttpWaitStrategy -  /dazzling_lichterman: Waiting for 300 seconds for URL: http://localhost:32801/
                     2020-11-23 09:30:47,293 [main] INFO  ?.e.c.10.0] -  Container docker.elastic.co/elasticsearch/elasticsearch:7.10.0 started in PT27.795S
                     2020-11-23 09:30:47,295 [main] INFO  ?.e.c.10.0] -  Starting an elasticsearch container using [docker.elastic.co/elasticsearch/elasticsearch:7.10.0]
                     2020-11-23 09:30:47,296 [main] INFO  ?.e.c.10.0] -  Creating container for image: docker.elastic.co/elasticsearch/elasticsearch:7.10.0
                     2020-11-23 09:30:47,701 [main] INFO  ?.e.c.10.0] -  Starting container with ID: 248ec0908480e7b52b9dfb3f23cc728829ac8592db9ad46439c21fca05466714
                     2020-11-23 09:30:48,680 [main] INFO  ?.e.c.10.0] -  Container docker.elastic.co/elasticsearch/elasticsearch:7.10.0 is starting: 248ec0908480e7b52b9dfb3f23cc728829ac8592db9ad46439c21fca05466714
                     2020-11-23 09:30:48,689 [main] INFO  o.t.c.wait.strategy.HttpWaitStrategy -  /jovial_cartwright: Waiting for 300 seconds for URL: http://localhost:32803/
                     2020-11-23 09:31:11,353 [main] INFO  ?.e.c.10.0] -  Container docker.elastic.co/elasticsearch/elasticsearch:7.10.0 started in PT24.058S
                     2020-11-23 09:31:11,356 [main] INFO  ?.e.c.10.0] -  Starting an elasticsearch container using [docker.elastic.co/elasticsearch/elasticsearch:7.10.0]
                     2020-11-23 09:31:11,356 [main] INFO  ?.e.c.10.0] -  Creating container for image: docker.elastic.co/elasticsearch/elasticsearch:7.10.0
                     2020-11-23 09:31:11,666 [main] INFO  ?.e.c.10.0] -  Starting container with ID: 5340ea3df2e6cb4d16953e92852abbdd7c64fa104599ff3608df0df5cf875b23
                     2020-11-23 09:31:12,747 [main] INFO  ?.e.c.10.0] -  Container docker.elastic.co/elasticsearch/elasticsearch:7.10.0 is starting: 5340ea3df2e6cb4d16953e92852abbdd7c64fa104599ff3608df0df5cf875b23
                     2020-11-23 09:31:12,765 [main] INFO  o.t.c.wait.strategy.HttpWaitStrategy -  /sad_poitras: Waiting for 300 seconds for URL: http://localhost:32805/
                     2020-11-23 09:31:37,367 [main] INFO  ?.e.c.10.0] -  Container docker.elastic.co/elasticsearch/elasticsearch:7.10.0 started in PT26.011S

Is there any way to only create one container for elasticsearch or is this expected behavior?

resolutioinvalid

Most helpful comment

From the logs it is clear that you're starting the container multiple times. I just tried the following test and it started the container only once (as expected):
https://github.com/testcontainers/testcontainers-java/blob/1e597ccd4fa1a7113d3077599e0b13237c1fef51/modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java#L86-L92

Refer to your test engine's docs on how to reuse resources between the tests or use the singleton container approach.

All 2 comments

From the logs it is clear that you're starting the container multiple times. I just tried the following test and it started the container only once (as expected):
https://github.com/testcontainers/testcontainers-java/blob/1e597ccd4fa1a7113d3077599e0b13237c1fef51/modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java#L86-L92

Refer to your test engine's docs on how to reuse resources between the tests or use the singleton container approach.

@bsideup thank you for your help. That was indeed the issue :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

denis-zhdanov picture denis-zhdanov  路  3Comments

chomhanks picture chomhanks  路  3Comments

itudoben picture itudoben  路  3Comments

micheal-swiggs picture micheal-swiggs  路  4Comments

andredasilvapinto picture andredasilvapinto  路  3Comments