Skip to content

[Bug]: WaitStrategyTarget Handles Multiple Ports Differently to Documentation #12196

Backlog

Description

@thebeanogamer

Module

Core

Testcontainers version

2.0.5

Using the latest Testcontainers version?

Yes

Host OS

Linux

Host Arch

x86

Docker version

Client:        Podman Engine
Version:       6.2.1
API Version:   6.2.1
Go Version:    go1.27.4-X:nodwarf5
Git Commit:    3471162efb28760529d852706c493c29245def6d
Built:         Tue Nov 24 00:00:00 2026
Build Origin:  Fedora Project
OS/Arch:       linux/amd64

What happened?

I'm trying to write a test for an application that listens on two ports, one HTTP and one SMTP.

container = new GenericContainer(image)
                .withExposedPorts(8090, 8091)
                .withNetwork(network)
                .withNetworkAliases("archive")
                .waitingFor(Wait.forHttp("/").forStatusCode(200))
                .withStartupTimeout(Duration.ofSeconds(30))

https://java.testcontainers.org/features/startup_and_waits/ says that TestContainers will wait for the first mapped port to be listening on TCP (or run a HTTP healthcheck if one is defined), but I'm seeing my tests intermittently fail to start. From the logs, it seems like TestContainers is alternating between ports to run the test against, failing to start if 8091 gets selected.

Tracing this through, I think the cause is WaitStrategyTarget.getLivenessCheckPortNumbers(). The ports listening on the container are remapped into a Set meaning the order is unpredictable between JVM executions. HashSets are deterministic, but it still means you can get unlucky with which ports get exposed on the container meaning that the wrong one gets picked to healthcheck against.

Relevant log output

2026-12-01T12:07:47.006+0000 INFO /sweet_montalcini: Waiting for 30 seconds for URL: http://localhost:45447/ (where port 45447 maps to container port 8091)

Additional Information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions