Issue with 'withExposedPorts' using multiple ports
See original GitHub issueaddExposedPorts uses an array to set the exposedPorts declared as LinkedHashSet, so it maintains the order of insertions.
However, withExposedPorts, uses Lists.newArrayList(ports) which internally calls Collections.addAll(…) which does not maintain the order of insertaion.
The result is unexpected behaviour, in particular, an issue when setting the waitingFor strategy.
For instance, say we call: withExposedPorts(8080,1234) and then set waiting strategy as: Wait.forHttp(...)
If Collections decides to place 1234 first, then the code will listen using port 1234, instead of 8080.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Wait only for some of ports · Issue #933 - GitHub
When using testcontainers this way: new GenericContainer("shopify/toxiproxy") .withExposedPorts(8474, 3305); it is waiting for port 3305 to ...
Read more >Why you should never use fixed ports in your Testcontainers ...
Bind for 0.0.0.0:80 failed: port is already allocated. But there is more - localhost is the king! There was some disturbance back in...
Read more >Java Testcontainers - Cannot connect to exposed port
So I started to port those tests to TestContainers using my POP3 Server image as a GenericContainer and starting my POP3 Client classes...
Read more >Networking and communicating with containers - Testcontainers
Generally, each required port needs to be explicitly exposed. For example, we can specify one or more ports as follows: Exposing ports.
Read more >GenericContainer (TestContainers Core 1.3.0 API) - Javadoc.io
Get the actual mapped port for a given port exposed by the container. ... Allow container startup to be attempted more than once...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I assumed there was an order guaranteed since the ports are stored using LinkedHashSet. Using forPort will definitely solve the issue I’m having, so thanks for that! @bsideup, I think adding a line of documentation about it at https://www.testcontainers.org/features/startup_and_waits/ can be very useful.
Thanks.
I just run into the same problem. I don’t think this has been clearly documented. Maybe reopen?