testcontainers/testcontainers-rs

Can we let Docker choose a port?

rotivleal opened this issue · 2 comments

I need to run tests in parallel.

Each test will spawn its own Kafka.

Each test will have a producer/consumer, connected to that Kafka.

Kafka therefore needs to expose a port, and it needs to be mapped to a host port. Problem is, since tests will run in parallel, I can't reuse the same port.

Docker allows you to generate a random host port by just running -p <internal>.

But the API forces me to specify the host port:

if let Some(ports) = image.ports() {
for port in ports {
command
.arg("-p")
.arg(format!("{}:{}", port.local, port.internal));
}

This same problem had been discussed in #285 which leads me to believe that somewhere in the API I'm allowed to do that... otherwise, why was it removed...?

Thanks.

Actually random port is assigned by default, take a look at the example in modules: https://github.com/testcontainers/testcontainers-rs-modules-community/blob/15ec27c5a50f968f48da662d1e6c0cc2ac2657c0/src/kafka/mod.rs#L133-L141

You can get host port via .get_host_port_ipv4(..)

Closing due to inactivity, don't hesitate to re-open if it's still relevant.

Also take a look at recently merged changes #575