testcontainers / testcontainers-rs

A library for integration-testing against docker containers from within Rust.
https://rust.testcontainers.org
Apache License 2.0
697 stars 133 forks source link

Specify host port of exposed port. #697

Closed VladasZ closed 1 month ago

VladasZ commented 1 month ago

If I understand correctly currently expose_ports method of Image trait allows users to specify only container port. For host port it automatically assigns some temporary port starting from 55000. But how do I specify both container and host ports like I do it in docker cli with -p 5432:5432 argument?

DDtKey commented 1 month ago

Hi @VladasZ 👋

You're looking for mapped port

Image itself can't be sticked to particular host port, but you can run it with the mapping (just like with docker)

DDtKey commented 1 month ago

Additionally, depending on your use case, you may not need port mapping.

Instead, you can get the host port used for a specific exposed port using get_host_port_ipv4 of already started container.

This way, you can start multiple containers(with the same image) simultaneously

VladasZ commented 1 month ago

Hi @DDtKey Yeah this is exactly what I was looking for. Thank you very much! I understand advantages of mapping containers to different ports and I already used get_host_port_ipv4 in some of my tests. But in other test I needed to map to default Postgres port and with_mapped_port works perfectly for this.