Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/features/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,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.
Expand All @@ -114,6 +114,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.
10 changes: 8 additions & 2 deletions internal/core/docker_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
34 changes: 30 additions & 4 deletions internal/core/docker_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -347,7 +355,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) {
Expand All @@ -359,7 +367,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) {
Expand All @@ -384,7 +392,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) {
Expand Down Expand Up @@ -449,7 +457,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("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() {
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) {
Expand Down
Loading