TestContainer ElasticSearchContainer 无法连接到 Docker 守护进程

问题描述 投票:0回答:1

在使用 Spring Boot 的集成测试中,我尝试使用以下代码连接到 ElasticSearchContainer:

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

我已经安装了 Docker Desktop 并且 Docker 引擎正在运行。在 Docker Desktop 设置中勾选了“Expose daemon on tcp://localhost:2375 without TLS”选项。

测试容器依赖版本:1.19.1

Spring 启动版本 - 2.5.5

Elasticsaerch 版本:8.10.4

hibernate-搜索版本:6.2.2.Final

但是在运行测试时,我收到以下错误:

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.

并且

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)

编辑1: 我将 ElasticSeachContainer 实例化更改为下面的内容,并在环境变量中添加了 DOCKET_HOST

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)))
                    ));

现在不再出现之前的错误,而是出现以下错误:

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
elasticsearch hibernate-search testcontainers hibernate-search-6
1个回答
0
投票

回答“EDIT1”(“连接已关闭”)。

这种错误通常意味着容器在给定的主机/端口上实际上无法访问。

我建议在容器启动后在测试中放置一个断点,并检查是否:

  1. 在到达断点之前您没有收到 Hibernate Search 错误。 如果您这样做了,那么您的容器启动得太晚了。您需要参考有关测试的 Spring 文档,以了解如何设置测试容器。
  2. 您可以访问 Elasticsearch(例如,在终端中手动使用
    curl
    )。 如果不能,则说明启动测试容器的方式存在问题。

另外,有点题外话,但是 FWIW Elasticsearch 测试容器功能内置于 Quarkus 中。

如果你必须坚持使用 Spring,也许你至少可以看一下 Quarkus 代码,看看它是如何完成的,找出代码中的错误/不同之处...

© www.soinside.com 2019 - 2024. All rights reserved.