弹性搜索 TestContainers 在 Docker 中等待 URL 可访问超时

Dav*_*tti 7 elasticsearch docker docker-compose testcontainers

本地环境:

  • macOS 10.14.6
  • Docker 桌面 2.0.1.2
  • Docker 引擎 19.03.2
  • 撰写引擎 1.24.1
  • 测试容器 1.12.1

我在应用程序中使用 Elastic 搜索,并且我希望能够在我的集成测试中使用 TestContainers。使用 ElasticSearch testcontainer 的 Play Framework 应用中的示例代码:

@BeforeAll
public static void setup() {
    private static final ElasticsearchContainer ES = new ElasticsearchContainer();
    ES.start();
}
Run Code Online (Sandbox Code Playgroud)

这在本地测试时有效,但我希望能够在 Docker 容器中运行它以在我的 CI 服务器上运行。在 Docker 容器内运行测试时出现此异常:

[warn] o.t.u.RegistryAuthLocator - Failure when attempting to lookup auth config (dockerImageName: alpine:3.5, configFile: /root/.docker/config.json. Falling back to docker-java default behaviour. Exception message: /root/.docker/config.json (No such file or directory)
[warn] o.t.u.RegistryAuthLocator - Failure when attempting to lookup auth config (dockerImageName: quay.io/testcontainers/ryuk:0.2.3, configFile: /root/.docker/config.json. Falling back to docker-java default behaviour. Exception message: /root/.docker/config.json (No such file or directory)
        ?? Checking the system...
        ? Docker version should be at least 1.6.0
        ? Docker environment should have more than 2GB free disk space
[warn] o.t.u.RegistryAuthLocator - Failure when attempting to lookup auth config (dockerImageName: docker.elastic.co/elasticsearch/elasticsearch:7.1.1, configFile: /root/.docker/config.json. Falling back to docker-java default behaviour. Exception message: /root/.docker/config.json (No such file or directory)
[error] d.e.c.1.1] - Could not start container
org.testcontainers.containers.ContainerLaunchException: Timed out waiting for URL to be accessible (http://172.17.0.1:32911/ should return HTTP [200])
    at org.testcontainers.containers.wait.strategy.HttpWaitStrategy.waitUntilReady(HttpWaitStrategy.java:197)
    at org.testcontainers.containers.wait.strategy.AbstractWaitStrategy.waitUntilReady(AbstractWaitStrategy.java:35)
    at org.testcontainers.containers.GenericContainer.waitUntilContainerStarted(GenericContainer.java:675)
    at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:332)
    at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:285)
    at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:81)
    at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:283)
    at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:272)
    at controllers.HomeControllerTest.setup(HomeControllerTest.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Run Code Online (Sandbox Code Playgroud)

我已阅读此处的说明:https : //www.testcontainers.org/supported_docker_environment/continuous_integration/dind_patterns/

所以我的 docker-compose.yml 看起来像(注意:我一直在用另一个 ES 容器进行测试,如下面注释所示,但我没有在这个测试中使用它)($INSTANCE 是一个随机的 16 个字符的字符串,用于特定的测试运行):

version: '3'

services:
  # elasticsearch:
  #   container_name: elasticsearch_${INSTANCE}
  #   image: docker.elastic.co/elasticsearch/elasticsearch:6.7.2
  #   ports:
  #     - 9200:9200
  #     - 9300:9300
  #   command: elasticsearch -E transport.host=0.0.0.0
  #   logging:
  #     driver: 'none'
  #   environment:
  #     ES_JAVA_OPTS: "-Xms750m -Xmx750m"
  mainapp:
    container_name: mainapp_${INSTANCE}
    image: test_image:${INSTANCE}
    stop_signal: SIGKILL
    stdin_open: true
    tty: true
    working_dir: $PWD
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - $PWD:$PWD
    environment:
      ES_JAVA_OPTS: "-Xms1G -Xmx1G"
    command: /bin/bash /projectfolder/build/tests/wrapper.sh
Run Code Online (Sandbox Code Playgroud)

我也尝试使用此命令运行我的测试,但收到相同的错误:

docker run -it --rm -v $PWD:$PWD -w $PWD -v /var/run/docker.sock:/var/run/docker.sock test_image:68F75D8FD4C7003772C7E52B87B774F5 /bin/bash /testproject/build/tests/wrapper.sh

我尝试在我的测试容器中以相同的方式创建一个 postgres 容器并且没有问题。我也试过用 Elasticsearch 图像制作一个 GenericContainer ,但没有运气。

我不认为这是一个连接问题,因为如果我curl 172.17.0.1:{port printed to test console}从我的测试容器内部运行,我会得到一个有效的弹性搜索响应,状态码为 200,所以即使连接在那里,它似乎也尝试连接超时.

谢谢。