Port mapping #902
-
Apologies if this is obvious but regarding port mapping, the best practice says:
So if I'm building a container and assign port 3000 to map from the host, if the container being built runs a service on port 3000 too, how do you instruct the service to change to the randomly assigned one? I have see an approach being used in the Kafka container builder - is that the only one? Some additional snippets within the docs for things like this would be really useful to explain the best practice guidance. Many thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I am not completely sure if I understood your question correctly. However, the service running inside the container will always use the default port. But publicly it will be (available) mapped to a random port to avoid conflicts. E.g.
If you really need the random assigned host port to configure your service running inside the container, Kafka's approach is the right way. Please note, this is an edge case and is usually not necessary. |
Beta Was this translation helpful? Give feedback.
To avoid any misunderstanding. It is
WithPortBinding(hostPort, containerPort)
, but for the random assigned host ports it isWithPortBinding(containerPort, true)
:testcontainers-dotnet/src/Testcontainers/Builders/IContainerBuilder`2.cs
Lines 164 to 171 in eedd883