Unable to build WireMockContainerBuilder with custom image #730

Closed
opened 2025-12-29 15:32:29 +01:00 by adam · 8 comments
Owner

Originally created by @OliBomby on GitHub (Dec 9, 2025).

Originally assigned to: @StefH on GitHub.

Describe the bug

Custom WireMock images that include their own mappings like from the guide can not be used in WireMockContainerBuilder.WithImage() leading to a InvalidOperationException on Build() because _imageOS is never set.

Expected behavior:

var container = new WireMockContainerBuilder()
            .WithLinuxImage()
            .WithImage("custom-wiremock:latest")
            .WithAutoRemove(true)
            .WithCleanUp(true)
            .Build();

await container.StartAsync().ConfigureAwait(false);
var client = container.CreateClient();
// Start requesting the built-in mappings

I expect with the above code to work an not throw an exception. However after WithLinuxImage() _imageOS is still set to null because the builder objects gets recreated from scratch with only the docker config remaining.

Originally created by @OliBomby on GitHub (Dec 9, 2025). Originally assigned to: @StefH on GitHub. ### Describe the bug Custom WireMock images that include their own mappings like from [the guide](https://wiremock.org/docs/standalone/docker/#building-your-own-image) can not be used in `WireMockContainerBuilder.WithImage()` leading to a `InvalidOperationException` on `Build()` because `_imageOS` is never set. ### Expected behavior: ```cs var container = new WireMockContainerBuilder() .WithLinuxImage() .WithImage("custom-wiremock:latest") .WithAutoRemove(true) .WithCleanUp(true) .Build(); await container.StartAsync().ConfigureAwait(false); var client = container.CreateClient(); // Start requesting the built-in mappings ``` I expect with the above code to work an not throw an exception. However after `WithLinuxImage()` `_imageOS` is still set to `null` because the builder objects gets recreated from scratch with only the docker config remaining.
adam added the feature label 2025-12-29 15:32:29 +01:00
adam closed this issue 2025-12-29 15:32:29 +01:00
Author
Owner

@StefH commented on GitHub (Dec 9, 2025):

@OliBomby
Please note that this project is related to WireMock.Net , the manual you reference is for wiremock (the Java docker version).
This will not work.

@StefH commented on GitHub (Dec 9, 2025): @OliBomby Please note that this project is related to WireMock.Net , the manual you reference is for wiremock (the Java docker version). This will not work.
Author
Owner

@OliBomby commented on GitHub (Dec 9, 2025):

Please note that this project is related to WireMock.Net , the manual you reference is for wiremock (the Java docker version). This will not work.

Ah I did not realize there was a distinction in the WireMock version used in this Docker image.

I still think it should work though. It almost works just like this, if it didn't throw the InvalidOperationException the rest of my code would've worked. The REST API of the Java and .NET versions of WireMock are almost the same.

Here's a minimal working example of using the Java image in a .NET test container:

var container = new ContainerBuilder()
    .WithImage("custom-wiremock:latest")
    .WithPortBinding(8080, true)
    .WithAutoRemove(true)
    .WithCleanUp(true)
    .Build();

await container.StartAsync().ConfigureAwait(false);

// Wait for WireMock to start
Thread.Sleep(5000);

var client = new HttpClient();
client.BaseAddress = new UriBuilder(Uri.UriSchemeHttp, container.Hostname, container.GetMappedPublicPorts()[8080]).Uri;

This code would be much cleaner if I could make use of the WireMockContainerBuilder and WireMockContainer classes, so that would be really nice to have.

