首页 > 解决方案 > 使用插件启动 ElasticSearch 的测试容器

问题描述

我正在使用 testcontainers.orgdocker.elastic.co/elasticsearch/elasticsearch-oss:7.3.2并且我想用它来测试我正在更新的插件,但是我找不到在测试环境中安装它的方法。

我可以尝试在里面复制文件并安装它

ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:$ELASTIC_VERSION")
String pluginPath = "/usr/share/elasticsearch/$PLUGIN_FILE_NAME"
container.start()
container.copyFileToContainer(MountableFile.forHostPath(new File(PLUGIN_PLUGIN_PATH).toPath()), pluginPath)
ExecResult result = container.execInContainer("bin/elasticsearch-plugin", "install", "file://$pluginPath")

但是容器已经启动并且弹性搜索已经在运行,所以插件不会被加载,所以我需要杀死它并复制它的创建方式,听起来像是很多黑客攻击。有没有更好的方法来做到这一点?

标签: javaelasticsearchtestcontainers

解决方案


我通过使用Testcontainers Dockerfile DSL解决了这个问题

例如以下代码片段对我有用:

@ClassRule
public static GenericContainer elastic = new GenericContainer(new ImageFromDockerfile()
    .withDockerfileFromBuilder(
        builder -> builder.from("elasticsearch:6.8.4")
                          .run("bin/elasticsearch-plugin", "install", "analysis-icu")
                          .run("bin/elasticsearch-plugin", "install", "analysis-smartcn")
                          .build()
)).withExposedPorts(9200);

推荐阅读