TestContainer can't start due to error: Timed out waiting for log output matching
Asked Answered
K

1

11

I got "ContainerLaunchException: Timed out waiting for log output matching" when starting testcontainer for elasticserach. How should I fix this issue?

container = new ElasticsearchContainer(ELASTICSEARCH_IMAGE)
            .withEnv("discovery.type", "single-node")
            .withExposedPorts(9200);

    container.start();

12:16:50.370 [main] ERROR 🐳 [docker.elastic.co/elasticsearch/elasticsearch:7.16.3] - Could not start container org.testcontainers.containers.ContainerLaunchException: Timed out waiting for log output matching '.("message":\s?"started".|] started $)' at org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy.waitUntilReady(LogMessageWaitStrategy.java:49) at org.testcontainers.containers.wait.strategy.AbstractWaitStrategy.waitUntilReady(AbstractWaitStrategy.java:51)

Updated: I looked into contructor ElasticsearchContainer

    public ElasticsearchContainer(DockerImageName dockerImageName) {
    super(dockerImageName);
    this.caCertAsBytes = Optional.empty();
    dockerImageName.assertCompatibleWith(new DockerImageName[]{DEFAULT_IMAGE_NAME, DEFAULT_OSS_IMAGE_NAME});
    this.isOss = dockerImageName.isCompatibleWith(DEFAULT_OSS_IMAGE_NAME);
    this.logger().info("Starting an elasticsearch container using [{}]", dockerImageName);
    this.withNetworkAliases(new String[]{"elasticsearch-" + Base58.randomString(6)});
    this.withEnv("discovery.type", "single-node");
    this.addExposedPorts(new int[]{9200, 9300});
    this.isAtLeastMajorVersion8 = (new ComparableVersion(dockerImageName.getVersionPart())).isGreaterThanOrEqualTo("8.0.0");
    String regex = ".*(\"message\":\\s?\"started\".*|] started\n$)";
    this.setWaitStrategy((new LogMessageWaitStrategy()).withRegEx(regex));
    if (this.isAtLeastMajorVersion8) {
        this.withPassword("changeme");
    }

}

It uses setWaitStrategy. So I updated my code as below

container.setWaitStrategy((new LogMessageWaitStrategy()).withRegEx(regex).withTimes(1));

But I still get the same error. Here is how far the log messages go.

enter image description here

Updated again: I relized above code change doesn't update any default values.

Here is the new change:

        container.setWaitStrategy((new LogMessageWaitStrategy())
                        .withRegEx(regex)
                        .withStartupTimeout(Duration.ofSeconds(180L)));

It works with this new change. I have to copy regex from ElasticsearchContainer constructor. I hope it has a better way to override the timeout value.

Killing answered 26/4, 2022 at 18:3 Comment(1)
Hey! I've converted your edit into an answer.Isoagglutinin
I
1

Add withStartupTimeout

String regex = ".*(\"message\":\\s?\"started\".*|] started\n$)";
container.setWaitStrategy((new LogMessageWaitStrategy())
    .withRegEx(regex)
    .withStartupTimeout(Duration.ofMinutes(3)));

Adjust the `Duration to fit the startup time you expect for your container. This is particularly useful in environments where the startup time might be longer due to various reasons like limited resources or network latency.

Isoagglutinin answered 8/2 at 12:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.