From 5152cffa097b660292989cddd490f80dcfefa994 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Tue, 28 Mar 2023 19:11:36 +0200 Subject: [PATCH] feat: Always use Docker's assigned random host port (Ryuk) (#852) --- docs/examples/{compose.md => dind.md} | 12 +++++++++++- mkdocs.yml | 2 +- src/Testcontainers/Builders/ContainerBuilder`3.cs | 3 +++ .../Configurations/TestcontainersSettings.cs | 1 + src/Testcontainers/Containers/ResourceReaper.cs | 1 - 5 files changed, 16 insertions(+), 3 deletions(-) rename docs/examples/{compose.md => dind.md} (63%) diff --git a/docs/examples/compose.md b/docs/examples/dind.md similarity index 63% rename from docs/examples/compose.md rename to docs/examples/dind.md index d46a9a1e3..00d006e73 100644 --- a/docs/examples/compose.md +++ b/docs/examples/dind.md @@ -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. diff --git a/mkdocs.yml b/mkdocs.yml index 6a88dfdce..49d8f42cc 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -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 diff --git a/src/Testcontainers/Builders/ContainerBuilder`3.cs b/src/Testcontainers/Builders/ContainerBuilder`3.cs index aa5bdb8ce..25fa41219 100644 --- a/src/Testcontainers/Builders/ContainerBuilder`3.cs +++ b/src/Testcontainers/Builders/ContainerBuilder`3.cs @@ -182,6 +182,9 @@ public TBuilderEntity WithPortBinding(string port, bool assignRandomHostPort = f /// 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 { { containerPort, hostPort } }; return Clone(new ContainerConfiguration(portBindings: portBindings)).WithExposedPort(containerPort); } diff --git a/src/Testcontainers/Configurations/TestcontainersSettings.cs b/src/Testcontainers/Configurations/TestcontainersSettings.cs index e06038588..f20aefedb 100644 --- a/src/Testcontainers/Configurations/TestcontainersSettings.cs +++ b/src/Testcontainers/Configurations/TestcontainersSettings.cs @@ -140,6 +140,7 @@ static TestcontainersSettings() /// - https://github.com/docker/for-win/issues/11584. /// [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 ResourceReaperPublicHostPort { get; set; } = _ => 0; diff --git a/src/Testcontainers/Containers/ResourceReaper.cs b/src/Testcontainers/Containers/ResourceReaper.cs index cec34f1bd..92cae87f7 100644 --- a/src/Testcontainers/Containers/ResourceReaper.cs +++ b/src/Testcontainers/Containers/ResourceReaper.cs @@ -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();