elasticsearchhibernate-searchtestcontainershibernate-search-6

TestContainer ElasticSearchContainer not able to connect to Docker daemon


In my integration tests using spring boot, I am trying to connect to ElasticSearchContainer by using following code:

@ClassRule
public static ElasticsearchContainer elasticsearch =
            new ElasticsearchContainer(DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:8.10.4"));

I have installed Docker Desktop and Docker engine is running.The option "Expose daemon on tcp://localhost:2375 without TLS" is ticked in Docker Desktop settings.

testcontainer dependency version: 1.19.1

Spring boot version - 2.5.5

Elasticsaerch version : 8.10.4

hibernate-search version: 6.2.2.Final

But on running the test, I get following error:

00:00.789 [main] DEBUG org.testcontainers.utility.TestcontainersConfiguration - Testcontainers configuration overrides will be loaded from file:/C:/Users/gaurav.bhardwaj/.testcontainers.properties
17:00:00.789 [main] DEBUG org.testcontainers.utility.TestcontainersConfiguration - Attempted to read Testcontainers configuration file at file:/C:/Users/gaurav.bhardwaj/.testcontainers.properties but the file was not found. Exception message: FileNotFoundException: C:\Users\gaurav.bhardwaj\.testcontainers.properties (The system cannot find the file specified)
17:00:00.805 [main] INFO org.testcontainers.images.PullPolicy - Image pull policy will be performed by: DefaultPullPolicy()
17:00:00.805 [main] INFO org.testcontainers.utility.ImageNameSubstitutor - Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')
17:00:00.820 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.docdata.payments.reconciliation.service.boundary.ReconciliationServiceTest]
17:00:00.820 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.docdata.payments.reconciliation.service.boundary.ReconciliationServiceTest]
17:00:01.072 [main] DEBUG org.testcontainers.dockerclient.DockerClientProviderStrategy - Trying out strategy: NpipeSocketClientProviderStrategy
17:00:11.094 [main] WARN org.testcontainers.dockerclient.DockerClientProviderStrategy - DOCKER_HOST npipe:////./pipe/docker_engine is not listening
17:00:11.094 [main] DEBUG org.testcontainers.dockerclient.DockerClientProviderStrategy - strategy NpipeSocketClientProviderStrategy did not pass the test
17:00:11.094 [main] INFO org.testcontainers.dockerclient.DockerMachineClientProviderStrategy - docker-machine executable was not found on PATH ([C:\Program Files\Common Files\Oracle\Java\javapath, C:\WINDOWS\system32, C:\WINDOWS, C:\WINDOWS\System32\Wbem, C:\WINDOWS\System32\WindowsPowerShell\v1.0\, C:\WINDOWS\System32\OpenSSH\, C:\ProgramData\chocolatey\bin, C:\Program Files\dotnet\, C:\Program Files\Git\cmd, C:\Program Files\PuTTY\, C:\Program Files\nodejs\, C:\Program Files\Docker\Docker\resources\bin, C:\Users\gaurav.bhardwaj\AppData\Local\Microsoft\WindowsApps, C:\Users\gaurav.bhardwaj\AppData\Roaming\npm])
17:00:11.094 [main] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy - Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
As no valid configuration was found, execution cannot continue.
See https://java.testcontainers.org/on_failure.html for more details.

AND

java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration

    at org.testcontainers.dockerclient.DockerClientProviderStrategy.lambda$getFirstValidStrategy$7(DockerClientProviderStrategy.java:277)
    at java.base/java.util.Optional.orElseThrow(Optional.java:408)
    at org.testcontainers.dockerclient.DockerClientProviderStrategy.getFirstValidStrategy(DockerClientProviderStrategy.java:268)
    at org.testcontainers.DockerClientFactory.getOrInitializeStrategy(DockerClientFactory.java:152)
    at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:193)
    at org.testcontainers.DockerClientFactory$1.getDockerClient(DockerClientFactory.java:106)
    at com.github.dockerjava.api.DockerClientDelegate.authConfig(DockerClientDelegate.java:109)
    at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:333)
    at org.testcontainers.containers.GenericContainer.starting(GenericContainer.java:1144)

EDIT 1: I changed ElasticSeachContainer instantiation to below and also added DOCKET_HOST in the environment variable

ElasticsearchContainer elasticsearch =
            new ElasticsearchContainer(DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:8.10.4"))
                    .withExposedPorts(9200)
                    .withCreateContainerCmdModifier(cmd -> cmd.withHostConfig(
                            new HostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(9200), new ExposedPort(9200)))
                    ));

and now no more getting previous error but getting following error:

org.hibernate.search.util.common.SearchException: HSEARCH400080: Unable to detect the Elasticsearch version running on the cluster: HSEARCH400007: Elasticsearch request failed: Connection is closed
Request: GET  with parameters {}
Response: (no response)
    at org.hibernate.search.backend.elasticsearch.client.impl.ElasticsearchClientUtils.getElasticsearchVersion(ElasticsearchClientUtils.java:59) ~[hibernate-search-backend-elasticsearch-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.search.backend.elasticsearch.impl.ElasticsearchLinkImpl.initVersion(ElasticsearchLinkImpl.java:206) ~[hibernate-search-backend-elasticsearch-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.search.backend.elasticsearch.impl.ElasticsearchLinkImpl.onStart(ElasticsearchLinkImpl.java:148) ~[hibernate-search-backend-elasticsearch-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.search.backend.elasticsearch.impl.ElasticsearchBackendFactory.create(ElasticsearchBackendFactory.java:130) ~[hibernate-search-backend-elasticsearch-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.search.engine.common.impl.IndexManagerBuildingStateHolder.createBackend(IndexManagerBuildingStateHolder.java:135) ~[hibernate-search-engine-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.search.engine.common.impl.IndexManagerBuildingStateHolder.createBackends(IndexManagerBuildingStateHolder.java:71) ~[hibernate-search-engine-6.2.2.Final.jar:6.2.2.Final]
    at org.hibernate.search.engine.common.impl.Search

Solution

  • Answering to "EDIT1" ("Connection is closed").

    This kind of error generally means the container is not actually reachable at the given host/port.

    I'd suggest putting a breakpoint in your test just after your container starts, and checking if:

    1. You didn't get Hibernate Search errors before reaching the breakpoint. If you did, your container is starting too late. You will need to refer to the Spring documentation about testing, to know how to set up test containers.
    2. You can reach Elasticsearch (e.g. using curl manually in a terminal). If you can't, there's a problem with how you start your test container.

    Also, a bit off-topic, but FWIW the Elasticsearch test container feature is built into Quarkus.

    If you must stick to Spring, maybe you can at least have a look at the Quarkus code and see how it's done there, to figure out what's wrong/different in your code...