运行 ES docker 带有使用测试容器的自定义端口的图像

Run ES docker image with custom port using testcontainers

我想 运行 一个容器通过 Docker 测试 运行ning ES 镜像。 经过一些研究,我发现 https://www.testcontainers.org/ and they also have a built-it 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

我尝试用测试容器来做,例如:

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() return localhost/127.0.0.1:32847 也来自 docker仪表板: 无论如何,无法与(1200 和 32384)建立 ES 连接。

运行 带有 **waitingFor** 命令的 start() 行抛出 Container startup failed 错误

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

我做错了。 withExposedPorts 允许我们从容器的角度公开端口(在这种情况下我不需要这样做,因为 ElasticContainer 已经公开了一个 http 端口(9200)和一个 tcp 端口(9300))。

要找出这些端口被映射的主机(测试运行的主机)上的随机端口,调用 getMappedPort(9200) 或在 ElasticContainer 的情况下获取 host:port 你调用 getHttpHostAddress( ).

更多信息在这里:https://www.testcontainers.org/features/networking/

最重要的是,将 init 更改为:

static ElasticsearchContainer esContainer =
    new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:7.6.2")
            .withEnv("discovery.type", "single-node");

通过以下方式获取端口:

int containerPort = esContainer.getMappedPort(9200);

P.S。 关于架构 - 我仍然不知道,但看起来 testContainer 在 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)))
        ));

您只需要绑定一个特定的端口,这是因为通用容器实现默认绑定一个免费的随机端口

    List<String> portBindings = container.getPortBindings();
    portBindings.add(String.format("%d:%d/%s", ES_PORT_HOST, ES_PORT_CONTAINER, InternetProtocol.TCP));
    container.setPortBindings(portBindings);

     // or just in one line if no previous binding required
     // container.setPortBindings(List.of(String.format("%d:%d/%s", ES_PORT_HOST, ES_PORT_CONTAINER, InternetProtocol.TCP)));

我能够按照下面的方式让它工作。我必须为 withExposedPorts()

设置 9200
container = new ElasticsearchContainer(ELASTICSEARCH_IMAGE)
            .withEnv("discovery.type", "single-node")
            .withExposedPorts(9200)     
            .withCreateContainerCmdModifier(cmd -> cmd.withHostConfig(
                        new HostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(34343), new ExposedPort(9200)))));

这是日志:

 org.elasticsearch.client.RestClient - request [GET http://localhost:34343/] returned 1 warnings