Start test container of ElasticSearch with plugin
Asked Answered
R

3

6

I'm using testcontainers.org using docker.elastic.co/elasticsearch/elasticsearch-oss:7.3.2 and I want to use it to test the plugin I'm updating, but I can't find a way to install it inside test environment.

I can try to copy file inside and install it

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

but then container is already started and elastic search is already running so plugin will not be loaded so I would need to kill it and replicate how it is created, sounds like a lot of hacking around. Is there some better way to do this?

Radices answered 27/9, 2019 at 13:10 Comment(0)
C
3

I solved this problem by using Testcontainers Dockerfile DSL

For example following code snippet works for me:

@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);
Corkboard answered 4/11, 2019 at 13:22 Comment(0)
T
0

I was able to start an Elasticsearch test container with a plugin this way (it's a Kotlin code):

ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:7.10.0").apply {
    withCreateContainerCmdModifier { cmd ->
        cmd.withCmd(
            *arrayOf(
                "bash",
                "-c",
                """/usr/share/elasticsearch/bin/elasticsearch-plugin install <URL> &&
                   su elasticsearch -s /usr/share/elasticsearch/bin/elasticsearch
                """.trimIndent()
            )
        )
    }
    start()
}
Transducer answered 9/9, 2022 at 21:52 Comment(0)
Q
0

For me this worked:

private static final String DOCKER_IMAGE = "docker.elastic.co/elasticsearch/elasticsearch:6.8.5"
private static final ElasticsearchContainer container = new ElasticsearchContainer(DOCKER_IMAGE);

static {
    container.withCreateContainerCmdModifier((cmd) -> {
        cmd.withCmd(
            "bash", "-c", "./bin/elasticsearch-plugin install analysis-icu && docker-entrypoint.sh eswrapper");

    });
    container.withStartupTimeout(Duration.ofSeconds(60));
}


@BeforeClass
public static void start() {
    container.start();
}

@AfterClass
public static void stop() {
    container.stop();
}

please note that Elasticsearch version 6.8.5 in this example is old and you should probably use a newer one.

Quimby answered 15/8, 2023 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.