简体   繁体   中英

Mountebank imposter ports not accessible when mountebank started using dotnet-testcontainers

I am using dotnet-testcontainers https://github.com/HofmeisterAn/dotnet-testcontainers to spin up a container with mountebank in my xUnit test. I can successfully create a mountebank client and create an imposter successfully. The issue is that when the test is run, the app tries to make a call to the imposter on http://localhost:3000 and gets connection refused.

I can successfully open http://localhost:2525 and can see Mountebank default page. So mountebank is running fine. I also confirmed that the imposter was created successfully on port 3000 by looking at docker container log. I also tried to use Postman to make a call to my imposter at http:localhost:3000 and get connection refused.

What could be the issue? Is this an issue that port 3000 in the docker container is not exposed or something? Below is my code:

MountebankClient mbClient = new MountebankClient();

var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
                .WithImage("bbyars/mountebank")
                .WithName("mountebank")
                .WithPortBinding(2525, false)
                .WithHostname("localhost");

var testContainers = testcontainersBuilder.Build();
await testContainers.StartAsync();

var testImposter = mbClient.CreateHttpImposter(3000);
testImposter.AddStub().ReturnsBody(HttpStatusCode.OK, File.ReadAllText(@".\Stubs\testImposter.json"));
mbClient.Submit(testImposter);

Might be useful for someone else who would face this issue. Figured out what the issue was. I was not mapping the host port and the imposter port in the container. Read about published portshttps://docs.docker.com/config/containers/container-networking/ and used WithExposedPort(3000) and then did port binding of that port with host port using WithPortBinding(3000,3000) where the first port in this method is host port and second port is container port.

var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
        .WithImage("bbyars/mountebank")
        .WithName("mountebank")
        .WithPortBinding(2525, 2525)
        .WithExposedPort(3000)
        .WithExposedPort(3100)
        .WithPortBinding(3000, 3000)
        .WithPortBinding(3100, 3100)                 
        .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(2525))
        .WithHostname("localhost");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM