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

g0czyy6m  于 12个月前  发布在  ElasticSearch
关注(0)|答案(2)|浏览(147)

在我使用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”选项。
testcontainer依赖版本:1.19.1
Spring Boot 版本- 2.5.5
Elasticsaerch版本:8.10.4
休眠搜索版本: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

uajslkp6

uajslkp61#

切换到“EDIT 1”(“连接已关闭”)。
这种错误通常意味着容器在给定的主机/端口上实际上不可访问。
我建议在容器启动后立即在测试中设置断点,并检查是否:
1.在到达断点之前,你没有得到Hibernate Search错误。如果你得到了,你的容器启动得太晚了。你需要参考Spring文档中关于测试的内容,以了解如何设置测试容器。
1.您可以访问Elasticsearch(例如,在终端中手动使用curl)。如果您不能访问,则说明您如何启动测试容器存在问题。
此外,有点偏离主题,但FWIW Elasticsearch测试容器功能内置于Quarkus中。
如果你必须坚持使用Spring,也许你至少可以看看the Quarkus code,看看它是如何完成的,找出代码中的错误/不同之处。

lmvvr0a8

lmvvr0a82#

谢谢你@yrodiere,我看了一下Quarkus代码,并相应地修改了我的代码。下面对我有用。

ElasticsearchContainer elasticsearch =
            new ElasticsearchContainer(DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:8.10.4"))
                    .withExposedPorts(9200)
                    .withEnv("xpack.security.enabled", "false")
                    .withEnv("ES_JAVA_OPTS", "-Xms256m -Xmx256m")
                    .withStartupTimeout(Duration.ofSeconds(30))
                    .withCreateContainerCmdModifier(cmd -> cmd.withHostConfig(
                            new HostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(9200), new ExposedPort(9200)))
                    ));

字符串

相关问题