Repository navigation
Description
Module
Core
Testcontainers version
2.0.5
Using the latest Testcontainers version?
Yes
Host OS
macOS 27.2.1
Host Arch
ARM
Docker version
Client:
Version: 29.8.2
API version: 1.56
Go version: go1.26.8
Git commit: 7fc2dff
Built: Wed Sep 30 19:32:03 2026
OS/Arch: darwin/arm64
Context: desktop-linux
Server: Docker Desktop 4.94.0 (241994)
Engine:
Version: 29.8.2
API version: 1.56 (minimum version 1.40)
Go version: go1.26.8
Git commit: 8af9fe3
Built: Wed Sep 30 19:31:50 2026
OS/Arch: linux/arm64
Experimental: false
containerd:
Version: v2.3.6
GitCommit: ee2735368117d2eb259779949d5e75cdafec9761
runc:
Version: 1.5.1
GitCommit: v1.5.1-0-g8f2685a4
docker-init:
Version: 0.19.0
GitCommit: de40ad0What happened?
withStartupTimeout(Duration) on a container without its own wait strategy changes the startup timeout of all containers in the JVM that use the default wait strategy.
Expected: the timeout applies only to the container where it is set.
Actual: other containers get this timeout too. A short timeout on one container makes another container fail. The result depends on the order of tests in the JVM, so it looks like a flaky test in another class.
Reproduction. Put this test in core/src/test/java/org/testcontainers/containers/ and run
./gradlew :testcontainers:test --tests 'org.testcontainers.containers.DefaultWaitStrategyTimeoutTest':
package org.testcontainers.containers;
import org.junit.jupiter.api.Test;
import java.time.Duration;
class DefaultWaitStrategyTimeoutTest {
@Test
void startupTimeoutOfOneContainerDoesNotChangeOtherContainers() {
// Never started. Only sets a short timeout for itself.
new GenericContainer<>("alpine:3.17").withStartupTimeout(Duration.ofSeconds(2));
// Default wait strategy (60 seconds). The port opens after 5 seconds.
try (
GenericContainer<?> slow = new GenericContainer<>("alpine:3.17")
.withExposedPorts(8080)
.withCommand("sh", "-c", "sleep 5; while true; do nc -l -p 8080; done")
) {
slow.start();
}
}
}- On
main(e16dca9): the test fails after about 3 seconds withContainerLaunchException(log below). - Without the first line (
new GenericContainer<>(...).withStartupTimeout(...)): the test passes after about 6 seconds.
Cause (links to main at e16dca9):
DEFAULT_WAIT_STRATEGYis onestaticinstance. Every container withoutwaitingFor(...)points to it: thewaitStrategyfield andContainerDef.getWaitStrategy()returns this shared instance.withStartupTimeout(Duration)callsgetWaitStrategy().withStartupTimeout(...), so it changes the shared instance itself.
Relevant log output
org.testcontainers.containers.ContainerLaunchException: Container startup failed for image alpine:3.17
Caused by: org.testcontainers.containers.ContainerLaunchException: Could not create/start container
Caused by: org.testcontainers.containers.ContainerLaunchException: Timed out waiting for container port to open (localhost ports: [55831] should be listening)Additional Information
A possible fix: give the container its own default strategy before changing the timeout. With this change, the test above passes:
public SELF withStartupTimeout(Duration startupTimeout) {
if (getWaitStrategy() == DEFAULT_WAIT_STRATEGY) {
waitingFor(Wait.defaultWaitStrategy());
}
getWaitStrategy().withStartupTimeout(startupTimeout);
return self();
}I can open a PR with this fix and the test if this direction is OK for you.
I found this while working on #12103: JdbcDatabaseContainer.withStartupTimeoutSeconds() must not change the shared instance either. #12103 is in draft until I fix it.
Activity
Adrastopoulos commentedon Nov 29, 2026
There is a second symptom of the shared default wait strategy: parallel starts can overwrite each other’s wait target even when neither container calls
withStartupTimeout(). The current #12172 patch copies the strategy only from that setter, so it would leave this path unchanged.Environment: Testcontainers 2.0.5 (core, latest release), macOS ARM64 with Colima, Docker Engine 29.5.2, JDK 26.
What happened?
Expected: Two distinct
GenericContainerinstances can start in parallel in one JVM. Each port check uses the container it belongs to.Actual: A container that exposes
6379can fail withRequested port (6379) is not mapped, even though Docker mapped its port and Redis was ready. In the reproduction, it looks up6379on the other container, which publishes6380.In the 2.0.5 release,
GenericContainercreates the static default, andContainerDefstores that same object.AbstractWaitStrategy.waitUntilReadyoverwriteswaitStrategyTarget, whichHostPortWaitStrategyreads again while mapping exposed ports. If the overwrite happens just after the port lookup, the internal probe can instead check the wrong container and report a listening-port timeout.Relevant log output
Additional Information
The reproduction starts two real containers on different ports. The latches control the wait order without changing either container. Save the files as
pom.xmlandRepro.java. Use a Docker environment configured for Testcontainers (tested with JDK 26).On the environment above, the default waiter failed in all three runs and the fresh-waiter variant passed all three.
Reproduction files
pom.xmlRepro.javaAs a workaround, each container can use
.waitingFor(Wait.forListeningPort()). That keeps the same check and timeout but gives each container its own target. Would it make sense to give every container its own default at construction, so both symptoms are covered?