Skip to content
Latchkey

Testcontainers "Could not start container" in CI

Testcontainers created the container and started it, but the wait strategy never confirmed readiness within the timeout, so it reports the start as failed and tears the container down. The container logs above the exception show whether the app crashed or was just slow.

What this error means

Test setup fails with "Could not start container" wrapping a "Timed out waiting for container ... to be ready" or a container-exited message. The failure is at start, before any test assertion runs.

Testcontainers
org.testcontainers.containers.ContainerLaunchException: Could not start container
Caused by: org.testcontainers.containers.ContainerLaunchException:
  Timed out waiting for container port to open (localhost ports: [49213] should be listening)

Common causes

The wait strategy timed out on a slow runner

CI hardware boots the image and app more slowly than a laptop, so the default readiness timeout can elapse before the service is up.

The container exited during startup

A missing env var, bad config, or crash makes the app exit right after start, so the wait strategy can never succeed.

How to fix it

Read the container logs first

  1. Scroll above the exception to the captured container stdout/stderr.
  2. If the app crashed, fix the config or env it complains about.
  3. If it was merely slow, raise the wait-strategy timeout for CI.

Increase the startup timeout for CI

Give a slow runner more time by extending the wait strategy so a healthy but slow boot is not treated as a failure.

Java
GenericContainer<?> c = new GenericContainer<>("myapp:latest")
    .waitingFor(Wait.forListeningPort()
        .withStartupTimeout(Duration.ofMinutes(3)));

How to prevent it

  • Set explicit, generous startup timeouts tuned for CI hardware.
  • Pass all required env/config so the container does not exit on boot.
  • Assert on container logs in setup to surface crashes quickly.

Frequently asked questions

What causes ""Could not start container""?
CI hardware boots the image and app more slowly than a laptop, so the default readiness timeout can elapse before the service is up.
How do I fix "Could not start container"?
Read the container logs first

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card