Skip to content

Commit 0ecda30

Browse files
authored
fix: Use the actual Docker endpoint to extract the socket path for the Resource Reaper (#930)
1 parent 1c92dcf commit 0ecda30

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

src/Testcontainers/Configurations/TestcontainersSettings.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public static class TestcontainersSettings
2323
private static readonly ManualResetEventSlim ManualResetEvent = new ManualResetEventSlim(false);
2424

2525
[CanBeNull]
26-
private static readonly IDockerEndpointAuthenticationProvider DockerEndpointAuthProvider =
27-
new IDockerEndpointAuthenticationProvider[]
26+
private static readonly IDockerEndpointAuthenticationProvider DockerEndpointAuthProvider
27+
= new IDockerEndpointAuthenticationProvider[]
2828
{
2929
new TestcontainersEndpointAuthenticationProvider(),
3030
new MTlsEndpointAuthenticationProvider(),
@@ -39,8 +39,8 @@ public static class TestcontainersSettings
3939
.FirstOrDefault(authProvider => authProvider.IsAvailable());
4040

4141
[CanBeNull]
42-
private static readonly IDockerEndpointAuthenticationConfiguration DockerEndpointAuthConfig =
43-
DockerEndpointAuthProvider?.GetAuthConfig();
42+
private static readonly IDockerEndpointAuthenticationConfiguration DockerEndpointAuthConfig
43+
= DockerEndpointAuthProvider?.GetAuthConfig();
4444

4545
static TestcontainersSettings()
4646
{

src/Testcontainers/Containers/ResourceReaper.cs

+9-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ namespace DotNet.Testcontainers.Containers
22
{
33
using System;
44
using System.IO;
5-
using System.Linq;
65
using System.Net.Sockets;
76
using System.Text;
87
using System.Threading;
@@ -130,7 +129,7 @@ await DefaultLock.WaitAsync(ct)
130129

131130
var requiresPrivilegedMode = TestcontainersSettings.ResourceReaperPrivilegedModeEnabled;
132131

133-
_defaultInstance = await GetAndStartNewAsync(DefaultSessionId, dockerEndpointAuthConfig, resourceReaperImage, UnixSocketMount.Instance, requiresPrivilegedMode, ct: ct)
132+
_defaultInstance = await GetAndStartNewAsync(DefaultSessionId, dockerEndpointAuthConfig, resourceReaperImage, new UnixSocketMount(dockerEndpointAuthConfig.Endpoint), requiresPrivilegedMode, ct: ct)
134133
.ConfigureAwait(false);
135134

136135
return _defaultInstance;
@@ -418,28 +417,25 @@ private sealed class UnixSocketMount : IMount
418417
{
419418
private const string DockerSocketFilePath = "/var/run/docker.sock";
420419

421-
static UnixSocketMount()
420+
public UnixSocketMount([NotNull] Uri dockerEndpoint)
422421
{
423-
}
422+
// If the Docker endpoint is a Unix socket, extract the socket path from the URI; otherwise, fallback to the default Unix socket path.
423+
Source = "unix".Equals(dockerEndpoint.Scheme, StringComparison.OrdinalIgnoreCase) ? dockerEndpoint.AbsolutePath : DockerSocketFilePath;
424424

425-
private UnixSocketMount()
426-
{
425+
// If the user has overridden the Docker socket path, use the user-specified path; otherwise, keep the previously determined source.
426+
Source = !string.IsNullOrEmpty(TestcontainersSettings.DockerSocketOverride) ? TestcontainersSettings.DockerSocketOverride : Source;
427+
Target = DockerSocketFilePath;
427428
}
428429

429-
public static IMount Instance { get; }
430-
= new UnixSocketMount();
431-
432430
public MountType Type
433431
=> MountType.Bind;
434432

435433
public AccessMode AccessMode
436434
=> AccessMode.ReadOnly;
437435

438-
public string Source
439-
=> TestcontainersSettings.DockerSocketOverride ?? GetSocketPath();
436+
public string Source { get; }
440437

441-
public string Target
442-
=> DockerSocketFilePath;
438+
public string Target { get; }
443439

444440
public Task CreateAsync(CancellationToken ct = default)
445441
{
@@ -450,12 +446,6 @@ public Task DeleteAsync(CancellationToken ct = default)
450446
{
451447
return Task.CompletedTask;
452448
}
453-
454-
private static string GetSocketPath()
455-
{
456-
var dockerEndpoints = new[] { TestcontainersSettings.OS.DockerEndpointAuthConfig.Endpoint, UnixEndpointAuthenticationProvider.DockerEngine };
457-
return dockerEndpoints.First(dockerEndpoint => "unix".Equals(dockerEndpoint.Scheme, StringComparison.OrdinalIgnoreCase)).AbsolutePath;
458-
}
459449
}
460450
}
461451
}

0 commit comments

Comments
 (0)