From 9721ba74b7afc703025f60e293ec23c677b3a11b Mon Sep 17 00:00:00 2001 From: Fiodor Tretyakov Date: Mon, 16 Nov 2026 13:29:17 +0000 Subject: [PATCH 1/2] fix: mount the engine's Docker socket for a TCP host on Windows On Windows, DockerSocketPath holds the client's default named pipe, so a tcp:// Docker host made Ryuk mount //./pipe/docker_engine and fail. Use the //var/run/docker.sock UNC path there, as the Docker Desktop branch already does. Linux and macOS are unchanged. --- docs/features/configuration.md | 4 ++-- internal/core/docker_host.go | 10 +++++++-- internal/core/docker_host_test.go | 34 +++++++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/docs/features/configuration.md b/docs/features/configuration.md index 8da214e977..37e95bf32a 100644 --- a/docs/features/configuration.md +++ b/docs/features/configuration.md @@ -100,7 +100,7 @@ _Testcontainers for Go_ will attempt to detect the Docker socket path and config However, sometimes customization is required. _Testcontainers for Go_ will respect the following order: 1. Read the **tc.host** property in the `~/.testcontainers.properties` file. E.g. `tc.host=tcp://my.docker.host:1234`. If this property is set, the returned Docker socket path -will be the default Docker socket path: `/var/run/docker.sock`. +will be the default Docker socket path: `/var/run/docker.sock`, or the `//var/run/docker.sock` UNC Path on Windows. 2. Read the **TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE** environment variable. Path to Docker's socket. Used by Ryuk, Docker Compose, and a few other containers that need to perform Docker actions. @@ -113,6 +113,6 @@ Path to Docker's socket. Used by Ryuk, Docker Compose, and a few other container 5. If the socket contains the unix schema, the schema is removed (e.g. `unix:///var/run/docker.sock` -> `/var/run/docker.sock`) -6. Else, the default location of the docker socket is used: `/var/run/docker.sock` +6. Else, the default location of the docker socket is used: `/var/run/docker.sock`, or the `//var/run/docker.sock` UNC Path on Windows The library panics if the Docker host cannot be discovered. diff --git a/internal/core/docker_host.go b/internal/core/docker_host.go index 11bac63663..ca9f987188 100644 --- a/internal/core/docker_host.go +++ b/internal/core/docker_host.go @@ -118,7 +118,7 @@ func ExtractDockerHost(ctx context.Context) (string, error) { // 3. Using a Docker client, check if the Info().OperatingSystem is "Docker Desktop" and return the default docker socket path for rootless docker. // 4. Else, Get the current Docker Host from the existing strategies: see MustExtractDockerHost. // 5. If the socket contains the unix schema, the schema is removed (e.g. unix:///var/run/docker.sock -> /var/run/docker.sock) -// 6. Else, the default location of the docker socket is used (/var/run/docker.sock) +// 6. Else, the default location of the docker socket is used (/var/run/docker.sock, or the //var/run/docker.sock UNC path on Windows) // // It panics if a Docker client cannot be created, or the Docker host cannot be discovered. func MustExtractDockerSocket(ctx context.Context) string { @@ -195,8 +195,14 @@ func extractDockerSocket(ctx context.Context) string { func extractDockerSocketFromClient(ctx context.Context, cli client.APIClient) string { // check that the socket is not a tcp or unix socket checkDockerSocketFn := func(socket string) string { - // this use case will cover the case when the docker host is a tcp socket + // this use case will cover the case when the docker host is a tcp socket. + // The socket lives on the engine's host, so on Windows return the UNC path of + // the engine's socket: DockerSocketPath is the client's named pipe there. if strings.HasPrefix(socket, TCPSchema) { + if IsWindows() { + return WindowsDockerSocketPath + } + return DockerSocketPath } diff --git a/internal/core/docker_host_test.go b/internal/core/docker_host_test.go index 402f1e9827..97fbd28446 100644 --- a/internal/core/docker_host_test.go +++ b/internal/core/docker_host_test.go @@ -41,6 +41,14 @@ var resetSocketOverrideFn = func() { os.Setenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", originalDockerSocketOverride) } +// tcpDockerSocketPath is the socket path expected for a tcp:// Docker host on the current platform. +func tcpDockerSocketPath() string { + if IsWindows() { + return WindowsDockerSocketPath + } + return DockerSocketPath +} + func testCallbackCheckPassing(_ context.Context, _ string) error { return nil } @@ -342,7 +350,7 @@ func TestExtractDockerSocketFromClient(t *testing.T) { setupTestcontainersProperties(t, content) socket := extractDockerSocketFromClient(context.Background(), mockCli{OS: "foo"}) - require.Equal(t, DockerSocketPath, socket) + require.Equal(t, tcpDockerSocketPath(), socket) }) t.Run("Docker socket from Testcontainers host takes precedence over TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", func(t *testing.T) { @@ -354,7 +362,7 @@ func TestExtractDockerSocketFromClient(t *testing.T) { t.Setenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", "/path/to/docker.sock") socket := extractDockerSocketFromClient(context.Background(), mockCli{OS: "foo"}) - require.Equal(t, DockerSocketPath, socket) + require.Equal(t, tcpDockerSocketPath(), socket) }) t.Run("Docker Socket as Testcontainers environment variable", func(t *testing.T) { @@ -379,7 +387,7 @@ func TestExtractDockerSocketFromClient(t *testing.T) { t.Setenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", testRemoteHost) host = extractDockerSocketFromClient(context.Background(), mockCli{OS: "foo"}) - require.Equal(t, DockerSocketPath, host) + require.Equal(t, tcpDockerSocketPath(), host) }) t.Run("Unix Docker Socket is passed as DOCKER_HOST variable (Docker Desktop on non-Windows)", func(t *testing.T) { @@ -444,7 +452,25 @@ func TestExtractDockerSocketFromClient(t *testing.T) { t.Setenv("DOCKER_HOST", testRemoteHost) socket = extractDockerSocketFromClient(ctx, mockCli{OS: "Ubuntu"}) - require.Equal(t, DockerSocketPath, socket) + require.Equal(t, tcpDockerSocketPath(), socket) + }) + + t.Run("TCP Docker host from a Windows client uses the engine's socket, not the client's named pipe", func(t *testing.T) { + // emulate a Windows client: init sets DockerSocketPath from the client's default named pipe + t.Setenv("GOOS", "windows") + t.Cleanup(func() { + DockerSocketPath = originalDockerSocketPath + }) + DockerSocketPath = "//./pipe/docker_engine" + + setupTestcontainersProperties(t, "") + + t.Cleanup(resetSocketOverrideFn) + os.Unsetenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE") + + t.Setenv("DOCKER_HOST", testRemoteHost) + socket := extractDockerSocketFromClient(context.Background(), mockCli{OS: "Ubuntu"}) + require.Equal(t, WindowsDockerSocketPath, socket) }) t.Run("Unix Docker Socket is passed as docker.host property", func(t *testing.T) { From f311c390e3a891c6a7d3b32c37acf8a2d9509ddb Mon Sep 17 00:00:00 2001 From: Fiodor Tretyakov Date: Fri, 4 Dec 2026 10:43:16 +0000 Subject: [PATCH 2/2] test(core): rename the Windows TCP host subtest --- internal/core/docker_host_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/core/docker_host_test.go b/internal/core/docker_host_test.go index 97fbd28446..a314ff5577 100644 --- a/internal/core/docker_host_test.go +++ b/internal/core/docker_host_test.go @@ -455,7 +455,7 @@ func TestExtractDockerSocketFromClient(t *testing.T) { require.Equal(t, tcpDockerSocketPath(), socket) }) - t.Run("TCP Docker host from a Windows client uses the engine's socket, not the client's named pipe", func(t *testing.T) { + t.Run("windows-tcp-host", func(t *testing.T) { // emulate a Windows client: init sets DockerSocketPath from the client's default named pipe t.Setenv("GOOS", "windows") t.Cleanup(func() {