Skip to content

Commit

Permalink
feat: Always use Docker's assigned random host port (Ryuk) (#852)
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn authored Mar 28, 2023
1 parent 2a3688d commit 5152cff
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
12 changes: 11 additions & 1 deletion docs/examples/compose.md → docs/examples/dind.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
# Compose
# Running inside another container

## Docker Desktop

### Sibling containers

If you choose to run your tests in a Docker Wormhole configuration, which involves using sibling containers, it is necessary to mount Docker's raw socket `/var/run/docker.sock.raw`. You find more information and an explanation of the Docker bug in this [comment](https://github.com/docker/for-mac/issues/5588#issuecomment-934600089).

```console
docker run -v /var/run/docker.sock.raw:/var/run/docker.sock $IMAGE dotnet test
```

### Compose

To use Docker's Compose tool to build and run a Testcontainers environment in a Docker Desktop Wormhole configuration,
it is necessary to override Testcontainers' Docker host resolution and set the environment variable `TESTCONTAINERS_HOST_OVERRIDE` to `host.docker.internal`.
Otherwise, Testcontainers cannot access sibling containers like the Resource Reaper Ryuk or other services running on the Docker host.
Expand Down
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ nav:
- api/wait_strategies.md
- api/best_practices.md
- Examples:
- examples/compose.md
- examples/dind.md
- examples/aspnet.md
- Modules:
- modules/index.md
Expand Down
3 changes: 3 additions & 0 deletions src/Testcontainers/Builders/ContainerBuilder`3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ public TBuilderEntity WithPortBinding(string port, bool assignRandomHostPort = f
/// <inheritdoc cref="IContainerBuilder{TBuilderEntity, TContainerEntity}" />
public TBuilderEntity WithPortBinding(string hostPort, string containerPort)
{
// Remove this together with TestcontainersSettings.ResourceReaperPublicHostPort.
hostPort = "0".Equals(hostPort, StringComparison.OrdinalIgnoreCase) ? string.Empty : hostPort;

var portBindings = new Dictionary<string, string> { { containerPort, hostPort } };
return Clone(new ContainerConfiguration(portBindings: portBindings)).WithExposedPort(containerPort);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ static TestcontainersSettings()
/// - https://github.com/docker/for-win/issues/11584.
/// </remarks>
[NotNull]
[Obsolete("The Resource Reaper will use Docker's assigned random host port. This property is no longer supported. For DinD configurations see: https://dotnet.testcontainers.org/examples/dind/.")]
public static Func<IDockerEndpointAuthenticationConfiguration, ushort> ResourceReaperPublicHostPort { get; set; }
= _ => 0;

Expand Down
1 change: 0 additions & 1 deletion src/Testcontainers/Containers/ResourceReaper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ private ResourceReaper(Guid sessionId, IDockerEndpointAuthenticationConfiguratio
.WithPrivileged(requiresPrivilegedMode)
.WithAutoRemove(true)
.WithCleanUp(false)
.WithExposedPort(RyukPort)
.WithPortBinding(TestcontainersSettings.ResourceReaperPublicHostPort.Invoke(dockerEndpointAuthConfig), RyukPort)
.WithMount(dockerSocket)
.Build();
Expand Down

0 comments on commit 5152cff

Please sign in to comment.