Skip to content

Commit 7ee83c7

Browse files
committed
docs: explain immutable builder behavior
1 parent 3e8e1db commit 7ee83c7

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.2.2"/>
7373
<PackageVersion Include="Microsoft.Playwright" Version="1.55.0"/>
7474
<PackageVersion Include="Milvus.Client" Version="2.3.0-preview.1"/>
75-
<PackageVersion Include="MongoDB.Driver" Version="3.8.0"/>
75+
<PackageVersion Include="MongoDB.Driver" Version="3.8.1"/>
7676
<PackageVersion Include="MQTTnet" Version="5.0.1.1416"/>
7777
<PackageVersion Include="MyCouch" Version="7.6.0"/>
7878
<PackageVersion Include="MySqlConnector" Version="2.2.5"/>

docs/api/create_docker_container.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,44 @@ Using `OverwriteEnumerable<string>(Array.Empty<string>())` removes all default c
214214

215215
You can create your own `ComposableEnumerable<T>` implementation to control exactly how configuration values are composed or modified.
216216

217+
## Reusing builder configurations
218+
219+
Testcontainers builders are immutable. Every builder method returns a new instance that includes the updated configuration. The existing builder instance remains unchanged.
220+
221+
This behavior is by design. It allows you to share a common configuration and derive multiple containers from it without modifying the original builder, for example for A/B testing:
222+
223+
```csharp
224+
var baseBuilder = new PostgreSqlBuilder()
225+
.WithUsername("Username")
226+
.WithPassword("Password")
227+
.WithLabel("Key", "Value");
228+
229+
var postgres15 = baseBuilder
230+
.WithImage("postgres:15")
231+
.Build();
232+
233+
var postgres14 = baseBuilder
234+
.WithImage("postgres:14")
235+
.Build();
236+
```
237+
238+
If you configure a builder across multiple statements or conditionally, reassign the return value. Calling `WithImage(...)` without using the returned builder does not update the existing instance:
239+
240+
```csharp
241+
var builder = new PostgreSqlBuilder()
242+
.WithImage("postgres:15")
243+
.WithUsername("Username")
244+
.WithPassword("Password")
245+
.WithLabel("Key", "Value");
246+
247+
if (Debugger.IsAttached)
248+
{
249+
builder = builder.WithImage("postgres:14");
250+
}
251+
252+
var container = builder.Build();
253+
```
254+
217255
## Examples
218256

219257
An NGINX container that binds the HTTP port to a random host port and hosts static content. The example connects to the web server and checks the HTTP status code.

0 commit comments

Comments
 (0)