diff --git a/docker/docker_client.go b/docker/docker_client.go index ba91917cc..78fe46b57 100644 --- a/docker/docker_client.go +++ b/docker/docker_client.go @@ -41,6 +41,14 @@ func NewClient(defaultHost string) (dockerClient client.CommonAPIClient, dockerH _url, err = url.Parse(dockerHost) isSSH := err == nil && _url.Scheme == "ssh" + isTCP := err == nil && _url.Scheme == "tcp" + + if isTCP { + // With TCP, it's difficult to determine how to expose the daemon socket to lifecycle containers, + // so we are defaulting to standard docker location by returning empty string. + // This should work well most of the time. + dockerHost = "" + } if !isSSH { dockerClient, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) diff --git a/docker/docker_client_test.go b/docker/docker_client_test.go index 2a7f9e4e4..d8d8050bf 100644 --- a/docker/docker_client_test.go +++ b/docker/docker_client_test.go @@ -18,7 +18,7 @@ import ( // Test that we are creating client in accordance // with the DOCKER_HOST environment variable -func TestNewDockerClient(t *testing.T) { +func TestNewClient(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1) defer cancel() @@ -41,6 +41,38 @@ func TestNewDockerClient(t *testing.T) { } } +func TestNewClient_DockerHost(t *testing.T) { + tests := []struct { + name string + dockerHostEnvVar string + expectedRemoteDockerHost string + }{ + { + name: "tcp", + dockerHostEnvVar: "tcp://10.0.0.1:1234", + expectedRemoteDockerHost: "", + }, + { + name: "unix", + dockerHostEnvVar: "unix:///some/path/docker.sock", + expectedRemoteDockerHost: "unix:///some/path/docker.sock", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + defer WithEnvVar(t, "DOCKER_HOST", tt.dockerHostEnvVar)() + _, host, err := docker.NewClient(client.DefaultDockerHost) + if err != nil { + t.Fatal(err) + } + if host != tt.expectedRemoteDockerHost { + t.Errorf("expected docker host %q, but got %q", tt.expectedRemoteDockerHost, host) + } + }) + } + +} + func startMockDaemon(t *testing.T, sock string) func() { server := http.Server{}