Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

Integration tests using Testcontainers pass locally but time out intermittently in CI. What would you check?

I would check, in order: whether the CI agent really has a Docker daemon with enough CPU and memory; how long image pulls take and whether we hit registry rate limits, which a mirror or pre-pulled images fix; whether the wait strategy waits for the service to be ready rather than just for its port; and whether parallel jobs on the same host compete for resources. Throughout, I would collect container logs on failure, because they usually show the real cause.

Why this question is asked

Intermittent failures are the most expensive kind. A test that fails one run in ten makes people press retry, then stop trusting the suite, and eventually real failures are ignored too. Testcontainers tests are especially prone to this in CI, because the CI machine is a very different environment from a developer's laptop. Interviewers ask this to see whether we debug systematically instead of just raising timeouts. In this answer, we will recap how Testcontainers starts a container, see where time goes, then check each likely cause with a concrete fix, starting with how to get evidence.

1A Quick Recap of What Testcontainers Does

Testcontainers is a library that starts real services, such as PostgreSQL, Kafka or Redis, in Docker containers for the duration of a test. When a test asks for a container, Testcontainers talks to a Docker daemon, pulls the image if it is not already present, starts the container, maps its ports to random free ports on the host, and then waits until the service is ready before the test continues. If readiness does not arrive within the startup timeout, the test fails with a ContainerLaunchException or a timeout message.

So a timeout can come from any of these steps: no usable Docker daemon, a slow pull, a slow start because the machine is overloaded, or a wait that is looking for the wrong signal. On a laptop with images cached and a fast CPU, all of these are quick. On a shared CI agent, any of them can be slow some of the time, which is exactly what intermittent means.

Step throughWhere a Testcontainers start can lose time
  1. Find a Docker daemonlocal socket, remote host or cloud runtime
  2. Pull the imageminutes on a cold agent, rate limits
  3. Start the containerslow on an overloaded host
  4. Wait strategy satisfied?
    YesTest runsNoTimeout after the startup limit
  5. Run the testsconnections to mapped ports

5 steps in order. Play them, or step through them one at a time.

Each step is fast on a laptop and can be slow on a shared CI agent.

2First, Collect Evidence: Container Logs on Failure

Before changing anything, we make failures tell us why. Testcontainers can stream a container's output to our test logger, and the CI job should keep test logs as artifacts. With logs, a timeout stops being a mystery: we can see whether PostgreSQL was still running recovery, whether Kafka was waiting for its controller, or whether the container never started at all.

Java
@Container
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:17")
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("postgres"))) // container output in our logs
.withStartupTimeout(Duration.ofMinutes(2));
// On failure the log shows, for example:
// postgres: database system was not properly shut down; automatic recovery in progress
// postgres: database system is ready to accept connections <- arrived 74 s after start

Notice the timestamps. If the ready line arrives after 74 seconds on a CI agent, the problem is not our code but the environment. The class names here follow the Testcontainers 1.x API; version 2 reorganised some modules and packages, so check your version.

3Is There a Docker Daemon, With Enough Resources?

The first check is basic. Does the agent have Docker at all, and which kind? Some CI setups run builds inside a container and give them Docker through a mounted socket or a Docker-in-Docker sidecar; others use a remote daemon or a cloud service such as Testcontainers Cloud. Testcontainers logs the environment it found at startup, including the Docker host and server version, and the total memory it reports; that line is worth reading.

Then resources. A Kafka broker and PostgreSQL together may want 1.5 to 2 GB of memory and a couple of CPU cores to start quickly, on top of the JVM running the tests. A CI agent with 2 vCPUs and 4 GB, also running Maven with parallel forks, may simply be too small; under memory pressure, containers start slowly or are killed. Looking at CPU and memory graphs for the agent during a failed run usually settles this.

4Image Pulls and Rate Limits

On a fresh CI agent, every image is pulled from scratch. A Kafka image of several hundred megabytes can take a minute or more, and if many jobs pull at once, longer. Docker Hub also rate-limits pulls, especially anonymous ones, so at busy times pulls fail or stall. A pull is not counted against the container's startup timeout in the same way, but it does make the whole test class slow and can hit the job's overall time limit.

The fixes are to pull from a registry mirror or an internal registry that caches the images, configured once for Testcontainers through its image name substitution, for example the hub.image.name.prefix setting or an ImageNameSubstitutor; to authenticate pulls; and to pre-pull the images on agent images or in an early pipeline step so tests start warm. Pinning exact image tags, such as postgres:17.6 rather than latest, also avoids surprise downloads when a tag moves.

