[Bug]: WaitStrategyTarget Handles Multiple Ports Differently to Documentation

Open Beginner friendly
#12,196 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@suvvvv is already working on this.

Since Dec 1, 2026.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
docker, java
Domain
testing-qa

Research direction

Start with WaitStrategyTarget.getLivenessCheckPortNumbers() and how it orders mapped container ports, then review the HTTP wait strategy tests for containers with multiple exposed ports. Reproduce the intermittent selection and confirm the fix consistently targets the first mapped port, with a regression test covering two exposed ports.

Written by the indexing model from the issue text.

Description

type/bug
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

Dominant language
Java
Stars
8.8k
Forks
2k
Avg merge
2d 1h
Merged PRs (30d)
11

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from testcontainers/testcontainers-java

All issues in testcontainers/testcontainers-java

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.