我想通过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
我尝试使用testcontainer进行操作,例如:
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仪表板:无论如何,无法同时与(1200和32384)建立ES连接。
使用
**waitingFor**
命令运行start()行会引发 Container startup failed
错误另一个问题我如何知道测试容器中的架构(http或https)?
最佳答案
我做错了。 withExposedPorts
允许我们从容器的 Angular 公开端口(在这种情况下我不需要这样做,因为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);
附言
关于模式-我仍然不知道,但看起来testContainer运行在https上。