Skip to content

Commit ebedc9a

Browse files
docs: Add best practices (#816)
Co-authored-by: Cristian Greco <cristianrgreco@gmail.com>
1 parent 87b7505 commit ebedc9a

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

docs/api/best_practices.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Best Practices
2+
3+
At times, configuring and launching disposable Docker resources for testing purposes can be initially complex. However, modules have already minimized the amount of configuration required to a bare minimum, requiring only a single line of code to create reliable test environments. Nonetheless, developers should be aware of and adhere to these best practices to write efficient and effective tests:
4+
5+
1. Avoid assigning static names to Docker resources such as containers, networks, and volumes. Static names may clash with existing resources, causing tests to fail. Instead, use random assigned names. By default, Docker assigns random names.
6+
2. Avoid assigning static port bindings to containers. Instead, use random assigned host ports `_builder.WithPortBinding(ushort, true)` and retrieve the mapped port with `_container.GetMappedPublicPort(ushort)`.
7+
3. Avoid using `localhost` or `127.0.0.1` to connect to the container. Instead, use the `_container.Hostname` property to access the container from the test host.
8+
4. When setting up a container-to-container communication, use network aliases `_builder.WithNetworkAliases(string)` to connect to the container. Access the service running inside the container through its container port.
9+
5. Avoid mounting local host paths to containers. Instead, use `_container.WithResourceMapping(string, string)` or one of its overloaded members to copy dependent files to the container before it starts.
10+
6. In rare cases, it may be necessary to access the underlying Docker API to configure specific properties that are not exposed by Testcontainers' own API. To get access to all Docker API properties required to create a resource, use `_builder.WithCreateParameterModifier(Action<TCreateResourceEntity>)`.
11+
7. Do not disable the Resource Reaper, as it cleans up remaining test resources. Disabling it may mess up the test environment.

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ nav:
3030
- api/create_docker_network.md
3131
- api/resource-reaper.md
3232
- api/wait_strategies.md
33+
- api/best_practices.md
3334
- Examples:
3435
- examples/compose.md
3536
- examples/aspnet.md

0 commit comments

Comments
 (0)