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
feat: add more functional options for customising containers (#3156)
* feat: add new option for AlwaysPull image
* feat: add new option for Lifecycle Hooks image
* feat: add new option for log consumer config
* feat: new option for attaching to networks by name
* feat: add two more options (no-start and name)
* feat: add one more option (image platform)
* fix: update error
* chore: separate bridge network in options
- Not available until the next release of testcontainers-go <ahref="https://github.com/testcontainers/testcontainers-go"><spanclass="tc-version">:material-tag: main</span></a>
194
+
195
+
If you need to set the log consumer config for the container, you can use `testcontainers.WithLogConsumerConfig`. This option completely replaces the existing log consumer config, including the log consumers and the log production options.
196
+
191
197
#### WithLogger
192
198
193
199
- Since testcontainers-go <ahref="https://github.com/testcontainers/testcontainers-go/releases/tag/v0.29.0"><spanclass="tc-version">:material-tag: v0.29.0</span></a>
Please read the [Following Container Logs](/features/follow_logs) documentation for more information about creating log consumers.
216
222
223
+
#### WithAlwaysPull
224
+
225
+
- Not available until the next release of testcontainers-go <ahref="https://github.com/testcontainers/testcontainers-go"><spanclass="tc-version">:material-tag: main</span></a>
226
+
227
+
If you need to pull the image before starting the container, you can use `testcontainers.WithAlwaysPull()`.
228
+
229
+
#### WithImagePlatform
230
+
231
+
- Not available until the next release of testcontainers-go <ahref="https://github.com/testcontainers/testcontainers-go"><spanclass="tc-version">:material-tag: main</span></a>
232
+
233
+
If you need to set the platform for a container, you can use `testcontainers.WithImagePlatform(platform string)`.
234
+
235
+
#### LifecycleHooks
236
+
237
+
- Not available until the next release of testcontainers-go <ahref="https://github.com/testcontainers/testcontainers-go"><spanclass="tc-version">:material-tag: main</span></a>
238
+
239
+
If you need to set the lifecycle hooks for the container, you can use `testcontainers.WithLifecycleHooks`, which replaces the existing lifecycle hooks with the new ones.
240
+
241
+
You can also use `testcontainers.WithAdditionalLifecycleHooks`, which appends the new lifecycle hooks to the existing ones.
242
+
217
243
#### Wait Strategies
218
244
219
245
If you need to set a different wait strategy for the container, you can use `testcontainers.WithWaitStrategy` with a valid wait strategy.
@@ -282,6 +308,24 @@ In the case you need to retrieve the network name, you can simply read it from t
282
308
!!!warning
283
309
This option is not checking whether the network exists or not. If you use a network that doesn't exist, the container will start in the default Docker network, as in the default behavior.
284
310
311
+
#### WithNetworkByName
312
+
313
+
- Not available until the next release of testcontainers-go <ahref="https://github.com/testcontainers/testcontainers-go"><spanclass="tc-version">:material-tag: main</span></a>
314
+
315
+
If you want to attach your containers to an already existing Docker network by its name, you can use the `network.WithNetworkName(aliases []string, networkName string)` option, which receives an alias as parameter and the network name, attaching the container to it, and setting the network alias for that network.
316
+
317
+
!!!warning
318
+
In case the network name is `bridge`, no aliases are set. This is because network-scoped alias is supported only for containers in user defined networks.
319
+
320
+
#### WithBridgeNetwork
321
+
322
+
- Not available until the next release of testcontainers-go <ahref="https://github.com/testcontainers/testcontainers-go"><spanclass="tc-version">:material-tag: main</span></a>
323
+
324
+
If you want to attach your containers to the `bridge` network, you can use the `network.WithBridgeNetwork()` option.
325
+
326
+
!!!warning
327
+
The `bridge` network is the default network for Docker. It's not a user defined network, so it doesn't support network-scoped aliases.
328
+
285
329
#### WithNewNetwork
286
330
287
331
- Since testcontainers-go <ahref="https://github.com/testcontainers/testcontainers-go/releases/tag/v0.27.0"><spanclass="tc-version">:material-tag: v0.27.0</span></a>
Reusing a container is experimental and the API is subject to change for a more robust implementation that is not based on container names.
382
+
383
+
#### WithName
384
+
385
+
- Not available until the next release of testcontainers-go <ahref="https://github.com/testcontainers/testcontainers-go"><spanclass="tc-version">:material-tag: main</span></a>
386
+
387
+
If you need to set the name of the container, you can use the `testcontainers.WithName` option.
This option is not checking whether the container name is already in use. If you use a name that is already in use, an error is returned.
397
+
At the same time, we discourage using this option as it might lead to unexpected behavior, but we understand that in some cases it might be useful.
398
+
399
+
#### WithNoStart
400
+
401
+
- Not available until the next release of testcontainers-go <ahref="https://github.com/testcontainers/testcontainers-go"><spanclass="tc-version">:material-tag: main</span></a>
402
+
403
+
If you need to prevent the container from being started after creation, you can use the `testcontainers.WithNoStart` option.
Copy file name to clipboardExpand all lines: docs/modules/index.md
+11Lines changed: 11 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -204,7 +204,12 @@ In order to simplify the creation of the container for a given module, `Testcont
204
204
-`testcontainers.WithTmpfs`: a function that adds tmpfs mounts to the container.
205
205
-`testcontainers.WithHostPortAccess`: a function that enables the container to access a port that is already running in the host.
206
206
-`testcontainers.WithLogConsumers`: a function that sets the log consumers for the container request.
207
+
-`testcontainers.WithLogConsumerConfig`: a function that sets the log consumer config for the container request.
207
208
-`testcontainers.WithLogger`: a function that sets the logger for the container request.
209
+
-`testcontainers.WithLifecycleHooks`: a function that sets the lifecycle hooks for the container request.
210
+
-`testcontainers.WithAdditionalLifecycleHooks`: a function that appends lifecycle hooks to the existing ones for the container request.
211
+
-`testcontainers.WithAlwaysPull`: a function that pulls the image before starting the container.
212
+
-`testcontainers.WithImagePlatform`: a function that sets the image platform for the container request.
208
213
-`testcontainers.WithWaitStrategy`: a function that sets the wait strategy for the container request.
209
214
-`testcontainers.WithWaitStrategyAndDeadline`: a function that sets the wait strategy for the container request with a deadline.
210
215
-`testcontainers.WithStartupCommand`: a function that sets the execution of a command when the container starts.
@@ -217,6 +222,12 @@ In order to simplify the creation of the container for a given module, `Testcont
217
222
-`testcontainers.WithEndpointSettingsModifier`: a function that sets the endpoint settings Docker type for the container request. Please see [Advanced Settings](../features/creating_container.md#advanced-settings) for more information.
218
223
-`testcontainers.CustomizeRequest`: a function that merges the default options with the ones provided by the user. Recommended for completely customizing the container request.
219
224
-`testcontainers.WithReuseByName`: a function that marks a container to be reused if it exists or create a new one if it doesn't.
225
+
-`testcontainers.WithName`: a function that sets the name of the container.
226
+
-`testcontainers.WithNoStart`: a function that prevents the container from being started after creation, so it must be started manually.
227
+
-`network.WithNetwork`: a function that sets the network and the network aliases for the container request, reusing an already existing network.
228
+
-`network.WithNetworkName`: a function that sets the network aliases for an already existing network, by its name.
229
+
-`network.WithBridgeNetwork`: a function that sets the container to be attached to the `bridge` network.
230
+
-`network.WithNewNetwork`: a function that sets the network aliases for a throw-away network for the container request.
0 commit comments