You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/create_docker_container.md
+38Lines changed: 38 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -214,6 +214,44 @@ Using `OverwriteEnumerable<string>(Array.Empty<string>())` removes all default c
214
214
215
215
You can create your own `ComposableEnumerable<T>` implementation to control exactly how configuration values are composed or modified.
216
216
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
+
varbaseBuilder=newPostgreSqlBuilder()
225
+
.WithUsername("Username")
226
+
.WithPassword("Password")
227
+
.WithLabel("Key", "Value");
228
+
229
+
varpostgres15=baseBuilder
230
+
.WithImage("postgres:15")
231
+
.Build();
232
+
233
+
varpostgres14=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
+
varbuilder=newPostgreSqlBuilder()
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
+
varcontainer=builder.Build();
253
+
```
254
+
217
255
## Examples
218
256
219
257
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