testcontainers-java icon indicating copy to clipboard operation
testcontainers-java copied to clipboard

Can't run MockServerContainer with withNetworkMode("host")

Open yaseco opened this issue 4 years ago • 3 comments

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?

yaseco avatar Jul 16 '21 08:07 yaseco

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 avatar Jul 26 '21 11:07 guss77

@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.

bsideup avatar Jul 26 '21 12:07 bsideup

@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?

aidando73 avatar Oct 25 '22 11:10 aidando73

Closing due to a decision was made in #5151. See this comment.

eddumelendez avatar Mar 14 '23 23:03 eddumelendez