java - 使用 testcontainers 使用自定义端口运行 ES docker 镜像

标签 java docker elasticsearch testcontainers

我想运行一个通过 Docker 运行 ES 镜像的容器测试。
经过一番研究,我发现 https://www.testcontainers.org/他们还有一个内置的 ES module .

因为我的开发环境在端口 9200 和 9300 中使用 ES,所以我更喜欢使用其他端口进行测试,比如 1200 和 1300。
因此,要从 CLI 运行 docker 镜像,我使用以下命令:
docker run -p 1200:9200 -p 1300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.6.2
我尝试用 testcontainers 来做,例如:

static ElasticsearchContainer esContainer =
        new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:7.6.2")
                .withExposedPorts(1200, 9200)
                .withExposedPorts(1300, 9300)
                .withEnv("discovery.type", "single-node");
                // .waitingFor(Wait.forHttp("/")); // Wait until elastic start – cause an error

@BeforeClass
public static void initEsDockerImage() {
    esContainer.start();
    esContainer.isRunning();
}

esContainer.isRunning() 中的断点:

端口是 32384 , 运行 esContainer.getHttpHostAddress()返回 localhost/127.0.0.1:32847 以及来自 docker 仪表板:
不管怎样,无法与两者建立 ES 连接 (1200 和 32384)。

使用 **waitingFor** 运行 start() 行命令抛出 Container startup failed错误

另一个问题,我如何知道架构 (http 或 https)在测试容器中?

最佳答案

如果你想指定一个端口而不是使用随机端口,你可以这样做:

static final MySQLContainer<?> mysql =
    new MySQLContainer<>("mysql:5.6")
        .withExposedPorts(34343)
        .withCreateContainerCmdModifier(cmd -> cmd.withHostConfig(
            new HostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(34343), new ExposedPort(3306)))
        ));

关于java - 使用 testcontainers 使用自定义端口运行 ES docker 镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61913787/

相关文章:

scala - 如何为 sbt 原生打包器的 DockerPlugin 设置基础镜像?

python - more_like_this 多个索引的elasticsearch查询

java - 读取而不从 JMS 队列中删除消息

docker - Kubernetes 集群中的 Kafka - 如何从 Kubernetes 集群外部发布/使用消息

java - 尝试运行Spring Boot应用程序时无法部署到Docker

Elasticsearch "size"值在与分区聚合方面不起作用

elasticsearch - 将 dotCMS 集群连接到外部 elasticsearch

java - 为什么我的 ArrayList 会抛出 IndexOutOfBoundsException 异常?

java - 同行测试 - 有什么好的引用吗?

java - 部分搜索集合java