【问题标题】:Start test container of ElasticSearch with plugin使用插件启动 ElasticSearch 的测试容器
【发布时间】:2025-11-23 00:45:02
【问题描述】:

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

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

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")

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

【问题讨论】:

    标签: java elasticsearch testcontainers


    【解决方案1】:

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

    例如以下代码 sn-p 对我有用:

    @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);
    

    【讨论】:

      最近更新 更多