testcontainers-java
testcontainers-java copied to clipboard
Can't run MockServerContainer with withNetworkMode("host")
Consider the following test:
@Test
public void test() {
MockServerContainer mock = new MockServerContainer("5.11.2")
.withNetworkMode("host");
mock.setPortBindings(List.of("1080:3333"));
mock.start();
while (true) {} // hang
}
The error I'm getting is:
java.lang.IllegalArgumentException: Requested port (1080) is not mapped
btw, I don't mind about the exact port from the host perspective (in my case, 3333) It could be arbitrary
Why do I get this error?
I get the same error with release 1.16 when asking for a port mapped from the container.
Example code:
public class MyContainer extends GenericContainer<MyContainer> {
public MyContainer () {
super("some-image");
withCreateContainerCmdModifier(c -> c
.withExposedPorts(new ExposedPort(8088, InternetProtocol.TCP)));
}
public String getApiURL() {
return "http://"+ this.getContainerIpAddress()+ ":" + this.getMappedPort(8088) + "/"; // <-- thrown here
}
//...
}
This works well in release 1.15.3, but broke since the 1.16 release.
@guss77 in 1.16.0, we no longer publish all ports, so your exposed port isn't published anymore and you need to publish it yourself.
There is GenericContainer#withExposedPorts that will do this for you tho, and, in general, you don't need withCreateContainerCmdModifier here.
@yaseco
Testcontainers doesn't network mode host currently.
There's still some discussion going around surrounding that. See https://github.com/testcontainers/testcontainers-java/issues/5151. The main concern atm, is that it's a platform dependent change (network mode "host" only works on linux).
Though I'd like to learn more about your use-case with network mode "host"? What are you planning to use it for?
Closing due to a decision was made in #5151. See this comment.