@OliBomby commented on GitHub (Dec 9, 2025): > Please note that this project is related to WireMock.Net , the manual you reference is for wiremock (the Java docker version). This will not work. Ah I did not realize there was a distinction in the WireMock version used in this Docker image. I still think it should work though. It *almost* works just like this, if it didn't throw the `InvalidOperationException` the rest of my code would've worked. The REST API of the Java and .NET versions of WireMock are almost the same. Here's a minimal working example of using the Java image in a .NET test container: ```cs var container = new ContainerBuilder() .WithImage("custom-wiremock:latest") .WithPortBinding(8080, true) .WithAutoRemove(true) .WithCleanUp(true) .Build(); await container.StartAsync().ConfigureAwait(false); // Wait for WireMock to start Thread.Sleep(5000); var client = new HttpClient(); client.BaseAddress = new UriBuilder(Uri.UriSchemeHttp, container.Hostname, container.GetMappedPublicPorts()[8080]).Uri; ``` This code would be much cleaner if I could make use of the `WireMockContainerBuilder` and `WireMockContainer` classes, so that would be really nice to have.
Author
Owner

@StefH commented on GitHub (Dec 10, 2025):

@OliBomby
I understand your question. Currently, it's indeed not possible to provide a custom WireMock.Net or wiremock-org (java) image.
I can try to add that logic.

However, the mappings are not 100% the same, so using the container.CreateClient(); will not work correct for you.
The best would be that I create a new NuGet named WireMock.Org.Testcontainers which will use a default java docker image and also provide code to provide your own custom image.

@StefH commented on GitHub (Dec 10, 2025): @OliBomby I understand your question. Currently, it's indeed not possible to provide a custom WireMock.Net or wiremock-org (java) image. I can try to add that logic. However, the mappings are not 100% the same, so using the `container.CreateClient();` will not work correct for you. The best would be that I create a new NuGet named `WireMock.Org.Testcontainers` which will use a default java docker image and also provide code to provide your own custom image.
Author
Owner

@StefH commented on GitHub (Dec 11, 2025):

I did fix it for WireMock.Net.Testcontainers --> https://github.com/wiremock/WireMock.Net/pull/1391

@StefH commented on GitHub (Dec 11, 2025): I did fix it for `WireMock.Net.Testcontainers` --> https://github.com/wiremock/WireMock.Net/pull/1391
Author
Owner

@StefH commented on GitHub (Dec 11, 2025):

The WireMock.Net.Testcontainers is updated to accept a custom image and set the _imageOS.
I will publish a new NuGet soon.


However, I cannot give any guarantee that this works for the wiremock java version.

@StefH commented on GitHub (Dec 11, 2025): The `WireMock.Net.Testcontainers` is updated to accept a custom image and set the `_imageOS`. I will publish a new NuGet soon. --- However, I cannot give any guarantee that this works for the wiremock java version.
Author
Owner

@OliBomby commented on GitHub (Dec 11, 2025):

I'm afraid this implementation still won't work for any image which doesn't have wiremock.net in the name, because _imageOS will still be null in the builder object returned by WithCustomImage, because the base.WithImage method creates a new builder object and doesn't retain the value for _imagesOS. 3f9de427b8/src/Testcontainers/Builders/ContainerBuilder%603.cs (L93-L96)
e3e2dc7c65/src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs (L281-L284)

@OliBomby commented on GitHub (Dec 11, 2025): I'm afraid this implementation still won't work for any image which doesn't have `wiremock.net` in the name, because `_imageOS` will still be `null` in the builder object returned by `WithCustomImage`, because the `base.WithImage` method creates a new builder object and doesn't retain the value for `_imagesOS`. https://github.com/testcontainers/testcontainers-dotnet/blob/3f9de427b877a4fee394eab354fd98abbee06417/src/Testcontainers/Builders/ContainerBuilder%603.cs#L93-L96 https://github.com/wiremock/WireMock.Net/blob/e3e2dc7c6506e4428d55d1154e247de751c8c6cb/src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs#L281-L284
Author
Owner

@StefH commented on GitHub (Dec 11, 2025):

I see

@StefH commented on GitHub (Dec 11, 2025): I see
Author
Owner

@StefH commented on GitHub (Dec 11, 2025):

https://github.com/wiremock/WireMock.Net/pull/1392

@StefH commented on GitHub (Dec 11, 2025): https://github.com/wiremock/WireMock.Net/pull/1392
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#730