5Wait Strategies: Ready, Not Just Listening

A wait strategy tells Testcontainers how to decide that a container is ready. The default for a generic container is to wait for its exposed port to accept connections. But a port can be open long before the service is usable: some databases open the port, then run initialisation scripts, then restart; a Spring Boot app in a container listens before its context is fully up. On a fast laptop the gap is tiny; on a slow agent it is seconds, and the first test hits a service that is not ready, which looks like a random timeout or connection error.

Java
// Waits only for the port: can pass too early on a slow machine
GenericContainer<?> app = new GenericContainer<>("acme/pricing:1.4.2").withExposedPorts(8080);
// Waits for the service to say it is ready
GenericContainer<?> app = new GenericContainer<>("acme/pricing:1.4.2")
.withExposedPorts(8080)
.waitingFor(Wait.forHttp("/actuator/health/readiness").forStatusCode(200)) // real readiness
.withStartupTimeout(Duration.ofMinutes(2)); // generous on CI
// For images with a HEALTHCHECK, or a known log line:
// .waitingFor(Wait.forHealthcheck())
// .waitingFor(Wait.forLogMessage(".*ready to accept connections.*\\s", 2)) // PostgreSQL logs it twice

The specialised modules, such as PostgreSQLContainer and KafkaContainer, already come with sensible wait strategies, which is one reason to prefer them over GenericContainer. Notice also that we raise the startup timeout only after fixing the wait strategy; a longer timeout with the wrong signal only hides the problem.

6Parallel Jobs Competing on the Same Host

Finally, look at what else runs at the same time. Many CI systems run several jobs on one host. If four pipelines each start PostgreSQL, Kafka and a Spring context while Maven runs tests in parallel forks, the host is overloaded, and the failures come and go with the load, which matches the intermittent pattern exactly. Correlating failures with time of day or with the number of concurrent jobs often shows this.

The fixes are to give integration-test jobs dedicated or larger agents, to limit concurrency for those jobs, and to start fewer containers: share one container per JVM across test classes instead of starting a new one per class, for example with a static container in a base class or Spring Boot's @ServiceConnection on a shared test configuration. Starting PostgreSQL once instead of 40 times removes most of the load.

7Follow-up questions

Should we just increase the startup timeout?

Only after we know why starts are slow. A generous timeout, for example two minutes on CI, is reasonable once the wait strategy is right and resources are adequate, but raising it to ten minutes to hide an overloaded host makes the suite slow and still flaky.

What is Ryuk and can it cause problems?

Ryuk is a small helper container Testcontainers starts to remove test containers when the JVM exits, even after a crash. Some locked-down CI environments do not allow it, for example because privileged containers are blocked; then it can be disabled with TESTCONTAINERS_RYUK_DISABLED=true, as long as the CI agent cleans up containers some other way.

8Conclusion

In this answer, we debugged intermittent Testcontainers timeouts in CI. We started by collecting container logs on failure, then checked that the agent has a Docker daemon with enough CPU and memory, removed slow pulls and rate limits with a mirror or pre-pulled images, replaced port-only waits with strategies that wait for real readiness, and reduced contention from parallel jobs, including by sharing containers across test classes.

What to say in the interviewAbout 80 seconds aloud

Open with

Intermittent timeouts that never happen locally almost always mean the CI environment is slower or more crowded, so I would collect evidence before touching timeouts. First I would add a log consumer so container output ends up in the test logs, and keep those logs as CI artifacts, because the container log usually shows whether the service was still starting, recovering or never started.

Then, if they want more

Then I would check the Docker side: does the agent really have a daemon, which one Testcontainers found, and does it have enough CPU and memory for PostgreSQL, Kafka and the test JVM together.

Next, image pulls: cold agents pull hundreds of megabytes and Docker Hub rate-limits, so I would use a registry mirror through image name substitution, authenticate, pin tags and pre-pull images.

Then wait strategies: waiting for a port is not the same as ready, so I would use the module containers or wait for an HTTP readiness endpoint, a health check or the right log line, and only then set a sensible startup timeout like two minutes.

Finally I would look at parallel jobs on the same host, give integration tests bigger or dedicated agents, and share one container per JVM instead of starting one per test class.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Integration tests using Testcontainers pass locally but time out intermittently in CI....": https://scrivio-interview-library.netlify.app/java/version-control-and-ci/9694af0fb8/